-
Notifications
You must be signed in to change notification settings - Fork 33
Add LeetCode-1 (different approach) and LeetCode-167 in C++ #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c214f5a
baab005
3d6d053
8454519
501f684
e064a05
cac0d40
2aaa517
42993b8
8351c53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution { | ||
public: | ||
vector<int> twoSum(vector<int>& numbers, int target) { | ||
int index1 = 0, index2 = numbers.size() - 1, sum; | ||
vector<int> answer; | ||
|
||
while(index1 < index2) { | ||
sum = numbers[index1] + numbers[index2]; | ||
|
||
if(sum == target) | ||
break; | ||
else if(sum < target) | ||
index1++; | ||
else | ||
index2--; | ||
} | ||
|
||
answer.push_back(index1 + 1); | ||
answer.push_back(index2 + 1); | ||
|
||
return answer; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class Solution { | ||
public: | ||
vector<int> twoSum(vector<int>& nums, int target) { | ||
vector<pair<int, int>> valueIndexPair; | ||
vector<int> answer; | ||
int index1, index2, sum; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to fix the indentation. |
||
|
||
for(int k = 0; k < nums.size(); k++) { | ||
valueIndexPair.push_back(pair<int, int>(nums[k], k)); | ||
} | ||
|
||
sort(valueIndexPair.begin(), valueIndexPair.end()); | ||
|
||
index1 = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to fix the indentation. |
||
index2 = valueIndexPair.size() - 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to fix the indentation. |
||
|
||
while(index1 < index2) { | ||
sum = valueIndexPair[index1].first + valueIndexPair[index2].first; | ||
|
||
if(sum == target) | ||
break; | ||
else if(sum < target) | ||
index1++; | ||
else | ||
index2--; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to fix the indentation of the if-else block. |
||
answer.push_back(valueIndexPair[index1].second); | ||
answer.push_back(valueIndexPair[index2].second); | ||
|
||
return answer; | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to fix the indentation.