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.

分析:

遍历数组,每当发现一对儿不相同的element时就成对儿删除,最后剩下的一定是个数过半儿的element。

 public class Solution {
     public int majorityElement(int[] nums) {
         int count = 0;
         int majElem = 0;

         for(int i=0; i<nums.length; i++) {
             if(count == 0) {
                 majElem = nums[i];
                 count++;
             } else {
                 if(majElem == nums[i]) {
                     count++;
                 } else {
                     count--;
                 }
             }
         }

         return majElem;
     }
 }

LeetCode 169. Majority Element的更多相关文章

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

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

  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#169. Majority Element(求众数)

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

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

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

  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. Java for LeetCode 169 Majority Element

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

随机推荐

  1. 【转】Web Service单元测试工具实例介绍之SoapUI

    转自:http://blog.csdn.net/oracle_microsoft/article/details/5689585 SoapUI 是当前比较简单实用的开源Web Service 测试工具 ...

  2. C#的选择语句练习2

    1.输入a,b,c三个数,计算一元二次方程ax²+bx+c的根:若a=0,则不是一元二次方程:△=b²-4ac,根的计算公式为-b±√b²-4ac/2a:若△=b²-4ac>0,则方程有两个不一 ...

  3. 汇编语言进阶和Makefile进阶---第二天

    摘要: 原创博文:转载请标明出处:http://www.cnblogs.com/zxouxuewei 首先加载启动代码: ; hello-os ; TAB=4 ORG 0x7c00 ; 指明程序装载地 ...

  4. 【python】PIL 批量绘制图片矩形框工具

    工具采用PIL:Python Imaging Library,图像处理标准库.PIL功能非常强大,但API却非常简单易用. 安装PIL 在Debian/Ubuntu Linux下直接通过apt安装 $ ...

  5. js代码实现下拉菜单

    效果 js代码: <script type="text/javascript"> function ShowSub(li) {//函数定义 var subMenu = ...

  6. GetLastError返回值的含义

    [0]-操作成功完成. [1]-功能错误. [2]-系统找不到指定的文件. [3]-系统找不到指定的路径. [4]-系统无法打开文件. [5]-拒绝访问. [6]-句柄无效. [7]-存储控制块被损坏 ...

  7. first

    不知道学啥,怎么办,写博客.找不到工作,怎么办,写博客.好吧,第一天博客完成.-渣渣米

  8. 【我是老中医】VMware在win8.1下开Ubuntu提示”内部错误"解决方案

    这个题目起得很洋气啊,其实问题也比较好解决,但是我想多码几个字!!! 友情提示:如果不想看废话,请直接看最后的红字! 好的,咱们从头说(废话)起.话说我们学院每年都会组织大三的进行校企联合实训(其实就 ...

  9. (转)AppiumLibrary基本操作

    *** Settings *** Library AppiumLibrary Library Collections Library String Library Dialogs *** Test C ...

  10. Bash Shell内建命令和保留字

    Bash Shell内建命令和保留字命令含义!保留字,逻辑非:不做任何事,只做参数展开.读取文件并在shell中执行它alias设置命令或命令行别名bg将作业置于后台运行bind将关键字序列与read ...