1.醜數
找2*2沒乘過的醜數、3*3沒乘過的醜數、5*5沒乘過的醜數,三者的最小值作爲新醜數
# -*- coding:utf-8 -*-
class Solution:
def GetUglyNumber_Solution(self, index):
# write code here
if index<7:return index
result=[1]
t2,t3,t5=0,0,0
for i in range(1,index):
result.append(result[t2]*2)
if result[i]>result[t3]*3:
result[i]=result[t3]*3
if result[i]>result[t5]*5:
result[i]=result[t5]*5
if result[i]==result[t2]*2:
t2+=1
if result[i]==result[t3]*3:
t3+=1
if result[i]==result[t5]*5:
t5+=1
return result[index-1]
2.第一個只出現一次的字符
python大法好
# -*- coding:utf-8 -*-
class Solution:
def FirstNotRepeatingChar(self, s):
# write code here
for i in range(0,len(s)):
if s[i] not in s[:i]+s[i+1:]:
return i
return -1
3.數組中的逆序對
嘗試寫歸併排序居然還是超時了,通過了75%的案例
class Solution:
def __init__(self):
self.count=0
def Merge(self,left,right):
i=0
j=0
result=[]
while(i<len(left) and j<len(right)):
if left[i]<=right[j]:
result.append(left[i])
i+=1
else:
result.append(right[j])
self.count+=len(left)-i
j+=1
result+=right[j:]
result+=left[i:]
return result
def clip(self,data):
print(data)
if len(data)>=2:
left=self.clip(data[:int(len(data)/2)])
right=self.clip(data[int(len(data)/2):])
return self.Merge(left,right)
else:
return data
def InversePairs(self, data):
# write code here
self.clip(data)
return self.count%1000000007
還有一個,參看這裏這裏
我試着寫了也沒通過= =?然後我看了python通過的代碼,是很奇怪的一行????
4.兩個鏈表的第一個公共結點
我還以爲公共結點的意思是隻有值相等,next是不一樣的
其實並不是,相遇之後尾部都一樣了
因此將兩個列表壓入兩個棧,之後同時pop一個,直到遇到不相等的,答案就是不相等的前一個
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def FindFirstCommonNode(self, pHead1, pHead2):
# write code here
t1=pHead1
t2=pHead2
l1=[]
l2=[]
while(t1 or t2):
if t1:
l1.append(t1)
t1=t1.next
if t2:
l2.append(t2)
t2=t2.next
result=None
while(l1 and l2):
t1=l1.pop()
t2=l2.pop()
if t1==t2:
result=t1
else:
break
return result
5.數字在排序數組中的次數
排序數組是啥意思= =升序還是降序
先寫個簡單的試試
# -*- coding:utf-8 -*-
class Solution:
def GetNumberOfK(self, data, k):
# write code here
count=0
flag=False
for i in data:
if k==i:
count+=1
flag=True
if k!=i and flag:
break
return count
過了= =。。。。。。
看了下討論區都是用二分法啊
我也寫個看看
6.二叉樹的深度
遞歸寫法
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def goTree(self,root,count):
if not root:
return count
count+=1
cl=count
cr=count
if root.left:
cl=self.goTree(root.left,count)
if root.right:
cr=self.goTree(root.right,count)
return cl if cl>cr else cr
def TreeDepth(self, pRoot):
# write code here
result=self.goTree(pRoot,0)
return result
7.平衡二叉樹
平衡二叉樹的特點就是左子樹右子樹高度不超過1
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def __init__(self):
self.flag=True
def getDepth(self,root):
if not root or not self.flag:
return 0
left=self.getDepth(root.left)
right=self.getDepth(root.right)
if abs(left-right)>1:
self.flag=False
return max(left,right)+1
def IsBalanced_Solution(self, pRoot):
# write code here
self.getDepth(pRoot)
return self.flag
8.數組中只出現一次的數字
利用字典的思路不寫了,python有個collections庫的Counter有個most_common()方法
# -*- coding:utf-8 -*-
class Solution:
# 返回[a,b] 其中ab是出現一次的兩個數字
def FindNumsAppearOnce(self, array):
# write code here
if not array:
return []
num=0
for i in array:
num^=i
#獲取低位(右邊數)1的位置
idx=0
while(num & 1)==0:
num>>=1
idx+=1
a,b=0,0
for i in array:
if (i>>idx)&1:
a^=i
else:
b^=i
return [a,b]