Skip to content

Instantly share code, notes, and snippets.

@vinitshahdeo
Last active September 15, 2018 10:32
Show Gist options
  • Save vinitshahdeo/a1420e50e4cdfc2d2ee162fc1a0580ea to your computer and use it in GitHub Desktop.
Save vinitshahdeo/a1420e50e4cdfc2d2ee162fc1a0580ea to your computer and use it in GitHub Desktop.
LeetCode Problems
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int,int> m;
vector<int> v;
for(int i=0;i<nums.size();i++)
{
if(m.find(target-nums[i])!=m.end())
{
v.push_back(m[target-nums[i]]);
v.push_back(i);
}
else
{
m[nums[i]]=i;
}
}
return v;
}
};
//Two Sum II - Input array is sorted
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int low=0;
int high=numbers.size()-1;
//vector<int> v(2);
while(low<high)
{
//int sum=numbers[low]+numbers[high];
if((numbers[low]+numbers[high])==target)
{
return {low+1,high+1};
}
else if((numbers[low]+numbers[high])>target)
{
high--;
}
else
{
low++;
}
}
return {};
}
};
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
set<vector<int> > st;
sort(num.begin(), num.end());
int i, j, k;
int n = num.size();
for (i = 0; i < n - 2; i++) {
j = i+1;
k = n-1;
while (j < k) {
int sum = num[i] + num[j] + num[k];
if (sum == 0) {
vector<int> v;
v.push_back(num[i]);
v.push_back(num[j]);
v.push_back(num[k]);
st.insert(v);
j++;
k--;
} else if (sum > 0) {
k--;
} else if (sum < 0) {
j++;
}
}
}
vector<vector <int> >result (st.begin(), st.end());
return result;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment