Skip to content

Commit f50af0d

Browse files
committed
update
1 parent 78560fc commit f50af0d

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
#
4+
# Given an array of integers that is already sorted in ascending order,
5+
# find two numbers such that they add up to a specific target number.
6+
#
7+
# The function twoSum should return indices of the two numbers such that
8+
# they add up to the target, where index1 must be less than index2.
9+
# Please note that your returned answers (both index1 and index2) are not zero-based.
10+
#
11+
# You may assume that each input would have exactly one solution.
12+
#
13+
# Input: numbers={2, 7, 11, 15}, target=9
14+
# Output: index1=1, index2=2
15+
#
16+
17+
class Solution:
18+
def twoSum(self, nums, target):
19+
start, end = 0, len(nums) - 1
20+
21+
while start != end:
22+
if nums[start] + nums[end] > target:
23+
end -= 1
24+
elif nums[start] + nums[end] < target:
25+
start += 1
26+
else:
27+
return [start + 1, end + 1]
28+
29+
if __name__ == "__main__":
30+
print Solution().twoSum([2, 7, 11, 15], 9)

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
LeetCode
22
========
33

4-
Up to date (2014-12-16), there are total `166` problems on [LeetCode Online Judge](https://oj.leetcode.com/).
4+
Up to date (2014-12-18), there are total `167` problems on [LeetCode Online Judge](https://oj.leetcode.com/).
55
The number of problems is increasing recently.
6-
Here is the classification of all `166` problems.
6+
Here is the classification of all `167` problems.
77
I'll keep updating for full summary and better solutions. Stay tuned for updates.
88

99
---
@@ -472,6 +472,7 @@ Problem | Solution | Time | Space | Difficul
472472
[Search in Rotated Sorted Array II] | [search-in-rotated-sorted-array-ii.py] | _O(logn)_ | _O(1)_ | Medium |
473473
[Search Insert Position] | [search-insert-position.py] | _O(logn)_ | _O(1)_ | Medium |
474474
[Sqrt(x)] | [sqrtx.py] | _O(logn)_ | _O(1)_ | Medium |
475+
[Two Sum II - Input array is sorted] | [two-sum-ii-input-array-is-sorted.py] | _O(n)_ | _O(1)_ | Medium |
475476

476477
[Find Minimum in Rotated Sorted Array]:https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/
477478
[find-minimum-in-rotated-sorted-array.py]:https://github.com/kamyu104/LeetCode/blob/master/Python/find-minimum-in-rotated-sorted-array.py
@@ -495,7 +496,8 @@ Problem | Solution | Time | Space | Difficul
495496
[search-insert-position.py]:https://github.com/kamyu104/LeetCode/blob/master/Python/search-insert-position.py
496497
[Sqrt(x)]:https://oj.leetcode.com/problems/sqrtx/
497498
[sqrtx.py]:https://github.com/kamyu104/LeetCode/blob/master/Python/sqrtx.py
498-
499+
[Two Sum II - Input array is sorted]:https://oj.leetcode.com/problems/two-sum-ii-input-array-is-sorted/
500+
[two-sum-ii-input-array-is-sorted.py]:https://github.com/kamyu104/LeetCode/blob/master/Python/wo-sum-ii-input-array-is-sorted.py
499501

500502
---
501503

0 commit comments

Comments
 (0)