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. R语言实现 广义加性模型 Generalized Additive Models(GAM) 入门

    转载请说明. R语言官网:http://www.r-project.org/ R语言软件下载:http://ftp.ctex.org/mirrors/CRAN/         注:下载时点击 ins ...

  2. Log4j的简要概述

    读取配置文件的三种使用方式: 1.Java代码:BasicConfigurator.configure() 2.Java特性:键值对解析(properties文件) PropertyConfigura ...

  3. input 栏位光标末尾闪烁

    var input1 =window.document.getElementById("input1").createTextRange(); input1.collapse(fa ...

  4. Android驱动开发前的准备(四)

    源代码的下载和编译 4.1 下载.编译和测试Android源代码 4.2下载和编译linux内核源代码 4.1.1 配置Android源代码下载环境 (1) 创建一个用于存放下载脚本文件的目录 # m ...

  5. 点击每个li节点,都弹出其文本值及修改

    点击每个li节点,都弹出其文本值 1,获取所有的li节点 var liNodes=document.GetElementsByTagName("li"); 2,使用for循环进行遍 ...

  6. lnmp无法删除目录,目录包含.user.ini

    无法删除".user.ini"文件解决方法,运行后删除即可 chattr -i /home/wwwroot/yoursite/.user.ini 如果是需要修改文件,记得修改完以后 ...

  7. 3d数学总结帖

    3d数学总结帖,以下是对3d学习过程中数学知识的简单总结 角度值和弧度制的互转 Deg2Rad 角度A1转弧度A2 => A2=A1*PI/180 Rad2Deg 弧度A2转换角度A1 => ...

  8. dfs序

    dfs序比较重要的性质:一棵子树的所有节点在dfs序里是连续一段,主要就是利用这个性质来解题 题型一:对某个点X权值加上一个数W,查询某个子树X里所有点权值和. 解:列出dfs序,实现修改一个数,查询 ...

  9. codeMirror的简单使用,js比较文本差异(标注出增删改)

    最近项目需要使用比较文本的差异的功能,在同事的推荐下,使用js脚本来比较,所以codeMirror变成了选择. 当然codeMirror中有其他功能,比较文本差异的只是其中一个功能,本人不在此做介绍, ...

  10. TOMCAT 无法安装P7B格式的证书

    背景:通过按以下链接的方式生成了CSR文件,并申请到P7B格式的证书 现象:TOMCAT安装该证书时,要求输入Keystore key, 但是P7B证书自身并不携带私钥.导致无法通过TOMCAT安装该 ...