Skip to content

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

Merged
merged 10 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Algorithms/Easy/167_TwoSum II/Solution.cpp
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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this code to

int index1 = 0, index2 = numbers.size() - 1, sum;

vector<int> returnVector;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name the variable returnVector to answer and change in other places


while(index1 < index2) {
sum = numbers[index1] + numbers[index2];

if(sum == target)
break;
else if(sum < target)
index1++;
else
index2--;
}

returnVector.push_back(index1 + 1);
returnVector.push_back(index2 + 1);

return returnVector;
}
};
33 changes: 33 additions & 0 deletions Algorithms/Easy/1_TwoSum/SolutionOne.cpp
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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this code to

vector<pair<int, int>> valueIndexPair;

vector<int> returnVector;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the variable name returnVector to answer and also, change in other places where returnVector has been used. Additionally, fix the indentation.

int index1, index2, sum;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the indentation.


for(int k = 0; k < nums.size(); k++) {
valueIndexPair.push_back(pair<int,int>(nums[k], k));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this code to

valueIndexPair.push_back(pair<int, int>(nums[k], k));

}

sort(valueIndexPair.begin(), valueIndexPair.end());

index1 = 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indentation

index2 = valueIndexPair.size() - 1;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indentation


while(index1 < index2) {
sum = valueIndexPair[index1].first + valueIndexPair[index2].first;

if(sum == target)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indentation

break;
else if(sum < target)
index1++;
else
index2--;
}

Copy link
Owner

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 of the if-else block.

returnVector.push_back(valueIndexPair[index1].second);
returnVector.push_back(valueIndexPair[index2].second);

return returnVector;
}
};