目录
【2】
# | Title | Acceptance | Difficulty | |
---|---|---|---|---|
46 | Permutations | 53.0% | Medium | |
48 | Rotate Image | 46.4% | Medium | |
49 | Group Anagrams | 44.3% | Medium | |
53 | Maximum Subarray | 42.6% | Easy | |
55 | Jump Game | 31.1% | Medium | |
56 | Merge Intervals | 34.6% | Medium | |
62 | Unique Paths | 46.1% | Medium | |
64 | Minimum Path Sum | 45.2% | Medium | |
70 | Climbing Stairs | 43.2% | Easy | |
72 | Edit Distance | 36.2% | Hard |
46. Permutations(Medium)
Given a collection of distinct integers, return all possible permutations.
Example:
Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
# my solution, Runtime: 48 ms, faster than 93.95% of Python3 , 回溯法
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
def dfs(nums,temp):
if len(temp)==n:
ans.append(temp)
temp=[]
for i in nums:
dfs(list(set(nums)-set(temp+[i])),temp+[i])
ans=[]
n=len(nums)
dfs(nums,[])
return ans
48. Rotate Image(Medium)
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Example:
# my solution, Runtime: 32 ms, faster than 99.57% of Python3
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
n=len(matrix)
temp=[[ -1 for j in range(n)] for i in range(n)] #不能用[[-1]*n]*n
for i in range(n):
for j in range(n):
temp[j][n-1-i]=matrix[i][j]
matrix[:]=temp[:]
# Runtime: 40 ms, faster than 81.85% of Python3
class Solution(object):
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
n = len(matrix)
for i in xrange(n):
for j in xrange(n):
if i < j:
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
for l in matrix:
l.reverse()
49. Group Anagrams(Medium)
Given an array of strings, group anagrams together.
Example:
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
Note:
All inputs will be in lowercase.
The order of your output does not matter.
# Approach 1: Categorize by Sorted String, T(n)=O(NKlogK), S(n)=O(NK)
# Runtime: 116 ms, faster than 82.68% of Python3
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
ans = {} # S(n)=O(NK)
for s in strs: # O(N)
if tuple(sorted(s)) in ans: # O(KlogK),list不能作为key,tuple可以
ans[tuple(sorted(s))].append(s)
else:
ans[tuple(sorted(s))]=[s]
return list(ans.values())
# Approach 2: Categorize by Count, T(n)=O(NK), S(n)=O(NK)
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
ans = {} # S(n)=O(NK)
for s in strs: # O(N)
count=[0]*26
for c in s:
count[ord(c)-ord('a')]+=1 # O(K)
if tuple(count) in ans:
ans[tuple(count)].append(s)
else:
ans[tuple(count)]=[s]
return list(ans.values())
53. Maximum Subarray(Easy)
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
网友评论