[問題] leetcode的permutations
Hi
最近小弟在刷dfs的題型
無奈是跨領域,一直卡關...
想請教各第46題 Permutations
以下是我寫的code:
class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
def dfs(value):
if len(value) == len(nums):
res.append(value)
print("value=",value)
for m in nums:
if m in value:
continue
value.append(m)
dfs(value)
value.remove(m)
res = []
dfs([])
return res
我試著將 value 給 print 出來,答案是
[[1 2 3], [1 3 2] , [2 1 3], [2 3 1], [3 1 2], [3 2 1]]
正確
但我return的res卻是[[], [], [], [], [], []]
不知哪出了問題,懇請高手解惑
感謝!
--
/ ̄ ̄ ̄ ㄟ | |
/ ㄟ | |
| (> )( <) < XD |
| /// (_人_) | |
| \__/ ! | |
| ㄟ \____|
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.38.110.251
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1546227798.A.2D6.html
推
12/31 12:12,
5年前
, 1F
12/31 12:12, 1F
→
12/31 12:31,
5年前
, 2F
12/31 12:31, 2F
→
12/31 13:02,
5年前
, 3F
12/31 13:02, 3F
推
12/31 13:28,
5年前
, 4F
12/31 13:28, 4F
→
12/31 13:28,
5年前
, 5F
12/31 13:28, 5F
→
12/31 14:10,
5年前
, 6F
12/31 14:10, 6F
→
12/31 14:22,
5年前
, 7F
12/31 14:22, 7F
推
12/31 14:39,
5年前
, 8F
12/31 14:39, 8F
→
12/31 14:39,
5年前
, 9F
12/31 14:39, 9F
推
12/31 14:42,
5年前
, 10F
12/31 14:42, 10F
→
12/31 14:42,
5年前
, 11F
12/31 14:42, 11F
→
12/31 14:42,
5年前
, 12F
12/31 14:42, 12F
→
12/31 14:42,
5年前
, 13F
12/31 14:42, 13F
→
12/31 14:45,
5年前
, 14F
12/31 14:45, 14F
→
12/31 14:45,
5年前
, 15F
12/31 14:45, 15F
剛剛嘗試了一下,改成res.append(value[:])也行
但其實我還是不懂為何直接append(value)會變成append一個空白的list
能否詳加說明
感恩!
※ 編輯: ed78617 (114.38.110.251), 12/31/2018 17:29:19
推
12/31 17:29,
5年前
, 16F
12/31 17:29, 16F
→
12/31 17:38,
5年前
, 17F
12/31 17:38, 17F
→
12/31 17:38,
5年前
, 18F
12/31 17:38, 18F
→
12/31 17:58,
5年前
, 19F
12/31 17:58, 19F
→
12/31 18:33,
5年前
, 20F
12/31 18:33, 20F
→
12/31 18:42,
5年前
, 21F
12/31 18:42, 21F
推
12/31 18:54,
5年前
, 22F
12/31 18:54, 22F
→
12/31 18:54,
5年前
, 23F
12/31 18:54, 23F
推
01/01 22:54,
6年前
, 24F
01/01 22:54, 24F
→
01/01 22:57,
6年前
, 25F
01/01 22:57, 25F
→
01/01 22:57,
6年前
, 26F
01/01 22:57, 26F
→
01/01 22:59,
6年前
, 27F
01/01 22:59, 27F
→
01/01 22:59,
6年前
, 28F
01/01 22:59, 28F