Skip to content

Commit a8b7ecb

Browse files
committed
add some k-sum code
1 parent 383b564 commit a8b7ecb

File tree

2 files changed

+236
-8
lines changed

2 files changed

+236
-8
lines changed

Array/K-SUM题解.md

Lines changed: 106 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,115 @@
33

44

55
### 题目详解
6-
#### 1.[1-Two Sum](https://leetcode.com/problems/two-sum/) [easy]
6+
# K-SUM
77

8-
#### 2.[167-Two Sum II](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) [easy]
8+
## 1.Two Sum
99

10+
- 题目:1 Two sum 【easy】
1011

11-
#### 3.[170.Two Sum III - Data structure design](https://leetcode.com/problems/two-sum-iii-data-structure-design/) [easy]
12+
- 解法:hash-map
1213

14+
- code
1315

14-
#### 4.[653. Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) [easy]
16+
Python
1517

18+
```python
19+
#hash-map
20+
class Solution(object):
21+
def twoSum(self, nums, target):
22+
"""
23+
:type nums: List[int]
24+
:type target: int
25+
:rtype: List[int]
26+
"""
27+
if len(nums) <= 1:
28+
return False
29+
keys = {}
30+
for i, v in enumerate(nums):
31+
if target-v in keys:
32+
return [keys[target-v],i]
33+
else:
34+
keys[v] = i
35+
return []
36+
37+
```
38+
39+
40+
41+
42+
43+
44+
45+
## 2.Two Sum II - Input array is sorted
46+
47+
- 题目:167 Two Sum II - Input array is sorted 【easy】
48+
49+
- 解法:
50+
51+
- hash-map:排序了而已,和第一题很像啊,就是把下标改一下
52+
- 双指针:与二分法结合
53+
- 二分查找:
54+
55+
- code:
56+
57+
Java
58+
59+
```java
60+
/*hash-map*/
61+
class Solution {
62+
public int[] twoSum(int[] numbers, int target) {
63+
int[] result = new int[2];
64+
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
65+
for (int i = 0;i<numbers.length;i++){
66+
if (map.containsKey(target-numbers[i])){
67+
result[0]=map.get(target-numbers[i]);
68+
result[1]= i+1;
69+
}
70+
map.put(numbers[i],i+1);
71+
}
72+
return result;
73+
}
74+
}
75+
```
76+
77+
78+
79+
Python
80+
81+
```python
82+
# 双指针法
83+
def twoSum1(self, numbers, target):
84+
l, r = 0, len(numbers)-1
85+
while l < r:
86+
s = numbers[l] + numbers[r]
87+
if s == target:
88+
return [l+1, r+1]
89+
elif s < target:
90+
l += 1
91+
else:
92+
r -= 1
93+
94+
# dictionary
95+
def twoSum2(self, numbers, target):
96+
dic = {}
97+
for i, num in enumerate(numbers):
98+
if target-num in dic:
99+
return [dic[target-num]+1, i+1]
100+
dic[num] = i
101+
102+
# binary search
103+
def twoSum(self, numbers, target):
104+
for i in xrange(len(numbers)):
105+
l, r = i+1, len(numbers)-1
106+
tmp = target - numbers[i]
107+
while l <= r:
108+
mid = l + (r-l)//2
109+
if numbers[mid] == tmp:
110+
return [i+1, mid+1]
111+
elif numbers[mid] < tmp:
112+
l = mid+1
113+
else:
114+
r = mid-1
115+
```
116+
117+

Array/array.md

Lines changed: 130 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 数组
1+
数组
22

33
### 什么是数组
44

@@ -33,9 +33,80 @@
3333
【一维数组】
3434
#### 1.K-Sum
3535
​ 这类题目通常会给定一个数组和一个值,让求出这个数组中两个/三个/K个值的和等于这个给定的值target。leetcode第一题就是two-sum,对于这类题目,首先看题目要求的时间复杂度和空间复杂度是什么,其次看有没有限制条件,如要求不能有重复的子数组或者要求按照升序/降序排列等。解法如下:
36-
- 暴力解法:最常见,但是通常会超时,只能作为备选,
37-
- hash-map:建立一个hash-map循环遍历一次即可
38-
- two-pointers:定位两个指针根绝和的大小来移动另外一个。这里设定的指针个数根据题目中K的个数来定。3Sum中可以设定3个指针,固定两个,移动另一个
36+
37+
> 1.暴力解法:最常见,但是通常会超时,只能作为备选,
38+
>
39+
> 2.hash-map:建立一个hash-map循环遍历一次即可
40+
>
41+
> 3.two-pointers:定位两个指针根绝和的大小来移动另外一个。这里设定的指针个数根据题目中K的个数来定。3Sum中可以设定3个指针,固定两个,移动另一个。
42+
43+
- 例题:167 Two Sum II - Input array is sorted 【easy】
44+
- 题解:
45+
- test case:
46+
- 解题思路:
47+
- code:
48+
49+
Java
50+
51+
```java
52+
/*hash-map*/
53+
class Solution {
54+
public int[] twoSum(int[] numbers, int target) {
55+
int[] result = new int[2];
56+
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
57+
for (int i = 0;i<numbers.length;i++){
58+
if (map.containsKey(target-numbers[i])){
59+
result[0]=map.get(target-numbers[i]);
60+
result[1]= i+1;
61+
}
62+
map.put(numbers[i],i+1);
63+
}
64+
return result;
65+
}
66+
}
67+
```
68+
69+
70+
71+
Python
72+
73+
```python
74+
# 双指针法
75+
def twoSum1(self, numbers, target):
76+
l, r = 0, len(numbers)-1
77+
while l < r:
78+
s = numbers[l] + numbers[r]
79+
if s == target:
80+
return [l+1, r+1]
81+
elif s < target:
82+
l += 1
83+
else:
84+
r -= 1
85+
86+
# dictionary
87+
def twoSum2(self, numbers, target):
88+
dic = {}
89+
for i, num in enumerate(numbers):
90+
if target-num in dic:
91+
return [dic[target-num]+1, i+1]
92+
dic[num] = i
93+
94+
# binary search
95+
def twoSum(self, numbers, target):
96+
for i in xrange(len(numbers)):
97+
l, r = i+1, len(numbers)-1
98+
tmp = target - numbers[i]
99+
while l <= r:
100+
mid = l + (r-l)//2
101+
if numbers[mid] == tmp:
102+
return [i+1, mid+1]
103+
elif numbers[mid] < tmp:
104+
l = mid+1
105+
else:
106+
r = mid-1
107+
```
108+
109+
39110

40111

41112
#### 2.区间问题
@@ -98,6 +169,8 @@ class Solution(object):
98169
>解释:满足子数组和=7的最小长度数组是[4,3],所以output=2
99170
- 解题思路:求的数字要大于等于这个数字target,譬如这个testcase中,[2,3,1,2,4,3],从前往后相加,前四项相加的和为8.已经大于7了,但是我们的target是7,后面的几项也都是正整数,继续往后走,肯定也会大于target7,所以这个时候我们把left指针往右移动一位,那么就是相当于成为了一个滑动窗口(sliding window),这种方法在String类型的题目出现的更多。
100171

172+
​ 在本题中,刚好最后两位4,3相加等于target7,所以滑动窗口滑到最后一位的时候满足题解,但是有个问题,如果在滑动的过程中,中间出现某个数字大于target或者等于target
173+
101174
![image](https://raw.githubusercontent.com/huxiaoman7/leetcodebook/master/Array/pic/subarray.png)
102175
103176
- code:
@@ -138,9 +211,62 @@ def minSubArrayLen(self, s, nums):
138211

139212
#### Rotate题型
140213

214+
- 例题:48. Rotate Image 【medium】
215+
216+
- 题解:
217+
218+
- test case:
219+
220+
> Given input matrix =
221+
> [
222+
> [1,2,3],
223+
> [4,5,6],
224+
> [7,8,9]
225+
> ],
226+
>
227+
> rotate the input matrix in-place such that it becomes:
228+
> [
229+
> [7,4,1],
230+
> [8,5,2],
231+
> [9,6,3]
232+
> ]
233+
234+
- 解题方法:
235+
236+
- 1.转置后再翻转
237+
- 2.旋转四个角
238+
239+
- code:
240+
241+
Python
242+
243+
```python
244+
# 旋转后再翻转
245+
class solution
246+
def rotate(self,matrix):
247+
"""
248+
:type matrix: List[List[int]]
249+
:rtype: void Do not return anything, modify matrix in-place instead.
250+
"""
251+
n = len(matrix[0])
252+
# 转置
253+
for i in range(n):
254+
for j in range(i,n):
255+
matrix[j][i],matrix[i][j] = matrix[i][j],matrix[j][i]
256+
257+
for i in range(n):
258+
matrix[i].reverse()
259+
260+
# 旋转四个角
261+
```
262+
263+
141264

142265

143266

267+
- 复杂度分析:
268+
- 转置后翻转:时间$O(N^2)$ ,空间$O(1)$.
269+
-
144270

145271

146272

0 commit comments

Comments
 (0)