Skip to content

Commit 48cf351

Browse files
authored
Merge pull request codewithdev#63 from ShreyashRoyzada/master
Added 5 LeetCode Questions.
2 parents c6fa042 + 86e1517 commit 48cf351

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed

ONLINE JUDGE/LeetCode/#15-3Sum.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> threeSum(vector<int>& A) {
4+
if(A.size()<3)
5+
{
6+
vector<vector<int>> emp;
7+
return emp;
8+
}
9+
set<vector<int>> ans;
10+
sort(A.begin(),A.end());
11+
for(int i=0;i<A.size()-2;i++)
12+
{
13+
if(i==0||A[i]>A[i-1])
14+
{
15+
int left=i+1,right=A.size()-1;
16+
17+
while(left<right)
18+
{
19+
if(A[left]+A[right]>(-A[i]))
20+
{
21+
right--;
22+
}
23+
else if(A[left]+A[right]<(-A[i]))
24+
{
25+
left++;
26+
}
27+
28+
if((right!=left)&&(A[left]+A[right]==(-A[i])))
29+
{
30+
vector<int> temp;
31+
temp.push_back(A[i]);
32+
temp.push_back(A[left]);
33+
temp.push_back(A[right]);
34+
ans.insert(temp);
35+
left++;
36+
right--;
37+
}
38+
}
39+
40+
}
41+
}
42+
vector<vector<int>> ans1;
43+
for(auto itr=ans.begin();itr!=ans.end();itr++)
44+
{
45+
ans1.push_back(*(itr));
46+
}
47+
return ans1;
48+
}
49+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
int longestPalindrome(string s) {
4+
map<char,int> check;
5+
int ans=0;
6+
for(int i=0;i<s.size();i++)
7+
{
8+
check[s[i]]++;
9+
}
10+
vector<int> temp;
11+
for(auto &x : check)
12+
{
13+
int val = x.second;
14+
15+
if(val%2==0)
16+
{
17+
ans=ans+val;
18+
}
19+
else
20+
{
21+
temp.push_back(val);
22+
}
23+
}
24+
if(temp.size()!=0)
25+
{
26+
ans = ans+*max_element(temp.begin(),temp.end());
27+
temp.erase(max_element(temp.begin(),temp.end()));
28+
for(int i=0;i<temp.size();i++)
29+
{
30+
ans = ans + temp[i] - 1;
31+
}
32+
}
33+
return ans;
34+
}
35+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
void rotate(vector<vector<int>>& matrix) {
4+
int n=matrix.size()/2,i=0,sz=matrix.size()-1;
5+
for(int i=0;i<n;i++)
6+
{
7+
for(int j=i;j<sz-i;j++)
8+
{
9+
int temp1 = matrix[i][j];
10+
int temp2 = matrix[j][sz-i];
11+
int temp3 = matrix[sz-i][sz-j];
12+
int temp4 = matrix[sz-j][i];
13+
matrix[i][j] = temp4;
14+
matrix[j][sz-i] = temp1;
15+
matrix[sz-i][sz-j] = temp2;
16+
matrix[sz-j][i] = temp3;
17+
}
18+
}
19+
20+
}
21+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
class Solution {
3+
public:
4+
vector<int> spiralOrder(vector<vector<int>>& matrix) {
5+
vector<int> ans;
6+
if(matrix.size()==0)
7+
{
8+
return ans;
9+
}
10+
int top = 0 , right = matrix[0].size()-1, bottom = matrix.size()-1, left = 0;
11+
while(top<=bottom&&left<=right)
12+
{
13+
//top layer
14+
for(int i=left;i<=right;i++)
15+
{
16+
ans.push_back(matrix[top][i]);
17+
}
18+
top++;
19+
20+
//right layer
21+
for(int i=top;i<=bottom;i++)
22+
{
23+
ans.push_back(matrix[i][right]);
24+
}
25+
right--;
26+
if(top>bottom||left>right)
27+
{
28+
break;
29+
}
30+
//bottom layer
31+
for(int i=right;i>=left;i--)
32+
{
33+
ans.push_back(matrix[bottom][i]);
34+
}
35+
bottom--;
36+
37+
//left layer
38+
for(int i=bottom;i>=top;i--)
39+
{
40+
ans.push_back(matrix[i][left]);
41+
}
42+
left++;
43+
}
44+
return ans;
45+
}
46+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
4+
bool isPalindrome(int x) {
5+
if(x==0)
6+
return true;
7+
if(x<0)
8+
return false;
9+
int size = log10(x)+1;
10+
for(int i=0;i<size/2;i++)
11+
{
12+
int a=pow(10,size-1-(i*2)),b=10;
13+
if(x/a!=x%b)
14+
{
15+
cout<<x<<" ";
16+
return false;
17+
}
18+
else
19+
{
20+
x=x%a;
21+
x=x/b;
22+
}
23+
}
24+
return true;
25+
}
26+
};

0 commit comments

Comments
 (0)