Skip to content

Commit

Permalink
feat: python solutions
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kant-li authored and azl397985856 committed Aug 21, 2019
1 parent 1ccf0ed commit 870b0ad
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 1 deletion.
2 changes: 2 additions & 0 deletions problems/31.next-permutation.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ Here are some examples. Inputs are in the left-hand column and its corresponding
- 找到从右边起`第一个大于nums[i]的`,并将其和nums[i]进行交换
## 代码

* 语言支持: Javascript,Python3

```js
/*
* @lc app=leetcode id=31 lang=javascript
Expand Down
34 changes: 33 additions & 1 deletion problems/33.search-in-rotated-sorted-array.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Output: -1
- 找出有序区间,然后根据target是否在有序区间舍弃一半元素
## 代码

* 语言支持: Javascript
* 语言支持: Javascript,Python3

```js
/*
Expand Down Expand Up @@ -115,6 +115,38 @@ var search = function(nums, target) {
return -1;
};
```
Python3 Code:
```python
class Solution:
def search(self, nums: List[int], target: int) -> int:
"""用二分法,先判断左右两边哪一边是有序的,再判断是否在有序的列表之内"""
if len(nums) <= 0:
return -1

left = 0
right = len(nums) - 1
while left < right:
mid = (right - left) // 2 + left
if nums[mid] == target:
return mid

# 如果中间的值大于最左边的值,说明左边有序
if nums[mid] > nums[left]:
if nums[left] <= target <= nums[mid]:
right = mid
else:
# 这里 +1,因为上面是 <= 符号
left = mid + 1
# 否则右边有序
else:
# 注意:这里必须是 mid+1,因为根据我们的比较方式,mid属于左边的序列
if nums[mid+1] <= target <= nums[right]:
left = mid + 1
else:
right = mid

return left if nums[left] == target else -1
```

## 扩展

Expand Down
41 changes: 41 additions & 0 deletions problems/39.combination-sum.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ A solution set is:

## 代码

* 语言支持: Javascript,Python3

```js
/*
* @lc app=leetcode id=39 lang=javascript
Expand Down Expand Up @@ -126,6 +128,45 @@ var combinationSum = function(candidates, target) {
return list;
};
```
Python3 Code:
```python
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
"""
回溯法,层层递减,得到符合条件的路径就加入结果集中,超出则剪枝;
主要是要注意一些细节,避免重复等;
"""
size = len(candidates)
if size <= 0:
return []

# 先排序,便于后面剪枝
candidates.sort()

path = []
res = []
self._find_path(target, path, res, candidates, 0, size)

return res

def _find_path(self, target, path, res, candidates, begin, size):
"""沿着路径往下走"""
if target == 0:
res.append(path.copy())
else:
for i in range(begin, size):
left_num = target - candidates[i]
# 如果剩余值为负数,说明超过了,剪枝
if left_num < 0:
break
# 否则把当前值加入路径
path.append(candidates[i])
# 为避免重复解,我们把比当前值小的参数也从下一次寻找中剔除,
# 因为根据他们得出的解一定在之前就找到过了
self._find_path(left_num, path, res, candidates, i, size)
# 记得把当前值移出路径,才能进入下一个值的路径
path.pop()
```

## 相关题目

Expand Down
40 changes: 40 additions & 0 deletions problems/40.combination-sum-ii.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ A solution set is:

## 代码

* 语言支持: Javascript,Python3

```js
/*
* @lc app=leetcode id=40 lang=javascript
Expand Down Expand Up @@ -130,6 +132,44 @@ var combinationSum2 = function(candidates, target) {
return list;
};
```
Python3 Code:
```python
class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
"""
与39题的区别是不能重用元素,而元素可能有重复;
不能重用好解决,回溯的index往下一个就行;
元素可能有重复,就让结果的去重麻烦一些;
"""
size = len(candidates)
if size == 0:
return []

# 还是先排序,主要是方便去重
candidates.sort()

path = []
res = []
self._find_path(candidates, path, res, target, 0, size)

return res

def _find_path(self, candidates, path, res, target, begin, size):
if target == 0:
res.append(path.copy())
else:
for i in range(begin, size):
left_num = target - candidates[i]
if left_num < 0:
break
# 如果存在重复的元素,前一个元素已经遍历了后一个元素与之后元素组合的所有可能
if i > begin and candidates[i] == candidates[i-1]:
continue
path.append(candidates[i])
# 开始的 index 往后移了一格
self._find_path(candidates, path, res, left_num, i+1, size)
path.pop()
```

## 相关题目

Expand Down

0 comments on commit 870b0ad

Please sign in to comment.