字符串的排列
class Solution:
def Permutation(self, ss):
# write code here
if not ss:
return []
res = []
self.Perm(ss, res, '')
res = list(set(res))
return sorted(res)
def Perm(self, ss, res, path):
if ss == '':
res.append(path)
else:
for i in range(len(ss)):
self.Perm(ss[:i]+ss[i+1:], res, path+ss[i])
连续子数组的最大和
class Solution:
def FindGreatestSumOfSubArray(self, array):
# write code here
if len(array) == 0:
return []
if len(array) == 1:
return array
res = max(array)
temp = 0
for i in array:
temp = max(i, temp+i)
res = max(temp, res)
return res
整数中1出现的次数(从1到n整数中1出现的次数)
class Solution:
def NumberOf1Between1AndN_Solution(self, n):
# write code here
temp = n
base = 1
ones = 0
while temp:
last = temp%10
temp = temp/10
ones += temp*base
if last == 1:
ones += n%base +1
elif last > 1:
ones += base
base *= 10
return ones
把数组排成最小的数
class Solution:
def PrintMinNumber(self, numbers):
# write code here
if not numbers:
return ""
num=map(str,numbers)
num.sort(lambda x,y:cmp(x+y,y+x))
return ''.join(num)
丑数
class Solution:
def GetUglyNumber_Solution(self, index):
# write code here
if index < 0:
return None
if index == 0:
return 0
ugly_list = [1]
t2, t3, t5 = 0, 0, 0
for i in range(1,index):
ugly_list.append(min(ugly_list[t2]*2, ugly_list[t3]*3, ugly_list[t5]*5))
if ugly_list[i] == ugly_list[t2]*2:
t2 += 1
if ugly_list[i] == ugly_list[t3]*3:
t3 += 1
if ugly_list[i] == ugly_list[t5]*5:
t5 += 1
return ugly_list[index-1]
和为S的连续正数序列
class Solution:
def FindContinuousSequence(self, tsum):
# write code here
if tsum < 3:
return []
small = 1
big = 2
mid = (tsum + 1) >> 1
cursum = small + big
res = []
while small < mid:
if cursum == tsum:
res.append(range(small,big+1))
big += 1
cursum += big
elif cursum < tsum:
big += 1
cursum += big
else:
cursum -= small
small += 1
return res
和为S的两个数字
class Solution:
def FindNumbersWithSum(self, array, tsum):
# write code here
ls = []
if not isinstance(array, list):
return ls
for i, v in enumerate(array):
for v1 in array[i+1:]:
if (v + v1) == tsum:
ls.append([v, v1])
if ls:
return ls[0]
else:
return ls
孩子们的游戏(圆圈中最后剩下的数)
class Solution:
def LastRemaining_Solution(self, n, m):
# write code here
if not n or not m:
return -1
res = range(n)
i = 0
while len(res) > 1:
i = (m+i-1) % len(res)
res.pop(i)
return res[0]
构建乘积数组
class Solution:
def multiply(self, A):
# write code here
if not A:
return []
# 计算上三角
num = len(A)
B = [None]*num
B[0] = 1
for i in range(1,num):
B[i] = B[i-1] * A[i-1]
# 计算下三角
tmp = 1
for j in range(num-2, -1, -1):
tmp *= A[j+1]
B[j] *= tmp
return B
滑动窗口的最大值
class Solution:
def maxInWindows(self, num, size):
# write code here
length = len(num)
if length < size or size < 1:
return []
left = 0
right = size-1
max_arr = []
while right < length:
num_ = []
for i in num[left:right+1]:
num_.append(i)
max_arr.append(max(num_))
left += 1
right += 1
return max_arr
矩阵中的路径
class Solution:
def hasPath(self, matrix, rows, cols, path):
# write code here
if not matrix or rows < 1 or cols < 1 or not path:
return False
visited = [0]*(rows*cols)
path_len = 0
for row in range(rows):
for col in range(cols):
if self.hasPathCore(matrix,rows,cols,row,col,path,path_len,visited):
return True
return False
def hasPathCore(self, matrix, rows, cols, row, col, path, path_len, visited):
if len(path) == path_len:
return True
has_path = False
if 0<=row
visited[row*cols+col] = True
has_path = self.hasPathCore(matrix,rows,cols,row+1,col,path,path_len,visited)\
or self.hasPathCore(matrix,rows,cols,row-1,col,path,path_len,visited)\
or self.hasPathCore(matrix,rows,cols,row,col+1,path,path_len,visited)\
or self.hasPathCore(matrix,rows,cols,row,col-1,path,path_len,visited)
if not has_path:
path_len = 0
visited[row*cols+col] = False
return has_path
机器人的运动范围
class Solution:
def movingCount(self, threshold, rows, cols):
# write code here
arr = [[0 for i in range(cols)] for j in range(rows)]
global count
count = 0
def block(rows, cols):
s = sum(map(int, str(rows)+str(cols)))
return s > threshold
def travel(row, col):
global count
if not (0<=row
if arr[row][col] != 0:
return
if arr[row][col] == -1 or block(row, col):
arr[row][col] = -1
return
arr[row][col] = 1
count += 1
moves = [[0,1],[0,-1],[1,0],[-1,0]]
for move in moves:
travel(row+move[0], col+move[1])
travel(0, 0)
return count
剪绳子
class Solution:
def cutRope(self, number):
# write code here
if number == 2:
return 1
if number == 3:
return 2
count = number // 3
rem = number % 3
if rem == 0:
return pow(3, count)
elif rem == 1:
return pow(3, count-1) * 4
else:
return pow(3, count) * 2