Skip to content

Commit

Permalink
feat: bunch of python3 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

* Add python3 solution for problem46

* Add python3 solution for problem 47

* Add python3 code for problem 48

* Add python3 solution for problem 49

* Add python3 code for problem 55

* Add python3 solution for problem 56

* Add python3 solution for problem 86

* Update 31.next-permutation.md
  • Loading branch information
kant-li authored and azl397985856 committed Sep 22, 2019
1 parent be5b648 commit 795b47e
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 1 deletion.
33 changes: 33 additions & 0 deletions problems/31.next-permutation.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,39 @@ class Solution:
j -= 1
```

Python3 Code:

```python
class Solution:
def nextPermutation(self, nums):
"""
Do not return anything, modify nums in-place instead.
:param list nums
"""
# 第一步,从后往前,找到下降点
down_index = None
for i in range(len(nums)-2, -1, -1):
if nums[i] < nums[i+1]:
down_index = i
break
# 如果没有下降点,重新排列
if down_index is None:
nums.reverse()
# 如果有下降点
else:
# 第二步,从后往前,找到比下降点大的数,对换位置
for i in range(len(nums)-1, i, -1):
if nums[down_index] < nums[i]:
nums[down_index], nums[i] = nums[i], nums[down_index]
break
# 第三步,重新排列下降点之后的数
i, j = down_index+1, len(nums)-1
while i < j:
nums[i], nums[j] = nums[j], nums[i]
i += 1
j -= 1
```

## 相关题目

- [46.next-permutation](./46.next-permutation.md)
Expand Down
26 changes: 25 additions & 1 deletion problems/46.permutations.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Output:

## 代码

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

Javascript Code:

Expand Down Expand Up @@ -97,6 +97,30 @@ var permute = function(nums) {
return list
};
```
Python3 Code:
```Python
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
"""itertools库内置了这个函数"""
return itertools.permutations(nums)

def permute2(self, nums: List[int]) -> List[List[int]]:
"""自己写回溯法"""
res = []
def _backtrace(nums, pre_list):
if len(nums) <= 0:
res.append(pre_list)
else:
for i in nums:
# 注意copy一份新的调用,否则无法正常循环
p_list = pre_list.copy()
p_list.append(i)
left_nums = nums.copy()
left_nums.remove(i)
_backtrace(left_nums, p_list)
_backtrace(nums, [])
return res
```

Python Code:

Expand Down
18 changes: 18 additions & 0 deletions problems/55.jump-game.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Explanation: You will always arrive at index 3 no matter what. Its maximum

## 代码

* 语言支持: Javascript,Python3

```js

/*
Expand Down Expand Up @@ -94,3 +96,19 @@ var canJump = function(nums) {
};

```
Python3 Code:
```Python
class Solution:
def canJump(self, nums: List[int]) -> bool:
"""思路同上"""
_max = 0
_len = len(nums)
for i in range(_len-1):
if _max < i:
return False
_max = max(_max, nums[i] + i)
# 下面这个判断可有可无,但提交的时候数据会好看点
if _max >= _len - 1:
return True
return _max >= _len - 1
```
25 changes: 25 additions & 0 deletions problems/56.merge-intervals.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ NOTE: input types have been changed on April 15, 2019. Please reset to default c

## 代码

* 语言支持: Javascript,Python3

```js


Expand Down Expand Up @@ -69,3 +71,26 @@ var merge = function(intervals) {
return intervals.filter(q => q);
};
```
Python3 Code:
```Python
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
"""先排序,后合并"""
if len(intervals) <= 1:
return intervals

# 排序
def get_first(a_list):
return a_list[0]
intervals.sort(key=get_first)

# 合并
res = [intervals[0]]
for i in range(1, len(intervals)):
if intervals[i][0] <= res[-1][1]:
res[-1] = [res[-1][0], max(res[-1][1], intervals[i][1])]
else:
res.append(intervals[i])

return res
```
37 changes: 37 additions & 0 deletions problems/86.partition-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Output: 1->2->2->4->3->5

## 代码

* 语言支持: Javascript,Python3

```js
/*
* @lc app=leetcode id=86 lang=javascript
Expand Down Expand Up @@ -105,3 +107,38 @@ var partition = function(head, x) {
return dummyHead1.next;
};
```
Python3 Code:
```python
class Solution:
def partition(self, head: ListNode, x: int) -> ListNode:
"""在原链表操作,思路基本一致,只是通过指针进行区分而已"""
# 在链表最前面设定一个初始node作为锚点,方便返回最后的结果
first_node = ListNode(0)
first_node.next = head
# 设计三个指针,一个指向小于x的最后一个节点,即前后分离点
# 一个指向当前遍历节点的前一个节点
# 一个指向当前遍历的节点
sep_node = first_node
pre_node = first_node
current_node = head

while current_node is not None:
if current_node.val < x:
# 注意有可能出现前一个节点就是分离节点的情况
if pre_node is sep_node:
pre_node = current_node
sep_node = current_node
current_node = current_node.next
else:
# 这段次序比较烧脑
pre_node.next = current_node.next
current_node.next = sep_node.next
sep_node.next = current_node
sep_node = current_node
current_node = pre_node.next
else:
pre_node = current_node
current_node = pre_node.next

return first_node.next
```

0 comments on commit 795b47e

Please sign in to comment.