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 5adf7cb

Browse files
kant-liazl397985856
authored andcommitted
feat: 增加Python Code
1 parent e8d58a7 commit 5adf7cb

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

‎problems/46.permutations.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Output:
4343

4444
## 代码
4545

46+
* 语言支持: Javascript,Python3
47+
4648
```js
4749
/*
4850
* @lc app=leetcode id=46 lang=javascript
@@ -94,6 +96,30 @@ var permute = function(nums) {
9496
return list
9597
};
9698
```
99+
Python3 Code:
100+
```Python
101+
class Solution:
102+
def permute(self, nums: List[int]) -> List[List[int]]:
103+
"""itertools库内置了这个函数"""
104+
return itertools.permutations(nums)
105+
106+
def permute2(self, nums: List[int]) -> List[List[int]]:
107+
"""自己写回溯法"""
108+
res = []
109+
def _backtrace(nums, pre_list):
110+
if len(nums) <= 0:
111+
res.append(pre_list)
112+
else:
113+
for i in nums:
114+
# 注意copy一份新的调用,否则无法正常循环
115+
p_list = pre_list.copy()
116+
p_list.append(i)
117+
left_nums = nums.copy()
118+
left_nums.remove(i)
119+
_backtrace(left_nums, p_list)
120+
_backtrace(nums, [])
121+
return res
122+
```
97123

98124
## 相关题目
99125

‎problems/47.permutations-ii.md‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Output:
4040

4141
## 代码
4242

43+
* 语言支持: Javascript,Python3
44+
4345
```js
4446
/*
4547
* @lc app=leetcode id=47 lang=javascript
@@ -96,6 +98,34 @@ var permuteUnique = function(nums) {
9698
return list;
9799
};
98100
```
101+
Python3 code:
102+
```Python
103+
class Solution:
104+
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
105+
"""与46题一样,当然也可以直接调用itertools的函数,然后去重"""
106+
return list(set(itertools.permutations(nums)))
107+
108+
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
109+
"""自己写回溯法,与46题相比,需要去重"""
110+
# 排序是为了去重
111+
nums.sort()
112+
res = []
113+
def _backtrace(nums, pre_list):
114+
if len(nums) <= 0:
115+
res.append(pre_list)
116+
else:
117+
for i in range(len(nums)):
118+
# 如果是同样的数字,则之前一定已经生成了对应可能
119+
if i > 0 and nums[i] == nums[i-1]:
120+
continue
121+
p_list = pre_list.copy()
122+
p_list.append(nums[i])
123+
left_nums = nums.copy()
124+
left_nums.pop(i)
125+
_backtrace(left_nums, p_list)
126+
_backtrace(nums, [])
127+
return res
128+
```
99129

100130
## 相关题目
101131

‎problems/48.rotate-image.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ var rotate = function(matrix) {
8888

8989
## 代码
9090

91+
* 语言支持: Javascript,Python3
92+
9193
```js
9294
/*
9395
* @lc app=leetcode id=48 lang=javascript
@@ -121,3 +123,26 @@ var rotate = function(matrix) {
121123
}
122124
};
123125
```
126+
Python3 Code:
127+
```Python
128+
class Solution:
129+
def rotate(self, matrix: List[List[int]]) -> None:
130+
"""
131+
Do not return anything, modify matrix in-place instead.
132+
先做矩阵转置(即沿着对角线翻转),然后每个列表翻转;
133+
"""
134+
n = len(matrix)
135+
for i in range(n):
136+
for j in range(i, n):
137+
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
138+
for m in matrix:
139+
m.reverse()
140+
141+
def rotate2(self, matrix: List[List[int]]) -> None:
142+
"""
143+
Do not return anything, modify matrix in-place instead.
144+
通过内置函数zip,可以简单实现矩阵转置,下面的代码等于先整体翻转,后转置;
145+
不过这种写法的空间复杂度其实是O(n);
146+
"""
147+
matrix[:] = map(list, zip(*matrix[::-1]))
148+
```

‎problems/49.group-anagrams.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ var groupAnagrams = function(strs) {
6868

6969
## 代码
7070

71+
* 语言支持: Javascript,Python3
72+
7173
```js
7274
/*
7375
* @lc app=leetcode id=49 lang=javascript
@@ -100,3 +102,21 @@ var groupAnagrams = function(strs) {
100102
return Object.values(hashTable);
101103
};
102104
```
105+
Python3 Code:
106+
```Python
107+
class Solution:
108+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
109+
"""
110+
思路同上,在Python中,这里涉及到3个知识点:
111+
1. 使用内置的 defaultdict 字典设置默认值;
112+
2. 内置的 ord 函数,计算ASCII值(等于chr)或Unicode值(等于unichr);
113+
3. 列表不可哈希,不能作为字典的键,因此这里转为元组;
114+
"""
115+
str_dict = collections.defaultdict(list)
116+
for s in strs:
117+
s_key = [0] * 26
118+
for c in s:
119+
s_key[ord(c)-ord('a')] += 1
120+
str_dict[tuple(s_key)].append(s)
121+
return str_dict.values()
122+
```

0 commit comments

Comments
(0)

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