一个数组里有一个数重复了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. bitmapData

    一些常用接口: clone(): 得到位图数据的拷贝: 用途:深复制位图 draw(source :IBitmapDrawable...): source 要绘制到 BitmapData 对象的显示对 ...

  2. IntelliLock托管代码保护和许可授权管理系统软件详细介绍及下载

    IntelliLock是一个能用于控件与应用程序许可授权的100%托管的先进解决方案.与.NET Reactor提供的基于源代码保护的授权许可系统不同,IntelliLock选择了以100%托管的方式 ...

  3. Android 圆形ProgressBar风格设置

    Android系统自带的ProgressBar风格不是很好,如果想自己设置风格的话,一般有几种方法.首先介绍一下第一种方法通过动画实现.在res的anim下创建动画资源loading.xml: < ...

  4. 黑马程序员——C语言基础语法 关键字 标识符 注释 数据及数据类型

    Java培训.Android培训.iOS培训..Net培训.期待与您交流! (一下内容是对黑马苹果入学视频的个人知识点总结) (一)C语言简单介绍 (1)C语言程序是由函数组成的任何C语言程序都是由一 ...

  5. JavaScript中Date的一些细节

    对于开发人员来说,Date有时候或许会很重要,我们可以通过new Date()来创建一个日期对象.例如: var start = new Date(), //获取当前时间 today = new Da ...

  6. Xcode如何查看内存中的数据

    在  debug 模式下如何在断点处,查看字符指针变量内存中的值,像vs2008的调试工具一样的内存查看器,现在只能查看第一个内存中的值可以在输出窗口采用gdb命令:x /nfu <addr&g ...

  7. Wythoff's game

    这个问题就是OJ题里出现的取石子游戏,http://en.wikipedia.org/wiki/Wythoff%27s_game. 维基里面的通项公式并不适用于算法求解.需要理解下面两条规律: 1.A ...

  8. 安装VMware Tools找不到内核头文件

    http://blog.csdn.net/bobbat/article/details/38568885 安装VMware Tools,解决无法找到kernel header path的问题 安装 V ...

  9. Detail in Response.redirect and Server.transfer in ASP.NET

    http://www.developerfusion.com/article/4643/implementing-http-handlers-in-aspnet/4/

  10. ERP登录(八)

    登录的存储过程: ALTER PROCEDURE [dbo].[UserLogin] @userid int output, @LoginName nvarchar(50), @Password nv ...