一个数组里有一个数重复了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. linux exec用法总结

    Linux中exec的用法总结 先总结一个表: exec命令 作用 exec ls 在shell中执行ls,ls结果显示结束后不返回原来的的目录中,而是/(根目录) exec <file 将fi ...

  2. [pjsip]板砖理解pjsip体系结构

    在pjsip的官方开发向导中给出两张体系结构图,分别是消息流程图和类图,如下所示: 图1:消息流程图 图2:类图

  3. C# WebBrowser NativeMethods

    using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using Syste ...

  4. Lib New

    gacutil /if E:\ThirdParty\RockyLib\Rocky\bin\Release\Rocky.dll gacutil /u Rocky partial class GmailF ...

  5. Activity(活动)-再讲

    通过多天的学习,大家也了解了adb.exe 是用来进行 客户端(pc)-服务器端(android) 数据交互的. 用户可以使用工具Eclipse 中DDMS 隐示使用  adb.exe 进行连接,也可 ...

  6. Android开发-略讲adb命令和SQLite数据库运用

    adb.exe  ADB -Android Debug Bridge, 是 Android sdk 里的一个工具,用这个工具可以直接操作管理 Android 模拟器或者真实的 Android 设备 简 ...

  7. arguments.callee

    arguments.callee在哪个函数中运行,他就代表哪个函数,一般在匿名函数中.在匿名函数中有时需要自己调用自己,但是由于是匿名函数,没有名字,所以可以用arguments.callee来代替匿 ...

  8. poj2129 dp

    //Accepted 320 KB 47 ms //dp //dp[i][j]=1 表示用s1的前i个,s2的前j个字符能构成s3的前i+j-1个字符 //dp[i][j]=0 表示构不成 //dp[ ...

  9. UITableViewCell 自适应高度 ios8特性

    这篇文章介绍了在一个动态数据的 table view 中,cell 根据 text view 内容的输入实时改变 cell 和 table view 的高度.自动计算 cell 高度的功能使用 iOS ...

  10. 关于WinForm引用WPF窗体---在Winform窗体中使用WPF控件

    项目中有个界面展示用WPF实现起来比较简单,并且能提供更酷炫的效果,但是在WinForm中使用WPF窗体出现了问题,在网上找了一下有些人说Winform不能引用WPF的窗体,我就很纳闷,Win32都能 ...