某陸生發瘋 說他願意跟我換功力
我在此宣布:
我願意拿我的 所有 物理數學功力 大學碩士加海外 22x學分
十幾門數學系課程
十幾張線上課程證書 gpa 4.18/4.17/4.0
所有國小國中高中大學碩士獎狀
所有publication
我其他賺小錢的副業 Youtube頻道 部落格廣告 跟他換
他們國內 有車有房 爸媽介紹女生 讀書能發光 讀書有人愛 能安穩沒有敵人
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋
times.
You may assume that the array is non-empty and the majority element always exist in the array.
Example 1:
Input: [3,2,3] Output: 3
Example 2:
Input: [2,2,1,1,1,2,2]
解: 逐步檢查: 誰一超過一半 就馬上輸出
這樣也滿快的
class Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :rtype: int """ m=len(nums) l={} for i in nums: if i in l: l[i]+=1 else: l[i]=1 if l[i]>m//2:
MOore 投票法:
速度上更快一些 O(n) linear time and O(1) space
class Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :rtype: int """ count=1 num=nums[0] for i in nums: if i==num: count+=1 if i!=num: count=count-1 if count==0: num=i count=1 return num return i
Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2] Output: [2,2]
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] Output: [4,9]
Note:
Follow up: