一道题目的状态分为三种:
[未掌握],[已掌握],[已巩固]
第一次无法AC的题目,标记为未掌握。
第二天可以默写代码的题目,改标志为已掌握。
一个星期复习时依然可以写出代码的题目,标记为已巩固。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31. Next Permutation [未掌握]
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.1,2,3
→ 1,3,2
3,2,1
→ 1,2,3
1,1,5
→ 1,5,1
Subscribe to see which companies asked this question
题意:
求一个序列的下一个排列。
分析:
可以用 STL 里的 ‘next_permutation’ 偷懒。
具体算法是:
首先,从最尾端开始往前寻找两个相邻的元素,令第一个元素是 i,第二个元素是 ii,且满足 i
然后,将 i 和 j 对调,再将 ii 及其后面的所有元素反转。
代码:
class Solution(object):
def nextPermutation(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
if len(nums) <= 1:
return
for i in xrange(len(nums) - 2, -1, -1):
if nums[i] < nums[i + 1]:
break
else:
i = -1
if i >= 0:
for j in range(len(nums) - 1, i - 1, -1):
if nums[j] > nums[i]:
nums[i], nums[j] = nums[j], nums[i]
break
left, right = i + 1, len(nums) - 1
while left <= right:
print left, right
nums[left], nums[right] = nums[right], nums[left]
left += 1
right -= 1
代码比较巧妙的思路是-1的设置。当不存在i +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 51. N-Queens
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of the n-queens' placement, where For example, Subscribe to see which companies asked this question 题目:点击打开链接 题意:给一个n*n的棋盘,n个皇后不能在同一行、同一列、同一条斜线。问有几种摆法 思路:这个解法类似于深度优先搜索,使用board表示棋盘很有技巧:比如board=[1, 3, 0, 2],这是4皇后问题的一个解,意思是:在第0行,皇后放在第1列;在第1行,皇后放在第3列;在第2行,皇后放在第0列;在第3行,皇后放在第2列。 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 52. N-Queens II Follow up for N-Queens problem. 题目:点击打开链接 题意:给一个n*n的棋盘,n个皇后不能在同一行、同一列、同一条斜线。问有几种摆法,返回数字 思路:与上道题思路一致,不过将三个函数分别作为类的成员函数来写了。上道题成员函数套函数的写法,命名空间有一点问题。
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 53. Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array click to show more practice. Subscribe to see which companies asked this question 题意:给一个数组,找到其中和最大的子序列 思路:这道题的难点在于,数组中的大的负数应该怎么处理。比如[1, 2, 3, -100, 2],应该返回6;[1, 2, 3, -100, 20],应该返回20。这个求解思路还是很巧妙的,ThisSum用来指示当前子序列的最大部分和,MaxSum用来指示曾经出现过的子序列最大部分和。
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 54. Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, You should return Subscribe to see which companies asked this question 题意:给一个n*n矩阵,要求按照一个圈遍历元素 思路:就是从外到内一圈一圈读取就可以了
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 55. Jump Game Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example: A = Subscribe to see which companies asked this question 题意:给一组数,每个数字代表这个格子可以往后跳的最大步数,检测这组数能否跳到末尾 思路:推荐这篇文章,是leetcode的article:点击打开链接 从后向前检测,如果一个格子可以跳到末尾(注意i + nums[i] >= last,>=即可),则问题变为从前面的格子能否跳到这一个格子。 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 56. Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example, Subscribe to see which companies asked this question 题意:给一堆区间,合并重叠部分 思路: 先按照start排序 建立一个结果数组,如果上一个元素的end小于当前元素的start,插入当前元素。 否则合并result最后一个元素,和当前元素的end(取max) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 57. Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Example 2: This is because the new interval Subscribe to see which companies asked this question 1、新建数组存储区间,不删除原数组元素。 2、不符合合并条件的区间段直接入result 3、记录插入合并段的位置 4、遇到要合并的段,合并到newInterval一起插入 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 58. Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space characters If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space characters only. For example, Subscribe to see which companies asked this question 题意:输出最后一个单词长度 思路:无 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 59. Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Subscribe to see which companies asked this question 题目链接:点击打开链接 题意:给出n,输出一个数字转圈排列的N*N矩阵 思路:一层一层,由外圈向内圈填数字,迭代递归都可以 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 60. Permutation Sequence
The set By listing and labeling all of the permutations in order, Given n and k, return the kth permutation sequence. Note: Given n will be between 1 and 9 inclusive. Subscribe to see which companies asked this question 题意:寻找n个数字全排列中的第k个数字 思路:以n=4,k=3为例,第一个数字出现范围为第一个到第六个,是3!次,根据这个规律求出第一个数字为(3-1)/6 = 0,即数字1,,删掉数字1. 令k=k%6,k=3,第二个数字出现的范围为第一个到第二个,是2!次,(3-1)/2 = 1,nums[1] = 3,第二个数字是3,删掉3 k=k%2,k=1,第三个数字是1!个,(1-1)/1 = 0,数字为2 剩下的是4,加入其中
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 61. Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Subscribe to see which companies asked this question 要点:链表首尾相连,然后找第length - k % length个,作为新的head即可。 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 62. Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below). How many possible unique paths are there? Above is a 3 x 7 grid. How many possible unique paths are there? Note: m and n will be at most 100. Subscribe to see which companies asked this question 题意:m*n的格子,从左上角到右下角有多少种走法。 思路:二维DP,这里用hashtable做了,还可以改进。 63. Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as For example, There is one obstacle in the middle of a 3x3 grid as illustrated below. The total number of unique paths is Note: m and n will be at most 100. Subscribe to see which companies asked this question 题意:格子中存在障碍,有多少个走法 思路:在找路时,加入右方格子和下方格子是否是障碍的判断 64. Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. Subscribe to see which companies asked this question 题意:m*n格子,找走过格子数字加起来最小的路径的数字和。(改成返回路径怎么做) 思路:还是二维dp +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 69. Sqrt(x) Implement Compute and return the square root of x. Subscribe to see which companies asked this question 题意:返回平方后比x小的最大的整数 要点:注意比x小 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 70. Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Subscribe to see which companies asked this question 递归解法:
其实看透这个问题的本质是一个斐波那契数列,一切就简单了。 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example, click to show corner cases. Subscribe to see which companies asked this question ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 72. Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a character Subscribe to see which companies asked this question 题意:求编辑距离,2维度DP 注意事项: 1、递归解法栈深度超限 2、python二维数组赋初值不能self.cache = [[0] * (len(word2) + 1)] * (len(word1) + 1) 应该self.cache = [[0 for col in range(len(word2) + 1)] for row in range(len(word1) + 1)] 详细的思路在点击打开链接 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 73. Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Subscribe to see which companies asked this question 题目链接:点击打开链接 题意:输入一个矩阵,如果一行一列中有一个元素为0,那么这行这列都标记为0 实际编写过程中,可将每行每列设置flag,不需要重复的将一行/一列多次置0。 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: For example, Consider the following matrix: Given target = Subscribe to see which companies asked this question Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. Note: click to show follow up. Subscribe to see which companies asked this question Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example, Subscribe to see which companies asked this question 题目链接:点击打开链接 题意:是一个组合问题,返回所有可能的组合(排序后) 思路:类似于DFS,最小的组合是[1,2,3,...,k],最大的组合是[n-k+1,n-k+2,...,n],需要做的是从后向前遍历,把还可以加的位置加一位,然后把之后的元素设置成+1,+2,+3...等,append到返回中。 Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not contain duplicate subsets. For example, Subscribe to see which companies asked this question Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once. For example, Subscribe to see which companies asked this question Follow up for "Remove Duplicates": For example, Your function should return length = Subscribe to see which companies asked this question Follow up for "Search in Rotated Sorted Array": Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. Subscribe to see which companies asked this question Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Subscribe to see which companies asked this question Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Subscribe to see which companies asked this question Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example, Subscribe to see which companies asked this question'Q'
and '.'
both indicate a queen and an empty space respectively.
There exist two distinct solutions to the 4-queens puzzle:[
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."],
["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]
通过深度优先遍历,找到所有可能答案。class Solution(object):
def solveNQueens(self, n):
"""
:type n: int
:rtype: List[List[str]]
"""
def check(k, j): # check if the kth queen can be put in column j!
for i in range(k):
if board[i]==j or abs(k-i)==abs(board[i]-j):
return False
return True
def dfs(depth, valuelist):
if depth==n: res.append(valuelist); return
for i in range(n):
if check(depth,i):
board[depth]=i
s='.'*n
dfs(depth+1, valuelist+[s[:i]+'Q'+s[i+1:]])
board=[-1 for i in range(n)]
res=[]
dfs(0,[])
return res
Now, instead outputting board configurations, return the total number of distinct solutions.
Subscribe to see which companies asked this questionclass Solution(object):
def check(self, k, j): # check if the kth queen can be put in column j!
for i in range(k):
if self.board[i]==j or abs(k-i)==abs(self.board[i]-j):
return False
return True
def dfs(self, depth):
if depth==self.n: self.res += 1; return
for i in range(self.n):
if self.check(depth,i):
self.board[depth]=i
self.dfs(depth+1)
def totalNQueens(self, n):
"""
:type n: int
:rtype: int
"""
self.res = 0
self.n = n
self.board=[-1 for i in range(n)]
self.dfs(0)
return self.res
[-2,1,-3,4,-1,2,1,-5,4]
,
the contiguous subarray [4,-1,2,1]
has the largest sum = 6
.
对于ThisSum而言,有一个很巧妙的等价条件:如果当前子序列的ThisSum < 0,那么应该放弃掉这个子序列,把ThisSum设为0,从下一个元素开始继续探测。
这个条件的正确性也是显而易见的,当一个子序列的和<0时,在它的后面加入任何数字,所产生的部分和一定比这个数字单独构成的子序列要小。
另外More practice中提到的分治算法,我没有想明白。难道以上算法就算是分治了么?还需要探究一下。class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
ThisSum = 0
MaxSum = -10000
for i in range(0, len(nums)):
if ThisSum < 0:
ThisSum = 0
ThisSum = ThisSum + nums[i]
MaxSum = max(ThisSum, MaxSum)
return MaxSum
Given the following matrix:[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
[1,2,3,6,9,8,7,4,5]
.#我的
class Solution(object):
def handleMatrix(self, matrix,istart,jstart):
if len(self.result) == self.i_len * self.j_len:
return
for j in range(jstart, self.j_len-jstart):
self.result.append(matrix[istart][j])
if len(self.result) == self.i_len * self.j_len:
return
for i in range(istart + 1, self.i_len-istart):
self.result.append(matrix[i][self.j_len - jstart - 1])
if len(self.result) == self.i_len * self.j_len:
return
for j in range(jstart, self.j_len - jstart - 1)[::-1]:
self.result.append(matrix[self.i_len-istart-1][j])
if len(self.result) == self.i_len * self.j_len:
return
for i in range(istart + 1, self.i_len - istart - 1)[::-1]:
self.result.append(matrix[i][jstart])
self.handleMatrix(matrix,istart + 1,jstart + 1)
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
self.result = []
self.i_len = len(matrix)
if self.i_len > 0:
self.j_len = len(matrix[0])
else:
self.j_len = 0
self.handleMatrix(matrix,0,0)
return self.result
#九章
class Solution:
# @param {int[][]} matrix a matrix of m x n elements
# @return {int[]} an integer array
def spiralOrder(self, matrix):
# Write your code here
if matrix == []: return []
up = 0; left = 0
down = len(matrix)-1
right = len(matrix[0])-1
direct = 0 # 0: go right 1: go down 2: go left 3: go up
res = []
while True:
if direct == 0:
for i in range(left, right+1):
res.append(matrix[up][i])
up += 1
if direct == 1:
for i in range(up, down+1):
res.append(matrix[i][right])
right -= 1
if direct == 2:
for i in range(right, left-1, -1):
res.append(matrix[down][i])
down -= 1
if direct == 3:
for i in range(down, up-1, -1):
res.append(matrix[i][left])
left += 1
if up > down or left > right: return res
direct = (direct+1) % 4
A = [2,3,1,1,4]
, return true
.[3,2,1,0,4]
, return false
.class Solution(object):
def canJump(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
last = len(nums) - 1
for i in range(len(nums))[::-1]:
if i + nums[i] >= last:
last = i
return last == 0
Given [1,3],[2,6],[8,10],[15,18]
,
return [1,6],[8,10],[15,18]
."""
Definition of Interval.
class Interval(object):
def __init__(self, start, end):
self.start = start
self.end = end
"""
class Solution:
# @param intervals, a list of Interval
# @return a list of Interval
def merge(self, intervals):
intervals = sorted(intervals, key=lambda x: x.start)
result = []
for interval in intervals:
if len(result) == 0 or result[-1].end < interval.start:
result.append(interval)
else:
result[-1].end = max(result[-1].end, interval.end)
return result
Given intervals [1,3],[6,9]
, insert and merge [2,5]
in as [1,5],[6,9]
.
Given [1,2],[3,5],[6,7],[8,10],[12,16]
, insert and merge [4,9]
in as [1,2],[3,10],[12,16]
.[4,9]
overlaps with [3,5],[6,7],[8,10]
.# Definition for an interval.
# class Interval(object):
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e
class Solution(object):
def insert(self, intervals, newInterval):
"""
:type intervals: List[Interval]
:type newInterval: Interval
:rtype: List[Interval]
"""
results = []
insertPos = 0
for interval in intervals:
if interval.end < newInterval.start:
results.append(interval)
insertPos += 1
elif interval.start > newInterval.end:
results.append(interval)
else:
newInterval.start = min(interval.start, newInterval.start)
newInterval.end = max(interval.end, newInterval.end)
results.insert(insertPos, newInterval)
return results
要点:
' '
, return the length of last word in the string.
Given s = "Hello World"
,
return 5
.class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
return len(s.strip().split(' ')[-1])
Given n = 3
,[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
class Solution(object):
def generateMatrix(self, n):
"""
:type n: int
:rtype: List[List[int]]
"""
self.result = []
for i in range(n):
self.result.append([0]*n)
self.handleMatrix(0,1,n)
return self.result
def handleMatrix(self, start,startnum,n):
if startnum > n**2:
return
for j in range(start, n-start):
self.result[start][j] = startnum
startnum += 1
for i in range(start + 1, n-start):
self.result[i][n - start - 1] = startnum
startnum += 1
for j in range(start, n - start - 1)[::-1]:
self.result[n-start-1][j] = startnum
startnum += 1
for i in range(start + 1, n - start - 1)[::-1]:
self.result[i][start] = startnum
startnum += 1
self.handleMatrix(start + 1,startnum,n)
[1,2,3,…,n]
contains a total of n! unique permutations.
We get the following sequence (ie, for n = 3):
"123"
"132"
"213"
"231"
"312"
"321"
class Solution:
"""
@param n: n
@param k: the k-th permutation
@return: the k-th permutation
"""
def getPermutation(self, n, k):
fac = [1]
for i in range(1, n + 1):
fac.append(fac[-1] * i)
elegible = range(1, n + 1)
per = []
for i in range(n):
digit = (k - 1) / fac[n - i - 1]
per.append(elegible[digit])
elegible.remove(elegible[digit])
k = (k - 1) % fac[n - i - 1] + 1
return "".join([str(x) for x in per])
Given 1->2->3->4->5->NULL
and k = 2
,
return 4->5->1->2->3->NULL
.# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def rotateRight(self, head, k):
"""
:type head: ListNode
:type k: int
:rtype: ListNode
"""
if k == 0 or head == None or head.next == None:
return head
length = 1
cur = head
while cur.next:
cur = cur.next
length += 1
cur.next = head
for i in range(length - k % length):
cur = cur.next
head = cur.next
cur.next = None
return head
class Solution(object):
def findPath(self, m, n):
if (m,n) in self.cache:
return self.cache[(m,n)]
m_step, n_step =0,0
if m > 0:
m_step = self.findPath(m - 1, n)
if n > 0:
n_step = self.findPath(m, n - 1)
self.cache[(m,n)] = m_step + n_step
return self.cache[(m,n)]
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
self.cache = {(0, 0): 1}
return self.findPath(m - 1, n - 1)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1
and 0
respectively in the grid.[
[0,0,0],
[0,1,0],
[0,0,0]
]
2
.class Solution(object):
def findPath(self, m, n):
if (m,n) in self.cache:
return self.cache[(m,n)]
m_step, n_step =0,0
if m < self.r_len and self.obstacleGrid[m + 1][n] == 0:
m_step = self.findPath(m + 1, n)
if n < self.c_len and self.obstacleGrid[m][n + 1] == 0:
n_step = self.findPath(m, n + 1)
self.cache[(m,n)] = m_step + n_step
return self.cache[(m,n)]
def uniquePathsWithObstacles(self, obstacleGrid):
"""
:type obstacleGrid: List[List[int]]
:rtype: int
"""
self.obstacleGrid = obstacleGrid
self.r_len = len(obstacleGrid) - 1
self.c_len = len(obstacleGrid[0]) - 1
self.cache = {(self.r_len, self.c_len): 1}
#起点或终点有阻碍则返回0
if obstacleGrid[self.r_len][self.c_len] or obstacleGrid[0][0]:
return 0
return self.findPath(0, 0)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class Solution(object):
def findPath(self, m, n):
if (m,n) in self.cache:
return self.cache[(m,n)]
temp = 99999
if m < self.r:
temp = self.grid[m][n] + self.findPath(m + 1, n)
if n < self.c:
temp = min(self.grid[m][n] + self.findPath(m, n + 1),temp)
self.cache[(m,n)] = temp
return self.cache[(m,n)]
def minPathSum(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
self.grid = grid
self.r = len(grid) - 1
self.c = len(grid[0]) - 1
self.cache = {(self.r, self.c): grid[self.r][self.c]}
return self.findPath(0, 0)
int sqrt(int x)
.class Solution(object):
def mySqrt(self, x):
"""
:type x: int
:rtype: int
"""
left = 0
right = x
mid = 0
while left <= right and left >= 0 and right >= 0:
mid = (left + right) / 2
if mid**2 > x:
right = mid - 1
else:
left = mid + 1
if mid ** 2 > x:
return mid - 1
else:
return mid
class Solution(object):
cache = {1:1, 2:2}
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
if n in self.cache:
return self.cache[n]
else:
temp = self.climbStairs(n - 1) + self.climbStairs(n - 2)
self.cache[n] = temp
return temp
迭代解法:
class Solution:
"""
@param n: a integer
@return: a integer
"""
def climbStairs(self, n):
# write your code here
if n <= 2:
return n
result=[1,2]
for i in range(n-2):
result.append(result[-2]+result[-1])
return result[-1]
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
class Solution(object):
def simplifyPath(self, path):
"""
:type path: str
:rtype: str
"""
temp = []
path = path.split('/')
for p in path:
if p == '.' or p == '':
pass
elif p == '..':
if len(temp) > 0:
temp.pop()
else:
temp.append(p)
return '/' + '/'.join(temp)
b) Delete a character
c) Replace a characterclass Solution(object):
def handleDistance(self, word1, word2, i, j):
if i == 0 and j == 0:
return 0
elif i == 0 and j != 0:
return j
elif i != 0 and j == 0:
return i
else:
return min(
self.cache[i - 1][j - 1] + (1 if word1[i - 1] != word2[j - 1] else 0),
self.cache[i][j - 1] + 1,
self.cache[i - 1][j] + 1
)
def minDistance(self, word1, word2):
"""
:type word1: str
:type word2: str
:rtype: int
"""
self.cache = [[0 for col in range(len(word2) + 1)] for row in range(len(word1) + 1)]
for i in range(len(word1) + 1):
for j in range(len(word2) + 1):
self.cache[i][j] = self.handleDistance(word1, word2, i, j)
if i == len(word1) and j == len(word2):
return self.cache[i][j]
class Solution:
"""
@param matrix: A list of lists of integers
@return: Nothing
"""
def setZeroes(self, matrix):
# write your code here
if len(matrix)==0:
return
rownum = len(matrix)
colnum = len(matrix[0])
row = [False for i in range(rownum)]
col = [False for i in range(colnum)]
for i in range(rownum):
for j in range(colnum):
if matrix[i][j] == 0:
row[i] = True
col[j] = True
for i in range(rownum):
for j in range(colnum):
if row[i] or col[j]:
matrix[i][j] = 0
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
3
, return true
.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import bisect
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
low = 0
high = len(matrix) - 1
while low <= high:
mid = (low + high) // 2
midVal = matrix[mid][0]
if mid == high and midVal < target:
index = bisect.bisect_left(matrix[mid], target)
if index < len(matrix[mid]) and matrix[mid][index] == target:
return True
else:
return False
elif midVal < target and matrix[mid + 1][0] > target:
index = bisect.bisect_left(matrix[mid], target)
if index < len(matrix[mid]) and matrix[mid][index] == target:
return True
else:
return False
elif midVal < target:
low = mid + 1
elif midVal > target:
high = mid - 1
else:
return True
return False
You are not suppose to use the library's sort function for this problem.
题意:三种颜色使用0,1,2表示,对这个数组排序
class Solution(object):
def sortColors(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
color = [0] * 4
for n in nums:
color[n + 1] += 1
for i in range(len(color))[1:]:
color[i] += color[i - 1]
for i in range(len(color))[:-1]:
for j in range(color[i], color[i + 1]):
nums[j] = i
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
If n = 4 and k = 2, a solution is:[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
class Solution(object):
def combine(self, n, k):
"""
:type n: int
:type k: int
:rtype: List[List[int]]
"""
result = []
if n == 0 or k == 0:
return result
now = range(1, k + 1)
modify = True
while modify:
result.append(now[:])
modify = False
for i in range(k)[::-1]:
if now[i] < (n if i == k - 1 else now[i + 1] - 1):
now[i] += 1
modify = True
if i != k - 1:
for j in range(i + 1, k):
now[j] = now[j - 1] + 1
break
return result
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
If nums = [1,2,3]
, a solution is:[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
class Solution(object):
def combine(self, n, k):
result = []
now = range(0, k)
modify = True
while modify:
result.append(now[:])
modify = False
for i in range(k)[::-1]:
if now[i] < (n - 1 if i == k - 1 else now[i + 1] - 1):
now[i] += 1
modify = True
if i != k - 1:
for j in range(i + 1, k):
now[j] = now[j - 1] + 1
break
return result
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = []
for k in range(len(nums) + 1):
result.extend(
[[nums[x] for x in l] for l in self.combine(len(nums), k)]
)
return result
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Given board =[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
word
=
"ABCCED"
, -> returns
true
,
word
=
"SEE"
, -> returns
true
,
word
=
"ABCB"
, -> returns
false
.
class Solution(object):
def isMatch(self, i, j, k):
if i < 0 or i >= self.row_len \
or j < 0 or j >= self.col_len:
return False
else:
if self.board[i][j] == self.word[k]:
if k + 1 == self.word_len:
return True
else:
temp = self.board[i][j]
self.board[i][j] = '*'
exist = self.isMatch(i - 1, j, k + 1) or \
self.isMatch(i, j - 1, k + 1) or \
self.isMatch(i + 1, j, k + 1) or \
self.isMatch(i, j + 1, k + 1)
self.board[i][j] = temp
return exist
else:
return False
def exist(self, board, word):
"""
:type board: List[List[str]]
:type word: str
:rtype: bool
"""
self.board = board
self.word = word
self.row_len = len(board)
self.col_len = len(board[0]) if self.row_len > 0 else 0
self.word_len = len(word)
if self.word_len == 0:
return True
for i in range(self.row_len):
for j in range(self.col_len):
if self.isMatch(i, j, 0):
return True
return False
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
What if duplicates are allowed at most twice?
Given sorted array nums = [1,1,1,2,2,3]
,5
, with the first five elements of nums being 1
, 1
, 2
, 2
and 3
. It doesn't matter what you leave beyond the new length.class Solution(object):
def removeDuplicates(self, nums):
i = 0
for n in nums:
if i < 2 or n > nums[i-2]:
nums[i] = n
i += 1
return i
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
cache = {}
for i in range(len(nums))[::-1]:
if nums[i] in cache:
if cache[nums[i]] == 2:
del nums[i]
else:
cache[nums[i]] += 1
else:
cache[nums[i]] = 1
return len(nums)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
What if duplicates are allowed?class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: bool
"""
size = len(nums)
first = 0;last = size - 1
while first <= last:
mid = (first + last) >> 1
if nums[mid] == target:
return True
if nums[first] == nums[mid] and nums[mid] == nums[last]:
first += 1
last -= 1
elif nums[first] <= nums[mid]:
if nums[first] <= target and target < nums[mid]:
last = mid - 1
else:
first = mid + 1
else:
if nums[mid] < target and target <= nums[last]:
first = mid + 1
else:
last = mid - 1
return False
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Given 1->2->3->3->4->4->5
, return 1->2->5
.
Given 1->1->1->2->3
, return 2->3
.# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return head
fakehead = ListNode(-1)
fakehead.next = head
pre = fakehead
cur = head
while cur:
while cur.next and cur.val == cur.next.val:
cur = cur.next
if pre.next == cur:
pre = pre.next
else:
pre.next = cur.next
cur = cur.next
return fakehead.next
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return head
fakehead = ListNode(-1)
fakehead.next = head
pre = fakehead
cur = head
while cur:
while cur.next and cur.val == cur.next.val:
cur = cur.next
if pre.next == cur:
pre = pre.next
cur = cur.next
else:
pre.next = cur
return fakehead.next
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Given 1->4->3->2->5->2
and x = 3,
return 1->2->2->4->3->5
.# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def partition(self, head, x):
"""
:type head: ListNode
:type x: int
:rtype: ListNode
"""
cur = head
small = None
big = None
while cur:
if cur.val < x:
if not small:
small = cur
cur_s = small
else:
cur_s.next = cur
cur_s = cur_s.next
else:
if not big:
big = cur
cur_b = big
else:
cur_b.next = cur
cur_b = cur_b.next
cur = cur.next
if small:
cur_s.next = big
else:
small = big
if big:
cur_b.next = None
return small
ListNode *partition(ListNode *head, int x) {
ListNode node1(0), node2(0);
ListNode *p1 = &node1, *p2 = &node2;
while (head) {
if (head->val < x)
p1 = p1->next = head;
else
p2 = p2->next = head;
head = head->next;
}
p2->next = NULL;
p1->next = node2.next;
return node1.next;
}