class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
res = [[]]
for x, k in collections.Counter(nums).items():
res.extend([subset + [x] * n for subset in res for n in range(1, k+1)])
return res
class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
res=[[]]
for val in nums:
n=len(res)
for i in range(n):
#res[i]+[val] would return a new list without affecting the original res[i]
#sort the list to avoid duplicates
add=sorted(res[i]+[val])
if add not in res:
res.append(add)
return res
class Solution(object):
def numDecodings(self, s):
if not s:
return 0
dp = [0] * (1 + len(s))
dp[0] = 1
for i in range(1, len(s) + 1):
if s[i - 1] != "0":
dp[i] = dp[i - 1]
if i > 1 and 10 <= int(s[i-2:i]) <= 26:
dp[i] += dp[i - 2]
return dp[-1]
class Solution(object):
def numDecodings(self, s):
if not s: return 0
len_s = len(s)
dp = [1] + [0] * len_s
for i in range(1, len_s + 1):
if s[i - 1] != '0':
dp[i] += dp[i - 1]
if i >= 2 and 10 <= int(s[i - 2: i]) <= 26:
dp[i] += dp[i - 2]
return dp[len_s]
刘润森!
认证博客专家
Python
Java
前端
17年就读于东莞XX学院化学工程与工艺专业,GitChat作者。Runsen的微信公众号是"Python之王",因为Python入了IT的坑,从此不能自拔。公众号内容涉及Python,Java计算机、杂谈。干货与情怀同在。喜欢的微信搜索:「Python之王」。个人微信号:RunsenLiu。不关注我公号一律拉黑!!!