Skip to content

Commit e8b2d82

Browse files
committed
Solution 1. Two Sum
1 parent be6fbdf commit e8b2d82

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ The repository contains the best versions of my solutions to [LeetCode](https://
1010
| # | Title | Solution | Time | Space |
1111
|---| ----- | -------- | ---- | ----- |
1212
|217|[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/)|[Python](./Solutions/217-contains-duplicate.py)|O(n)|O(n)|
13-
|53|[Maximum Subarray](https://leetcode.com/problems/maximum-subarray/)|[Python](./Solutions/53-maximum-subarray.py)|O(n)|O(1)|
13+
|53|[Maximum Subarray](https://leetcode.com/problems/maximum-subarray/)|[Python](./Solutions/53-maximum-subarray.py)|O(n)|O(1)|
14+
|1|[Two Sum](https://leetcode.com/problems/two-sum/)|[Python](./Solutions/1-two-sum.py)|O(n^2)|O(1)|

Solutions/1-two-sum.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''
2+
Given an array of integers nums and an integer target, return indices of
3+
the two numbers such that they add up to target.
4+
5+
You may assume that each input would have exactly one solution,
6+
and you may not use the same element twice.
7+
8+
You can return the answer in any order.
9+
10+
Example 1:
11+
Input: nums = [2,7,11,15], target = 9
12+
Output: [0,1]
13+
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
14+
15+
Example 2:
16+
Input: nums = [3,2,4], target = 6
17+
Output: [1,2]
18+
19+
Example 3:
20+
Input: nums = [3,3], target = 6
21+
Output: [0,1]
22+
'''
23+
24+
from typing import List
25+
26+
class Solution:
27+
def twoSum(self, nums: List[int], target: int) -> List[int]:
28+
length = len(nums)
29+
30+
for i in range(length - 1):
31+
if nums[i] + nums[i + 1] == target:
32+
return [i, i + 1]
33+
34+
for i in range(length - 1):
35+
for j in range(i + 1, length):
36+
if nums[i] + nums[j] == target:
37+
return [i, j]
38+
39+
# Result
40+
s = Solution()
41+
print(s.twoSum([3,2,3], 6))

Solutions/217-contains-duplicate.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
'''
1717

1818
from typing import List
19-
2019
class Solution:
2120
def containsDuplicate(self, nums: List[int]) -> bool:
2221
duplicates = {}

Solutions/53-maximum-subarray.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
'''
2020

2121
from typing import List
22-
23-
2422
class Solution:
2523
def maxSubArray(self, nums: List[int]) -> int:
2624
currentSum = 0
@@ -38,4 +36,4 @@ def maxSubArray(self, nums: List[int]) -> int:
3836

3937
# Result
4038
s = Solution()
41-
print(s.containsDuplicate([-2, 1, -3, 4, -1, 2, 1, -5, 4])) # 6
39+
print(s.maxSubArray([-2, 1, -3, 4, -1, 2, 1, -5, 4])) # 6

0 commit comments

Comments
 (0)