1 题目描述

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

2 分析

求主元素有两种O(n)量级的算法,一种是堆栈法,若栈为空或当前元素与栈顶元素相同,进栈,否则出栈。最后栈顶元素变为主元素。

另一种做法是芯片法,使用分治策略,比堆栈法要麻烦,主要是每次比较两个数字,不同,两个都扔掉,相同,留下一个,最后剩下的肯定是主元素。

3 代码

堆栈代码。

    public int majorityElement(int[] num)
{
int majorrityElement = 0;
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < num.length; i++) {
if (stack.isEmpty()) {
stack.push(num[i]);
}else{
if (stack.peek() == num[i]) {
stack.push(num[i]);
}else {
stack.pop();
}
}
}
majorrityElement = stack.peek();
return majorrityElement;
}

芯片法有点麻烦,也没写。

[Leedcode 169]Majority Element的更多相关文章

  1. 169. Majority Element(C++)

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

  2. 23. leetcode 169. Majority Element

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

  3. LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II

    169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...

  4. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  5. Leetcode#169. Majority Element(求众数)

    题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  6. Week1 - 169.Majority Element

    这周刚开始讲了一点Divide-and-Conquer的算法,于是这周的作业就选择在LeetCode上找分治法相关的题目来做. 169.Majority Element Given an array ...

  7. 169. Majority Element - LeetCode

    Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排 ...

  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. (Array)169. Majority Element

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

随机推荐

  1. sqrt函数倒数计算新对比

    某人发表说 雷神之锤 里面有一个 1/sqrt(x) 的函数非常了不起. 但经过实测,发现现在计算机已经优化, 该算法已经没有优势. 具体看文档: <a href="http://fi ...

  2. Ubuntu安装bcmath扩展

    sudo apt-get install php-bcmath

  3. Configuration Reference In Vue CLI 3.0

    Configuration Reference This project is sponsored by  #Global CLI Config Some global configurations ...

  4. [SoapUI] 通过JSONAssert比较两个环境的JSON Response,定制化错误信息到Excel

    package ScriptLibrary; import org.json.JSONArray; import org.json.JSONException; import org.json.JSO ...

  5. Java第2章笔记

    1.什么是变量:在程序运行过程中它的值是允许改变的量 2.java中常用的数据类型分为四类八种  第一类:整型   int(整数类型)     byte(字节类型)    short(短整形)     ...

  6. java web 大总结

    C/s架构:        socket.serversocket.awt/swing做一个客户端软件        建好socket连接后,通过IO流交换数据.数据格式由各个开发者自己确定,B/C架 ...

  7. Pappus一阶矩公式

  8. java常用设计模式十二:命令模式

    一.概述 定义:命令(Command)模式又叫作动作(Action)模式或事务(Transaction)模式,是一种对象的行为模式.将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对 ...

  9. s4-6 二层交换

    为什么需要二层交换?  有很多LAN,如何将它们连接起来? 可用网桥(bridges )将它们连接起来.  网桥工作在DLL层,通过检查MAC地址做出转发帧的决策 不会检查网络层,所以,IPv ...

  10. c# 得到list符合某条件的索引值,排序

    请教,在List集合中怎么得到元素的索引值,参考:http://www.myexception.cn/c-sharp/385022.html 这个可以用来读取窗口的多个textbox控件中内容: -- ...