Skip to content

Commit 61293fc

Browse files
committed
15-3sum.md Added Python solution.
1 parent 436a1e7 commit 61293fc

File tree

3 files changed

+337
-26
lines changed

3 files changed

+337
-26
lines changed

README.md

+12-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ You can skip the more difficult problems and do them later.
1414

1515
# Array
1616
- [704. Binary Search](solutions/1-1000/704-binary-search.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_.
17-
- [27. Remove Element](solutions/1-1000/27-remove-element.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_ and **2** solutions.
17+
- [27. Remove Element](solutions/1-1000/27-remove-element.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_ and **2** ways.
1818
- [977. Squares of a Sorted Array](solutions/1-1000/977-squares-of-a-sorted-array.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_.
1919
- [209. Minimum Size Subarray Sum](solutions/1-1000/209-minimum-size-subarray-sum.md) was solved in _Python, Java, JavaScript, C#_.
2020
- [303. Range Sum Query - Immutable](solutions/1-1000/303-range-sum-query-immutable.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_.
@@ -35,6 +35,7 @@ You can skip the more difficult problems and do them later.
3535
- [1. Two Sum](solutions/1-1000/1-two-sum.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_.
3636
- [202. Happy Number](solutions/1-1000/202-happy-number.md) was solved in _Python, Java, JavaScript, C#_.
3737
- [454. 4Sum II](solutions/1-1000/454-4sum-ii.md) was solved in _Python, Java, JavaScript, C#, Ruby_.
38+
- [15. 3Sum](solutions/1-1000/15-3sum.md) was solved in _Python_ and 2 ways.
3839

3940
# Dynamic Programming
4041
## Basics
@@ -43,12 +44,12 @@ You can skip the more difficult problems and do them later.
4344
## House Robber
4445
- [198. House Robber](solutions/1-1000/198-house-robber.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_.
4546
- [213. House Robber II](solutions/1-1000/213-house-robber-ii.md) was solved in _Python, JavaScript, Go_.
46-
- [337. House Robber III](solutions/1-1000/337-house-robber-iii.md) was solved in _Python, JavaScript, Go_ and **2** solutions.
47+
- [337. House Robber III](solutions/1-1000/337-house-robber-iii.md) was solved in _Python, JavaScript, Go_ and **2** ways.
4748

4849
## Knapsack Problems
4950
### 0/1 Knapsack
50-
- [416. Partition Equal Subset Sum](solutions/1-1000/416-partition-equal-subset-sum.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_ and **2** solutions.
51-
- [1049. Last Stone Weight II](solutions/1001-2000/1049-last-stone-weight-ii.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_ and **2** solutions.
51+
- [416. Partition Equal Subset Sum](solutions/1-1000/416-partition-equal-subset-sum.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_ and **2** ways.
52+
- [1049. Last Stone Weight II](solutions/1001-2000/1049-last-stone-weight-ii.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_ and **2** ways.
5253
- [494. Target Sum](solutions/1-1000/494-target-sum.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_.
5354
- [474. Ones and Zeroes](solutions/1-1000/474-ones-and-zeroes.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_.
5455

@@ -81,20 +82,20 @@ You can skip the more difficult problems and do them later.
8182
# Monotonic Stack
8283
- [739. Daily Temperatures](solutions/1-1000/739-daily-temperatures.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_.
8384
- [496. Next Greater Element I](solutions/1-1000/496-next-greater-element-i.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_.
84-
- [42. Trapping Rain Water](solutions/1-1000/42-trapping-rain-water.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_ and **2** solutions.
85+
- [42. Trapping Rain Water](solutions/1-1000/42-trapping-rain-water.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_ and **2** ways.
8586
- [84. Largest Rectangle in Histogram](solutions/1-1000/84-largest-rectangle-in-histogram.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_.
8687

8788
# Graph Theory
88-
- [797. All Paths From Source to Target](solutions/1-1000/797-all-paths-from-source-to-target.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_ and **2** solutions.
89-
- [200. Number of Islands](solutions/1-1000/200-number-of-islands.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_ and **3** solutions.
90-
- [463. Island Perimeter](solutions/1-1000/463-island-perimeter.md) was solved in _Python_ and **2** solutions.
89+
- [797. All Paths From Source to Target](solutions/1-1000/797-all-paths-from-source-to-target.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_ and **2** ways.
90+
- [200. Number of Islands](solutions/1-1000/200-number-of-islands.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_ and **3** ways.
91+
- [463. Island Perimeter](solutions/1-1000/463-island-perimeter.md) was solved in _Python_ and **2** ways.
9192
- [695. Max Area of Island](solutions/1-1000/695-max-area-of-island.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_.
9293
- [827. Making A Large Island](solutions/1-1000/827-making-a-large-island.md) was solved in _Python_.
9394
- [127. Word Ladder](solutions/1-1000/127-word-ladder.md) was solved in _Python_.
94-
- [1971. Find if Path Exists in Graph](solutions/1001-2000/1971-find-if-path-exists-in-graph.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_ and 2 solutions.
95+
- [1971. Find if Path Exists in Graph](solutions/1001-2000/1971-find-if-path-exists-in-graph.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_ and 2 ways.
9596
- [684. Redundant Connection](solutions/1-1000/684-redundant-connection.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_.
9697
- [685. Redundant Connection II](solutions/1-1000/685-redundant-connection-ii.md) was solved in _Python_.
97-
- [1584. Min Cost to Connect All Points](solutions/1001-2000/1584-min-cost-to-connect-all-points.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_ and 2 solutions.
98-
- [207. Course Schedule](solutions/1-1000/207-course-schedule.md) was solved in _Python, Java, C++, C#_ and 2 solutions.
98+
- [1584. Min Cost to Connect All Points](solutions/1001-2000/1584-min-cost-to-connect-all-points.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_ and 2 ways.
99+
- [207. Course Schedule](solutions/1-1000/207-course-schedule.md) was solved in _Python, Java, C++, C#_ and 2 ways.
99100

100101
More LeetCode problems will be added soon...

solutions/1-1000/1-two-sum.md

+51-15
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Difficulty: **Easy**
3131
- `-10**9 <= target <= 10**9`
3232
- **Only one valid answer exists.**
3333

34-
## Solution 1
34+
## Solution 1: Two pointers (should master)
3535
[中文题解](#中文题解)
3636

3737
1. The time complexity of the brute force solution is `O(n**2)`. To improve efficiency, you can sort the array, and then use **two pointers**, one pointing to the head of the array and the other pointing to the tail of the array, and decide `left += 1` or `right -= 1` according to the comparison of `sum` and `target`.
@@ -41,7 +41,7 @@ Difficulty: **Easy**
4141
* Time: `O(N * log N)`.
4242
* Space: `O(N)`.
4343

44-
## Solution 2
44+
## Solution 2: Use Map (also should master)
4545
1. In `Map`, `key` is `num`, and `value` is array `index`.
4646
2. Traverse the array, if `target - num` is in `Map`, return it. Otherwise, add `num` to `Map`.
4747

@@ -73,6 +73,12 @@ for (let i = 0; i < nums.length; i++) {
7373
* Space: `O(n)`.
7474

7575
## Java
76+
### Solution 1: Two pointers
77+
```java
78+
// Welcome to create a PR to complete the code, thanks!
79+
```
80+
81+
### Solution 2: Using Map
7682
```java
7783
class Solution {
7884
public int[] twoSum(int[] nums, int target) {
@@ -92,19 +98,6 @@ class Solution {
9298
```
9399

94100
## Python
95-
### Solution 2: Using Map
96-
```python
97-
class Solution:
98-
def twoSum(self, nums: List[int], target: int) -> List[int]:
99-
num_to_index = {}
100-
101-
for i, num in enumerate(nums):
102-
if target - num in num_to_index:
103-
return [num_to_index[target - num], i]
104-
105-
num_to_index[num] = i
106-
```
107-
108101
### Solution 1: Two pointers
109102
```python
110103
class Solution:
@@ -130,7 +123,26 @@ class Solution:
130123
return [original_nums.index(nums[left]), len(nums) - 1 - original_nums[::-1].index(nums[right])]
131124
```
132125

126+
### Solution 2: Using Map
127+
```python
128+
class Solution:
129+
def twoSum(self, nums: List[int], target: int) -> List[int]:
130+
num_to_index = {}
131+
132+
for i, num in enumerate(nums):
133+
if target - num in num_to_index:
134+
return [num_to_index[target - num], i]
135+
136+
num_to_index[num] = i
137+
```
138+
133139
## C++
140+
### Solution 1: Two pointers
141+
```cpp
142+
// Welcome to create a PR to complete the code, thanks!
143+
```
144+
145+
### Solution 2: Using Map
134146
```cpp
135147
class Solution {
136148
public:
@@ -151,6 +163,12 @@ public:
151163
```
152164

153165
## JavaScript
166+
### Solution 1: Two pointers
167+
```javascript
168+
// Welcome to create a PR to complete the code, thanks!
169+
```
170+
171+
### Solution 2: Using Map
154172
```javascript
155173
var twoSum = function (nums, target) {
156174
let numToIndex = new Map()
@@ -166,6 +184,12 @@ var twoSum = function (nums, target) {
166184
```
167185

168186
## C#
187+
### Solution 1: Two pointers
188+
```c#
189+
// Welcome to create a PR to complete the code, thanks!
190+
```
191+
192+
### Solution 2: Using Map
169193
```c#
170194
public class Solution {
171195
public int[] TwoSum(int[] nums, int target) {
@@ -185,6 +209,12 @@ public class Solution {
185209
```
186210

187211
## Go
212+
### Solution 1: Two pointers
213+
```go
214+
// Welcome to create a PR to complete the code, thanks!
215+
```
216+
217+
### Solution 2: Using Map
188218
```go
189219
func twoSum(nums []int, target int) []int {
190220
numToIndex := map[int]int{}
@@ -202,6 +232,12 @@ func twoSum(nums []int, target int) []int {
202232
```
203233

204234
## Ruby
235+
### Solution 1: Two pointers
236+
```ruby
237+
# Welcome to create a PR to complete the code, thanks!
238+
```
239+
240+
### Solution 2: Using Map
205241
```ruby
206242
def two_sum(nums, target)
207243
num_to_index = {}

0 commit comments

Comments
 (0)