169.Majority Element
建立字典,统计数字数目,满足要求的输出。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution(object): 2 def majorityElement(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6 """ 7 n = len(nums) 8 if n == 0:return None 9 dict1 = {} 10 for i in range(len(nums)): 11 if nums[i] in dict1: 12 dict1[nums[i]] +=1 13 else: 14 dict1[nums[i]] = 1 15 for i,j in dict1.items(): 16 if j > n/2: 17 return i 18 return None
还有一种快速方法,先排序,返回nums[n/2]
Excel列名与数字对应关系,反向计算即可;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution(object): 2 def titleToNumber(self, s): 3 """ 4 :type s: str 5 :rtype: int 6 """ 7 flag = 0 8 result = 0 9 for i in s[::-1]: 10 temp = ord(i)-64 11 result = result + temp*26**flag 12 flag +=1 13 return result
172. Factorial Trailing Zeroes
关键在于数列中存在多少个5就等于多少个0
0的个数等于 n/5+n/5/5+n/5/5/5...直到n/5等于0
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution(object): 2 def trailingZeroes(self, n): 3 """ 4 :type n: int 5 :rtype: int 6 """ 7 result = 0 8 while n>0: 9 n = n/5 10 result +=n 11 return result
189.Rotate Array
旋转列表n步
可用pop和insert组合
可用切片,然后相加赋值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution(object): 2 def rotate(self, nums, k): 3 """ 4 :type nums: List[int] 5 :type k: int 6 :rtype: void Do not return anything, modify nums in-place instead. 7 """ 8 """ 9 k = k%len(nums) 10 for i in range(k): 11 temp = nums.pop() 12 nums.insert(0,temp) 13 """ 14 k = k%len(nums) 15 nums[:] = nums[-k:] + nums[:-k]