Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 870b0ad

Browse files
kant-liazl397985856
authored andcommitted
feat: python solutions
* Fix solution on problem 88 with js, fixs azl397985856#62 * Fix solution on problem 88, fixs azl397985856#62 * Add python3 solution for problem 31 * Add python3 solution for problem 33 * Add python3 solution for problem 39 * Add python3 solution for problem 40 * Add language support description
1 parent 1ccf0ed commit 870b0ad

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

‎problems/31.next-permutation.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ Here are some examples. Inputs are in the left-hand column and its corresponding
5656
- 找到从右边起`第一个大于nums[i]的`,并将其和nums[i]进行交换
5757
## 代码
5858

59+
* 语言支持: Javascript,Python3
60+
5961
```js
6062
/*
6163
* @lc app=leetcode id=31 lang=javascript

‎problems/33.search-in-rotated-sorted-array.md‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Output: -1
6262
- 找出有序区间,然后根据target是否在有序区间舍弃一半元素
6363
## 代码
6464

65-
* 语言支持: Javascript
65+
* 语言支持: Javascript,Python3
6666

6767
```js
6868
/*
@@ -115,6 +115,38 @@ var search = function(nums, target) {
115115
return -1;
116116
};
117117
```
118+
Python3 Code:
119+
```python
120+
class Solution:
121+
def search(self, nums: List[int], target: int) -> int:
122+
"""用二分法,先判断左右两边哪一边是有序的,再判断是否在有序的列表之内"""
123+
if len(nums) <= 0:
124+
return -1
125+
126+
left = 0
127+
right = len(nums) - 1
128+
while left < right:
129+
mid = (right - left) // 2 + left
130+
if nums[mid] == target:
131+
return mid
132+
133+
# 如果中间的值大于最左边的值,说明左边有序
134+
if nums[mid] > nums[left]:
135+
if nums[left] <= target <= nums[mid]:
136+
right = mid
137+
else:
138+
# 这里 +1,因为上面是 <= 符号
139+
left = mid + 1
140+
# 否则右边有序
141+
else:
142+
# 注意:这里必须是 mid+1,因为根据我们的比较方式,mid属于左边的序列
143+
if nums[mid+1] <= target <= nums[right]:
144+
left = mid + 1
145+
else:
146+
right = mid
147+
148+
return left if nums[left] == target else -1
149+
```
118150

119151
## 扩展
120152

‎problems/39.combination-sum.md‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ A solution set is:
5454

5555
## 代码
5656

57+
* 语言支持: Javascript,Python3
58+
5759
```js
5860
/*
5961
* @lc app=leetcode id=39 lang=javascript
@@ -126,6 +128,45 @@ var combinationSum = function(candidates, target) {
126128
return list;
127129
};
128130
```
131+
Python3 Code:
132+
```python
133+
class Solution:
134+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
135+
"""
136+
回溯法,层层递减,得到符合条件的路径就加入结果集中,超出则剪枝;
137+
主要是要注意一些细节,避免重复等;
138+
"""
139+
size = len(candidates)
140+
if size <= 0:
141+
return []
142+
143+
# 先排序,便于后面剪枝
144+
candidates.sort()
145+
146+
path = []
147+
res = []
148+
self._find_path(target, path, res, candidates, 0, size)
149+
150+
return res
151+
152+
def _find_path(self, target, path, res, candidates, begin, size):
153+
"""沿着路径往下走"""
154+
if target == 0:
155+
res.append(path.copy())
156+
else:
157+
for i in range(begin, size):
158+
left_num = target - candidates[i]
159+
# 如果剩余值为负数,说明超过了,剪枝
160+
if left_num < 0:
161+
break
162+
# 否则把当前值加入路径
163+
path.append(candidates[i])
164+
# 为避免重复解,我们把比当前值小的参数也从下一次寻找中剔除,
165+
# 因为根据他们得出的解一定在之前就找到过了
166+
self._find_path(left_num, path, res, candidates, i, size)
167+
# 记得把当前值移出路径,才能进入下一个值的路径
168+
path.pop()
169+
```
129170

130171
## 相关题目
131172

‎problems/40.combination-sum-ii.md‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ A solution set is:
5555

5656
## 代码
5757

58+
* 语言支持: Javascript,Python3
59+
5860
```js
5961
/*
6062
* @lc app=leetcode id=40 lang=javascript
@@ -130,6 +132,44 @@ var combinationSum2 = function(candidates, target) {
130132
return list;
131133
};
132134
```
135+
Python3 Code:
136+
```python
137+
class Solution:
138+
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
139+
"""
140+
与39题的区别是不能重用元素,而元素可能有重复;
141+
不能重用好解决,回溯的index往下一个就行;
142+
元素可能有重复,就让结果的去重麻烦一些;
143+
"""
144+
size = len(candidates)
145+
if size == 0:
146+
return []
147+
148+
# 还是先排序,主要是方便去重
149+
candidates.sort()
150+
151+
path = []
152+
res = []
153+
self._find_path(candidates, path, res, target, 0, size)
154+
155+
return res
156+
157+
def _find_path(self, candidates, path, res, target, begin, size):
158+
if target == 0:
159+
res.append(path.copy())
160+
else:
161+
for i in range(begin, size):
162+
left_num = target - candidates[i]
163+
if left_num < 0:
164+
break
165+
# 如果存在重复的元素,前一个元素已经遍历了后一个元素与之后元素组合的所有可能
166+
if i > begin and candidates[i] == candidates[i-1]:
167+
continue
168+
path.append(candidates[i])
169+
# 开始的 index 往后移了一格
170+
self._find_path(candidates, path, res, left_num, i+1, size)
171+
path.pop()
172+
```
133173

134174
## 相关题目
135175

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /