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. Chp10 10.7

    <Java语言程序设计>P296 本章是关于对象的思考,主要是在研究面向对象的程序设计时类的设计,作业写得比较杂乱,构造方法时没有严格遵守类的流行设计风格,由于是作业,再加上比较简单,没有 ...

  2. ARP 扫描主机学习笔记

    1.通用套接字地址结构与具体套接字地址结构之间可相互转化 1)通用转具体,某些函数将结果存储在通用套接字地址结构中,这时将通用转换为具体,具体通过访问成员名可以方便的得到结果. 2)具体转通用,为了消 ...

  3. "错误消息 401.2。: 未经授权: 服务器配置导致登录失败。"的解决办法

    [详细报错如下]: “/”应用程序中的服务器错误. 访问被拒绝. 说明: 访问服务此请求所需的资源时出错.服务器可能未配置为访问所请求的 URL. 错误消息 401.2.: 未经授权: 服务器配置导致 ...

  4. google.GIS小例子

    var map; var array = [[41.774166667, 85.943055556], [43.864052, 87.560499]];//经纬度 var array1 = [&quo ...

  5. c语言数据结构之 堆排序

    算法:先生成随机数,赋值到数组,将数组第一个元素a[0]设置为哨兵,函数调用数组和随机数个数n,再设定n/2的根结点与孩子结点进行比较操作,若右孩子存在,则选出三个数里最小的数赋值给根节点,如果右孩子 ...

  6. java播放背景音乐的几种方式

    大四第一学期快结束了,准备找实习单位的时候,用了一周的时间去投简历去面试,结果没有一家有反馈要不就是面试没通过,拿着iOS的项目(在老师工作室的外包项目)去面试java开发,结果全部碰壁. 第一种,直 ...

  7. 《Matrix Computation 3rd》读书笔记——第2章 矩阵分析

  8. 解决 Django 后台上传图片前端无法展示

  9. 【新手】python爬虫遍历贴吧用户

    想法是遍历学校贴吧的用户,获取用户的数据用来分析,因为是初学python,就一点一点的写,变量命名也不规范,见谅 系统:windows 版本:python 3.5 #获取河北大学工商学院吧1000页以 ...

  10. git下载自己项目到本地

    git下载自己项目到本地 假如外出工作,需要在另一台电脑上面pull自己的某个git远程项目到本地 $ git init $ git pull https://github.com/TTyb/54qj ...