Skip to content

Commit 4a7f82f

Browse files
Add files via upload
1 parent fa7a3eb commit 4a7f82f

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Runtime: 8 ms, faster than 100.00% of C++ online submissions for Two Sum II - Input array is sorted.
2+
// Memory Usage: 9.6 MB, less than 100.00% of C++ online submissions for Two Sum II - Input array is sorted.
3+
4+
class Solution
5+
{
6+
public:
7+
vector<int> twoSum(vector<int>& numbers, int target)
8+
{
9+
vector<int> res;
10+
int leftPtr = 0, rightPtr = numbers.size() - 1;
11+
12+
while (leftPtr < rightPtr)
13+
{
14+
int temp = numbers[leftPtr] + numbers[rightPtr];
15+
if (temp == target)
16+
{
17+
res.push_back(leftPtr + 1);
18+
res.push_back(rightPtr + 1);
19+
break;
20+
}
21+
else if (temp > target)
22+
rightPtr--;
23+
else
24+
leftPtr++;
25+
}
26+
return res;
27+
}
28+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Runtime: 36 ms, faster than 99.51% of Python3 online submissions for Two Sum II - Input array is sorted.
2+
# Memory Usage: 12.6 MB, less than 100.00% of Python3 online submissions for Two Sum II - Input array is sorted.
3+
4+
class Solution:
5+
def twoSum(self, numbers: 'List[int]', target: 'int') -> 'List[int]':
6+
res = []
7+
leftPtr, rightPtr = 0, len(numbers) - 1
8+
9+
while leftPtr < rightPtr:
10+
temp = numbers[leftPtr] + numbers[rightPtr]
11+
if (temp == target):
12+
res.append(leftPtr + 1)
13+
res.append(rightPtr + 1)
14+
break
15+
elif temp > target:
16+
rightPtr -= 1
17+
else:
18+
leftPtr += 1
19+
20+
return res

0 commit comments

Comments
 (0)