一个数组里有一个数重复了n/2多次,找到

思路:既然这个数重复了一半以上的长度,那么排序后,必然占据了 a[n/2]这个位置。

class Solution {
public:
int majorityElement(vector<int>& nums) {
sort(nums.begin(),nums.end());
return nums[nums.size()/2];
}
};

线性解法:投票算法,多的票抵消了其余人的票,那么我的票一定还有剩的。

int majority;
int cnt = 0;
for(int i=0; i<num.size(); i++){
if ( cnt ==0 ){
majority = num[i];
cnt++;
}else{
majority == num[i] ? cnt++ : cnt --;
if (cnt >= num.size()/2+1) return majority;
}
}
return majority;

  

Majority Element II:

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.

思路:

窗口(n/3)检查算法。先排序,然后用一个长度为n/3的窗口来检查两端的数是否相等。

class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
vector<int> res;
int maj_ele;
int len=nums.size();
sort(nums.begin(),nums.end());
int index=;
while(index<len)
{
maj_ele = nums[index];
if(maj_ele == nums[index+len/])
{
res.push_back(maj_ele);
while(index<len && maj_ele == nums[++index])
;
}
else
{ index++;
}
}
return res;
}
};

LeetCode(169)Majority Element and Majority Element II的更多相关文章

  1. Leetcode # 169, 229 Majority Element I and II

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  2. Leetcode之分治法专题-169. 求众数(Majority Element)

    Leetcode之分治法专题-169. 求众数(Majority Element) 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是 ...

  3. [LeetCode] 169. Majority Element 多数元素

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  4. 23. leetcode 169. Majority Element

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  5. LeetCode 169. Majority Element (众数)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  6. leetcode 169 Majority Element 冰山查询

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  7. LeetCode 169. Majority Element - majority vote algorithm (Java)

    1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...

  8. ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  9. LeetCode 169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

随机推荐

  1. java基础之 泛型

    泛型(Generic type 或者generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指定的类型的一个占位符,就像方法 ...

  2. C语言输出规定长度的整数,不够位数前面补零

    今天在做ACM题目的时候,遇到了这么一个问题,还真别说,这个以前真的没用过,当时就傻掉了,还好这个世界有Google,通过搜索了解了输出这种格式的C语言实现方法.但是没有找到C++的实现方法,希望知道 ...

  3. windows命令行及批处理文件小结

    1.命令Shell概述(Command shell overview): The command shell is a separate software program that provides ...

  4. Struts2 的ModelDriven理解

    以UserAction为例,当UserAction实现了ModelDriven接口之后,与该接口相关的默认配置的拦截器会在拦截请求之后判断该请求是将要被UserAction处理而且UserAction ...

  5. 创建MySQL数据库和表(一)

    一.启动MySQL服务 1.在Windows操作系统的“服务”中启动,找到你安装MySQL的起的服务名称,我本机服务名的是MySQL. 2.在命令行中用命令启动: A.启动MySQL服务:net st ...

  6. Mainstoryboard

    页面间进行跳转 [self performSegueWithIdentifier:@"signInSuccess" sender:self] signSuccess是miansto ...

  7. iOS App上架流程(2016详细版

    http://www.jianshu.com/p/b1b77d804254 iOS App上传项目遇到的问题 http://www.jianshu.com/p/9195cd991fc7

  8. 2016 - 1 - 19NSOpertation的依赖关系和监听

    一:NSOperation的依赖: 1.概念:队列中的A操作需要等其他B操作或者某些操作执行完毕后才执行,就叫做A依赖与B或者A依赖于其他某些操作. 2.注意点:不能循环依赖,否则卡死.如: [op2 ...

  9. python中的面向对象编程

    在python中几乎可以完成C++里所有面向对象编程的元素. 继承:python支持多继承: class Derived(base1, base2, base3): pass 多态:python中的所 ...

  10. MagSpoof:能预测并窃取你下一张信用卡号码的廉价设备

    想象一下,你丢失了信用卡,并从银行申请了一张新的信用卡.但是,如果在你收到这张新卡之前,一些网络罪犯就已经在使用你的新信用卡,此时你作何感想?是的,这完全是可以实现的,至少使用这个仅仅10美元的设备M ...