07-斐波那契数列(python)
class Solution:
def Fibnacci(self,n):
tempArray=[0,1]
if n>=2:
for i in range(2,n+1):
tempArray[i%2]=tempArray[0]+tempArray[1]
return tempArray[n%2]
08-跳台阶(python)
class Solution:
def jumpFloor(self,n):
tempArray=[1,2]
if n>=3:
for i in range(3,n+1):
tempArray[(i+1)%2]=tempArray[0]+tempArray[1]
return tempArray[(n+1)%2]
08-变态跳台阶(python)
class Solution:
def jumpFloor2(self,n):
ans=1
if n>=2:
for i in range(n-1):
ans=ans*2
return ans
10-矩形覆盖(python)
class Solution:
def rectCover(self,n):
tempArray=[1,2]
if n>=3:
for i in range(3,n+1):
tempArray[(i+1)%2]=temArray[0]+tempArray[1]
return tempArray[(n+1)%2]
01-二维数组查找(python)
class Solution:
def Find(self,array):
if not array:
return False
row=len(array)
col=len(array)
i=0
j=col-1
while i<row and j >= 0:
if array[i][j]<target:
i+=1
elif array[i][j]>target:
j-=1
else:
return True
06-旋转数组的最小数字(python)
#二分查找
class Solution:
def minNumberInRotateArray(self,array):
if not array:
return 0
left=0
right=len(array)-1
while left<=right:
mid=(left+right)//2
if array[mid]>=array[left]:
left=mid
else:
right=mid
if array[mid]<array[mid-1]:
return array[mid]
37-数字在排序数组中出现次数(python)
class Solution:
def GetNumberOfk(self,data,k):
if not data:
return 0
return self.search(data,k+0.5)-self.search(data,k-0.5)
def search(self,data,k):
left=0
right=len(data)-1
while left<=right:
mid=(left+right)//2
if data[mid]>k:
right=mid-1
elif data[mid]<k:
low=mid+1
return high
27-字符串的排列(python)
class Solution:
def permutation(self,ss):
if not ss:return []
words=list(ss)
return sorted(set(self.helper(words)))
def helper(self,s):
if len(s)==1:
return s[0]
res=[]
for i in range(len(s)):
l=self.helper(s[:i]+s[i+1:])
for j in l:
res.append(s[i]+j)
return res
30-连续子数组的最大和(python)
#动态规划
class Solution:
def FindGreatestSumOfSubArray(self,array):
max=array[0]
dp=[0]*(len(array)+1)
dp[0]=array[0]
for i in range(1,len(array)):
if dp[i-1]<0:
dp=array[i]
else:
dp=dp[i-1]+array[i]
if dp[i]>max:
max=dp[i]
return max
52-正则表达式匹配(python)
class Solution:
# s, pattern都是字符串
def match(self, s, pattern):
if len(s) == 0 and len(pattern) == 0:
return True
if len(s) > 0 and len(pattern) == 0:
return False
# 如果模式第二个字符是*
if len(pattern) > 1 and pattern[1] == '*':
if len(s) > 0 and (s[0] == pattern[0] or pattern[0] == '.'):
# 如果第一个字符匹配,三种可能1、字符串移1位;2、字符串移1位,模式移2位;3、模式后移两位(这里将2、3合并书写)
return self.match(s, pattern[2:]) or self.match(s[1:], pattern)
else:
# 如果第一个字符不匹配,模式往后移2位,相当于忽略x*
return self.match(s, pattern[2:])
# 如果模式第二个字符不是*
if len(s) > 0 and (s[0] == pattern[0] or pattern[0] == '.'):
return self.match(s[1:], pattern[1:])
else:
return False
035-数组中的逆序对(python)
#归并排序
class Solution:
def InversePairs(self, data):
self.cnt=0
p = self.guibing(data)
return self.cnt%1000000007
def guibing(self,data):
global cnt
if len(data) == 1:
return data
mid = len(data)//2
left = self.guibing(data[:mid])
right = self.guibing(data[mid:])
i = 0
j = 0
res = []
while i < len(left) and j < len(right):
if left[i] < right[j]:
res.append(left[i])
i += 1
else:
res.append(right[j])
self.cnt += len(left) - i #计算逆序对的数量
j += 1
res += left[i:]
res += right[j:]
return res
011-二进制中1的个数(python)
class Solution:
def NumberOf1(self,n):
if n<0:n=n&0xffffffff
count=0
while n!=0:
count+=1
n=n&n-1
return count
40-数组中只出现一次的数字(python)
class Solution:
def FindNumsAppearOne(self,array):
map={}
res=[]
for i in array:
map[i]=map.get(i,0)+1
for i in array:
if map[i]==1:
res.append(i)
return res
013-调整数组使得奇数位于偶数前面(python)
class Solution:
def reOrderArray(self,array):
odd_cnt=0
res=[0]*len(array)
for i in array:
if i%2!=0:
odd_cnt+=1
odd_i=0
for i in range(len(array)):
if array[i]%2!=0:
res[odd_i]=array[i]
odd_i+=1
else:
res[odd_cnt]=array[i]
odd_cnt+=1
return res
028-数组中出现次数超过一半的数字(python)
class Solution:
def MoreThanHalfNum_Solution(self, numbers):
# write code here
map={}
for n in numbers:
map[n]=map.get(n,0)+1
if map[n]>len(numbers)/2:
return n
return 0
32-把数字排成最小的数(python)
class Solution:
def printMinNumber(self,numbers):
if not numbers:
return ''
str_num=[str(num) for num in numbers]
for i in range(len(numbers)-1):
for j in range(i+1,len(numbers)):
if str_num[i]+str_num[j]>str_num[j]+str_num[i]:
str_num[i],str_num[j]=str_num[j],str_num[i]
return ''.join(str_num)
041-和为s的连续正数序列(python)
class Solution:
def FindContinuousSequence(self, tsum):
res = []
windows = []
sum = 0
for t in range(1, tsum):
windows.append(t)
sum += t
while sum > tsum:
sum -= windows.pop(0)
if sum == tsum:
res.append(windows[:])
return res
42-和为s的两个数字(python)
#双指针
class Solution:
def FindNumbersWithSum(self,array,tsum):
if not array:return []
start=0
end=len(array)-1
while start<end:
csum=array[start]+array[end]
if csum<tsum:
start+=1
elif csum>tsum:
end-=1
else:
return [array[start],array[end]]
return []
43-左旋转字符串(python)
class Solution:
def leftRotateString(self,s,n):
if len(s)==0 or n==0:return s
s=list(s)
self.reverse(s,0,n)
self.reverse(s,n,len(s))
self.reverse(s,0,len(s))
return ''.join(s)
def reverse(self,s,left,right):
right-=1
while left<right:
s[left],s[right]=s[right],s[left]
left+=1
right-=1
51-构建乘积数组(python)
class Solution:
def multiply(self,A):
B=[1]*len(A)
temp=1
for i in range(1,len(A)):
temp*=A[i-1]
B[i]*=temp
temp=1
for i in range(len(A)-2,-1,-1):
temp*=A[i+1]
B[i]*=temp
return B