在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素。
容易想到的方式就是计数,出现次数最多的就是majority element,其次就是排序,中间的就是majority element。
但是还有两种更有意思的实现方式时间效率O(n),空间效率O(1):
1、Moore voting algorithm 投票算法,因为符合要求的majority element总是存在的,所以首先置计数器count=1,并选择数组的第一个元素作为candidate,往后遍历并计数,与candidate相同则count++,不同则count--,当count=0则选择数组中的下一个数作为candidate,遍历结束candidate则为majority element。

public int majorityElement(int[] num) {
int count = 1;
int candidate = num[0];
for (int i = 1; i < num.length; i++) {
if (count == 0) {
candidate = num[i];
}
if (num[i] == candidate) {
count++;
} else {
count--;
}
}
return candidate;
}

2、这种方式挺有意思。因为majority element出现的次数大于 ⌊ n/2 ⌋ 次,而题目中给的是int类型数组,对于每一个数字的32位,majority element的每一位是相同的,所以不管这一位是1或0,出现次数多的总是majority element的。

下面之所以用C++的方式来实现,是因为在用Java来写的过程中出现了个问题就是Math.pow(a,b)来计算幂次的时候结果是double类型,强转之后丢失一半,所以还要处理这个情况比较麻烦,用C++来没有出现这个问题。

class Solution
{
public:
int majorityElement(vector<int> &num) {
int bitCnt[];
memset(bitCnt, , sizeof(bitCnt)); for (int i = ; i < num.size(); i++) {
for (int j = ; j < ; j++) {
if (num[i] & ( << j))
bitCnt[j]++;
}
} int ans = ;
for (int i = ; i < ; i++) {
if (bitCnt[i] > num.size()/)
ans += (int)pow(, i);
}
return ans;
}
};

LeetCode——Majority Element的更多相关文章

  1. 2016.5.18——leetcode:Majority Element

    Majority Element 本题收获: 1.初步了解hash,nth_element的用法 2.题目的常规思路 题目: Given an array of size n, find the ma ...

  2. [LeetCode] Majority Element II 求众数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  3. [LeetCode] Majority Element 求众数

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

  4. LeetCode Majority Element I && II

    原题链接在这里:Majority Element I,Majority Element II 对于Majority Element I 来说,有多重解法. Method 1:最容易想到的就是用Hash ...

  5. [LeetCode] Majority Element II 求大多数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...

  6. [LeetCode] Majority Element 求大多数

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

  7. LeetCode Majority Element I

    原题链接在这里:https://leetcode.com/problems/majority-element/ 题目: Given an array of size n, find the major ...

  8. LeetCode Majority Element Python

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

  9. Leetcode: Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

随机推荐

  1. C#反射—解决类的实例化问题

    利用简单工厂模式来改进抽象工厂使用的复杂性(抽象工厂详见 设计模式之—抽象工厂模式) 数据表(User)业务处理接口(IUser) namespace FactoryMethodPatternDB.C ...

  2. jQuery创建ajax关键词数据搜索

    在web开发过程当中,我们经常需要在前台页面输入关键词进行数据的搜索,我们通常使用的搜索方式是将搜索结果用另一个页面显示,这样的方式对于搭建高性能网站来说不是最合适的,今天给大家分享一下如何使用 jQ ...

  3. ajax请求aspx页面

    首先,这么用是不好的.最好用ashx,但也难免遇到这种需求.开发过这么一个系统,每天访问量最多100,web服务器压力很小,完全大马拉小车,主要压力都在数据库服务器上,要做大量的统计.所以页面直接全上 ...

  4. jquery对同级的td做radio限制

    <html> <head> <title></title> <script src="http://libs.baidu.com/jqu ...

  5. Activity之间的数据传递(Arraylist)

    1.使用Serialiable方法 实现序列化 2.使用Parcelable方法(这是android自己封装的类) Parcel类是封装数据的容器,封装后的数据通过Intent和IPC传递     实 ...

  6. 安装php时,make步骤报错make: *** [sapi/fpm/php-fpm] Error 1

    安装PHP过程中,make步骤报错:(集中网络上各种解决方法) (1)-liconv -o sapi/fpm/php-fpm /usr/bin/ld: cannot find -liconv coll ...

  7. oracle死锁模拟

    环境介绍: 用户test01 创建表tab01,用户test02创建表tab02.Test01 更新tab01不提交,test02 更新表tab02不提交.然后test01 更新test02下的表ta ...

  8. 01-android快速入门

    adb Android debug bridge 安卓调试桥 创建模拟器,屏幕尽量小些,启动速度运行速度快 Android项目的目录结构 Activity:应用被打开时显示的界面 src:项目代码 R ...

  9. phpstudy apache 刚启动便停止

    1.添加站点 2.重启服务 3.遇见问题 apache 刚启动,1秒钟中后就停止 4.解决问题 发现是自己添加的网站中包含中文路径的问题,建议不要在自己的网站目录下包含中文.

  10. SqlServer CTE 递归查询 Oracle递归查询

    在做数据库设计这块,很多时候表的数据模型就是典型的二叉树结构. 于是在查询数据的时候,就涉及到了数据的递归查询. 递归查询分为两种:1.从根节点查询自身以及所有的子节点:2.从子节点查询自身以及所有的 ...