给一个长度为 n 的数组,数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。
例如输入一个长度为9的数组[1,2,3,2,2,2,5,4,2]。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。
数据范围:,数组中元素的值
要求:空间复杂度:,时间复杂度
保证数组输入非空,且保证有解
[1,2,3,2,2,2,5,4,2]
2
[3,3,3,3,2,2,2]
3
[1]
1
class Solution: def MoreThanHalfNum_Solution(self , numbers: List[int]) -> int: count = 0 candidate = None for i in numbers: if count == 0: # 票为0,先换候选人 candidate = i count += 1 if i == candidate else -1 # 投给候选人,则票数+1,头给别人,票数-1 return candidate
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param numbers int整型一维数组 # @return int整型 # class Solution: def MoreThanHalfNum_Solution(self , numbers: List[int]) -> int: # write code here d=dict() for num in numbers: if not d.get(num): #target-numbers[i-1],则放入哈希表中 d[num]=1 else: d[num]+=1 d1=sorted(d.items(),key=lambda x:x[1]) return d1[-1][0]
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param numbers int整型一维数组 # @return int整型 # class Solution: def MoreThanHalfNum_Solution(self , numbers: List[int]) -> int: # write code here numbers.sort() return numbers[len(numbers)//2]
class Solution: def MoreThanHalfNum_Solution(self , numbers: List[int]) -> int: return sorted(numbers)[len(numbers) >> 1]2. 找众数,题目保证有解,所以众数一定是出现超过一半的数字。
class Solution: def MoreThanHalfNum_Solution(self , numbers: List[int]) -> int: preNum, count = 0, 0 for i in numbers: if count: count = count + 1 if preNum == i else count - 1 else: preNum, count = i, 1 return preNum
def MoreThanHalfNum_Solution(self , numbers: List[int]) -> int: # write code here has = {} res = [] for i in range(len(numbers)): if numbers[i] in has: has[numbers[i]] += 1 else: has[numbers[i]] = 1 if has[numbers[i]] > int(len(numbers)/2): res = numbers[i] return res
在保证一定有解的情况下,可以使用这种方法 时间复杂度O(n) 空间复杂度O(1)
class Solution: def MoreThanHalfNum_Solution(self , numbers: List[int]) -> int: temp = -1 cnt = 0 for num in numbers: if cnt == 0: temp = num cnt += 1 else: if num == temp: cnt += 1 else: cnt -= 1 return temp
哈希法
时间复杂度:O(n) 空间复杂度O(n)
class Solution: def MoreThanHalfNum_Solution(self , numbers: List[int]) -> int: n = len(numbers) mp = {} for num in numbers: if num not in mp: mp[num] = 1 else: mp[num] += 1 if mp[num] >= n / 2: return num
class Solution: def MoreThanHalfNum_Solution(self , numbers: List[int]) -> int: # write code here rating=0 for num in numbers: if rating==0: res=num if num == res: rating +=1 else: rating -=1 return res# 直观的话就是哈希表记录每个字符出现的次数 但是时间空间复杂度都是O(n)
public int MoreThanHalfNum_Solution(int [] array) {
int length=array.length;
if(array==null||length<=0){
return 0;
}
if(length==1){
return array[1];
}
int[] tempArray=new int[length];
for(int i=0;i<length;i++){
tempArray[i]=array[i];
}
for(int i=0;i<length;i++){
//后面需要用零来代表抹除数字,所以对0时做特殊处理
if(tempArray[i]==0){
continue;
}
for(int j=i+1;j<length;j++){
if(tempArray[i]!=tempArray[j]&&tempArray[j]!=0){
tempArray[i]=0;//此处用0代表抹去该数字
tempArray[j]=0;
break;
}
}
}
for(int i=0;i<length;i++){
System.out.println(tempArray[i]);
}
//找出未被抹去的数字
int result=0;
for(int i=0;i<length;i++){
if(tempArray[i]!=0){
result=tempArray[i];
break;
}
}
int times=0;
for(int i=0;i<length;i++){
if(result==array[i]){
times++;
}
}
if(times*2<length){
result=0;
}
return result;
}
}
public int MoreThanHalfNum_Solution(int [] array) {
int length=array.length;
if(array==null||length<=0){
return 0;
}
int result=array[0];
int times=1;
for(int i=1;i<length;i++){
if(times==0){
result=array[i];
times=1;
}else{
if(array[i]==result){
times++;
}else{
times--;
}
}
}
times=0;
for(int i=0;i<length;i++){
if(result==array[i]){
times++;
}
}
if(times*2<length){
result=0;
}
return result;
}
}