Skip to content

Commit

Permalink
feat: 增加Python Code
Browse files Browse the repository at this point in the history
  • Loading branch information
kant-li authored and azl397985856 committed Aug 22, 2019
1 parent e8d58a7 commit 5adf7cb
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
26 changes: 26 additions & 0 deletions problems/46.permutations.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Output:

## 代码

* 语言支持: Javascript,Python3

```js
/*
* @lc app=leetcode id=46 lang=javascript
Expand Down Expand Up @@ -94,6 +96,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
```

## 相关题目

Expand Down
30 changes: 30 additions & 0 deletions problems/47.permutations-ii.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Output:

## 代码

* 语言支持: Javascript,Python3

```js
/*
* @lc app=leetcode id=47 lang=javascript
Expand Down Expand Up @@ -96,6 +98,34 @@ var permuteUnique = function(nums) {
return list;
};
```
Python3 code:
```Python
class Solution:
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
"""与46题一样,当然也可以直接调用itertools的函数,然后去重"""
return list(set(itertools.permutations(nums)))

def permuteUnique(self, nums: List[int]) -> List[List[int]]:
"""自己写回溯法,与46题相比,需要去重"""
# 排序是为了去重
nums.sort()
res = []
def _backtrace(nums, pre_list):
if len(nums) <= 0:
res.append(pre_list)
else:
for i in range(len(nums)):
# 如果是同样的数字,则之前一定已经生成了对应可能
if i > 0 and nums[i] == nums[i-1]:
continue
p_list = pre_list.copy()
p_list.append(nums[i])
left_nums = nums.copy()
left_nums.pop(i)
_backtrace(left_nums, p_list)
_backtrace(nums, [])
return res
```

## 相关题目

Expand Down
25 changes: 25 additions & 0 deletions problems/48.rotate-image.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ var rotate = function(matrix) {

## 代码

* 语言支持: Javascript,Python3

```js
/*
* @lc app=leetcode id=48 lang=javascript
Expand Down Expand Up @@ -121,3 +123,26 @@ var rotate = function(matrix) {
}
};
```
Python3 Code:
```Python
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
先做矩阵转置(即沿着对角线翻转),然后每个列表翻转;
"""
n = len(matrix)
for i in range(n):
for j in range(i, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
for m in matrix:
m.reverse()

def rotate2(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
通过内置函数zip,可以简单实现矩阵转置,下面的代码等于先整体翻转,后转置;
不过这种写法的空间复杂度其实是O(n);
"""
matrix[:] = map(list, zip(*matrix[::-1]))
```
20 changes: 20 additions & 0 deletions problems/49.group-anagrams.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ var groupAnagrams = function(strs) {

## 代码

* 语言支持: Javascript,Python3

```js
/*
* @lc app=leetcode id=49 lang=javascript
Expand Down Expand Up @@ -100,3 +102,21 @@ var groupAnagrams = function(strs) {
return Object.values(hashTable);
};
```
Python3 Code:
```Python
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
"""
思路同上,在Python中,这里涉及到3个知识点:
1. 使用内置的 defaultdict 字典设置默认值;
2. 内置的 ord 函数,计算ASCII值(等于chr)或Unicode值(等于unichr);
3. 列表不可哈希,不能作为字典的键,因此这里转为元组;
"""
str_dict = collections.defaultdict(list)
for s in strs:
s_key = [0] * 26
for c in s:
s_key[ord(c)-ord('a')] += 1
str_dict[tuple(s_key)].append(s)
return str_dict.values()
```

0 comments on commit 5adf7cb

Please sign in to comment.