We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7db46d5 commit e6206a0Copy full SHA for e6206a0
two-sum/wclee2265.cpp
@@ -0,0 +1,25 @@
1
+// https://leetcode.com/problems/two-sum/
2
+
3
+class Solution {
4
+public:
5
+ vector<int> twoSum(vector<int>& nums, int target) {
6
+ int n = nums.size();
7
+ vector<pair<int, int> > numsIndices;
8
9
+ for(int i=0; i<n; i++){
10
+ numsIndices.push_back({nums[i], i});
11
+ }
12
13
+ sort(numsIndices.begin(), numsIndices.end());
14
+ int i, j = n-1;
15
+ for(i=0; i<n; i++) {
16
+ while(numsIndices[i].first + numsIndices[j].first > target) j--;
17
+ if(numsIndices[i].first + numsIndices[j].first == target) break;
18
19
20
+ vector<int> ans;
21
+ ans.push_back(numsIndices[i].second);
22
+ ans.push_back(numsIndices[j].second);
23
+ return ans;
24
25
+};
0 commit comments