Skip to content

Commit 1f0bcde

Browse files
author
rchen102
committed
2019/10/25
1 parent bb0b4f5 commit 1f0bcde

File tree

8 files changed

+50
-59
lines changed

8 files changed

+50
-59
lines changed

Python/leetcode 1.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ def twoSum(self, nums, target):
55
if (target - nums[i]) in dic:
66
return [dic[target - nums[i]], i]
77
else:
8-
dic[nums[i]] = i
9-
8+
dic[nums[i]] = i

Python/leetcode 15.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
def threeSum(self, nums: List[int]) -> List[List[int]]:
3+
res = []
4+
nums.sort()
5+
for i in range(len(nums)-2):
6+
if i == 0 or (i > 0 and nums[i] != nums[i-1]):
7+
self.twoSum(res, i, nums)
8+
return res
9+
10+
def twoSum(self, res, idx, nums):
11+
target = -nums[idx]
12+
lo, hi = idx + 1, len(nums) - 1
13+
while (lo < hi):
14+
if nums[lo] + nums[hi] == target:
15+
res.append((nums[idx], nums[lo], nums[hi]))
16+
while (lo < hi) and (nums[lo] == nums[lo+1]):
17+
lo += 1
18+
while (lo < hi) and (nums[hi] == nums[hi-1]):
19+
hi -= 1
20+
lo += 1
21+
hi -= 1
22+
elif nums[lo] + nums[hi] < target:
23+
lo += 1
24+
else:
25+
hi -= 1
26+
27+
28+
29+
30+
31+
File renamed without changes.

SUMMARY/Array/Leetcode 1.MD

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## 1 Two Sum
2+
3+
### Solution1: Brute force
4+
T: O(n^2) S: O(1)
5+
6+
双重循环查找
7+
8+
### Solution2: HashMap
9+
T: O(n) S: O(n)

SUMMARY/Array/Leetcode 15.MD

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## 15 3Sum
2+
3+
### Solution1: Sort + Two pointer
4+
T: O(n^2) S: O(1)
5+
6+
- 避免重复,首先进行排序
7+
- 利用Two Pointers实现增强版Two Sum
8+
+ 不使用HashMap的原因: 节省时间+避免重复

SUMMARY/SUMMARY.MD

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1. 输入数据中含有重复数字,输出要求含有避免重复 -> 排序

总结/Array/Leetcode 1.MD

-54
This file was deleted.

总结/Array/Leetcode 15.MD

-3
This file was deleted.

0 commit comments

Comments
 (0)