1.实现Tire(前缀树)
class Node :
def __init__ ( self) :
self. children = [ None ] * 26
self. isEnd = False
class Trie ( object ) :
def __init__ ( self) :
self. root = Node( )
def insert ( self, word) :
"""
:type word: str
:rtype: None
"""
node = self. root
for char in word:
index = ord ( char) - ord ( "a" )
if not node. children[ index] :
node. children[ index] = Node( )
node = node. children[ index]
node. isEnd = True
def search ( self, word) :
"""
:type word: str
:rtype: bool
"""
node = self. searchPrefix( word)
return node != None and node. isEnd == True
def startsWith ( self, prefix) :
"""
:type prefix: str
:rtype: bool
"""
node = self. searchPrefix( prefix)
return node != None
def searchPrefix ( self, word) :
node = self. root
for char in word:
node = node. children[ ord ( char) - ord ( "a" ) ]
if not node:
return None
return node
2.全排列——回溯 待 刷!!!
class Solution ( object ) :
def permute ( self, nums) :
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = [ ]
path = [ ]
used = [ 0 ] * len ( nums)
self. backtracking( nums, path, used, res)
return res
def backtracking ( self, nums, path, used, res) :
if len ( path) == len ( nums) :
res. append( path[ : ] )
return
for i in range ( len ( nums) ) :
if used[ i] == 1 :
continue
path. append( nums[ i] )
used[ i] = 1
self. backtracking( nums, path, used, res)
used[ i] = 0
path. pop( )
3.子集
代码链接 组合问题 时间复杂度: O(n * 2^n) 空间复杂度: O(n)
class Solution ( object ) :
def subsets ( self, nums) :
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = [ ]
path = [ ]
self. backtracking( nums, 0 , path, res)
return res
def backtracking ( self, nums, startIndex, path, res) :
res. append( path[ : ] )
if startIndex >= len ( nums) :
return
for i in range ( startIndex, len ( nums) ) :
path. append( nums[ i] )
self. backtracking( nums, i + 1 , path, res)
path. pop( )
4.电话号码的字母组合
class Solution ( object ) :
def __init__ ( self) :
self. res = [ ]
self. path = ""
self. letterMap = [
"" ,
"" ,
"abc" ,
"def" ,
"ghi" ,
"jkl" ,
"mno" ,
"pqrs" ,
"tuv" ,
"wxyz" ,
]
def trackbacking ( self, nums, startIndex) :
if startIndex == len ( nums) :
self. res. append( self. path)
return
dight = int ( nums[ startIndex] )
print ( dight)
letter = self. letterMap[ dight]
for i in letter:
self. path += i
self. trackbacking( nums, startIndex + 1 )
self. path = self. path[ : - 1 ]
def letterCombinations ( self, digits) :
"""
:type digits: str
:rtype: List[str]
"""
if not digits: return [ ]
self. trackbacking( digits, 0 )
return self. res
5.组合总和
class Solution ( object ) :
def combinationSum ( self, candidates, target) :
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
res = [ ]
path = [ ]
self. trackbacking( candidates, target, 0 , path, res)
return res
def trackbacking ( self, candidates, target, startIndex, path, res) :
if startIndex >= len ( candidates) :
return
if sum ( path) == target:
res. append( path[ : ] )
return
for i in range ( startIndex, len ( candidates) ) :
if sum ( path) > target:
return
path. append( candidates[ i] )
self. trackbacking( candidates, target, i, path, res)
path. pop( )
6.括号生成
class Solution ( object ) :
def generateParenthesis ( self, n) :
"""
:type n: int
:rtype: List[str]
"""
if n <= 0 :
return [ ]
res = [ ]
path = ""
def dfs ( path, left, right) :
if left > n or right > left:
return
if len ( path) == n * 2 :
res. append( path)
dfs( path + "(" , left + 1 , right)
dfs( path + ")" , left, right + 1 )
dfs( path, 0 , 0 )
return res
7.单词搜索
class Solution ( object ) :
def exist ( self, board, word) :
"""
:type board: List[List[str]]
:type word: str
:rtype: bool
"""
def dfs ( i, j, k) :
if not 0 <= i < len ( board) or not 0 <= j < len ( board[ 0 ] ) : return False
if board[ i] [ j] != word[ k] :
return False
if k == len ( word) - 1 :
return True
board[ i] [ j] = ''
res = dfs( i + 1 , j, k + 1 ) or dfs( i - 1 , j, k + 1 ) or dfs( i, j - 1 , k + 1 ) or dfs( i, j + 1 , k + 1 )
board[ i] [ j] = word[ k]
return res
for i in range ( len ( board) ) :
for j in range ( len ( board[ 0 ] ) ) :
if dfs( i, j, 0 ) :
return True
return False
8.分割回文串
class Solution ( object ) :
def partition ( self, s) :
"""
:type s: str
:rtype: List[List[str]]
"""
res = [ ]
path = [ ]
self. trackbacking( s, 0 , path, res)
return res
def trackbacking ( self, s, startIndex, path, res) :
if startIndex >= len ( s) :
res. append( path[ : ] )
return
for i in range ( startIndex, len ( s) ) :
if self. is_pali( s, startIndex, i) :
path. append( s[ startIndex : i + 1 ] )
self. trackbacking( s, i + 1 , path, res)
path. pop( )
else :
continue
def is_pali ( self, s, start, end) :
i = start
j = end
while i < j:
if s[ i] != s[ j] :
return False
i += 1
j -= 1
return True
9.N皇后
class Solution ( object ) :
def solveNQueens ( self, n) :
"""
:type n: int
:rtype: List[List[str]]
"""
res = [ ]
board = [ '.' * n for i in range ( n) ]
print ( board)
self. trackbacking( n, 0 , board, res)
return res
def trackbacking ( self, n, row, board, res) :
if row == n:
res. append( board[ : ] )
return
for col in range ( n) :
if self. isValid( row, col, board) :
board[ row] = board[ row] [ : col] + 'Q' + board[ row] [ col + 1 : ]
self. trackbacking( n, row + 1 , board, res)
board[ row] = board[ row] [ : col] + '.' + board[ row] [ col + 1 : ]
def isValid ( self, row, col, board) :
for i in range ( row) :
if board[ i] [ col] == "Q" :
return False
i, j = row - 1 , col - 1
while i >= 0 and j >= 0 :
if board[ i] [ j] == "Q" :
return False
i -= 1
j -= 1
i, j = row - 1 , col + 1
while i >= 0 and j < len ( board) :
if board[ i] [ j] == 'Q' :
return False
i -= 1
j += 1
return True
10.搜索插入位置
class Solution ( object ) :
def searchInsert ( self, nums, target) :
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
left, right = 0 , len ( nums) - 1
while left <= right:
mid = ( left + right) / 2
if nums[ mid] == target:
return mid
elif nums[ mid] > target:
right = mid - 1
else :
left = mid + 1
return left
11.搜索二维矩阵
class Solution ( object ) :
def searchMatrix ( self, matrix, target) :
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
row, col = len ( matrix) - 1 , len ( matrix[ 0 ] ) - 1
i, j = row, 0
while 0 <= i <= row and 0 <= j <= col:
if matrix[ i] [ j] == target:
return True
elif matrix[ i] [ j] > target:
i -= 1
elif matrix[ i] [ j] < target:
j += 1
return False
12.在排序数组中寻找第一个和最后一个位置
class Solution ( object ) :
def searchRange ( self, nums, target) :
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
left, right = 0 , len ( nums) - 1
ans = [ ]
while left <= right:
mid = ( left + right) / 2
if nums[ mid] == target:
left_index, right_index = mid, mid
while nums[ left_index] == target:
left_index -= 1
if left_index < 0 :
break
while nums[ right_index] == target :
right_index += 1
if right_index >= len ( nums) :
break
return [ left_index + 1 , right_index - 1 ]
elif nums[ mid] > target:
right = mid - 1
else :
left = mid + 1
return [ - 1 , - 1 ]
13.寻找旋转排序数组中的最小值
class Solution ( object ) :
def findMin ( self, nums) :
"""
:type nums: List[int]
:rtype: int
"""
left, right = 0 , len ( nums) - 1
if ( len ( nums) == 1 ) or ( nums[ left] < nums[ right] ) :
return nums[ left]
while left <= right:
mid = ( left + right) / 2
if nums[ 0 ] <= nums[ mid] :
left = mid + 1
elif nums[ mid] > nums[ mid - 1 ] :
right = mid - 1
else :
return nums[ mid]
return nums[ left]