题目描述:

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,就成对删除即count--,最终剩下的一定就是所求的。时间复杂度:O(n)。

代码如下:

public class Solution {
public int majorityElement(int[] nums) {
int major = 0;
int count = 0;
for(int i = 0; i < nums.length; i++){
if(count == 0){
major = nums[i];
count++;
}else{
if(nums[i] == major)
count++;
else
count--;
if(count >= nums.length / 2 + 1)
return major;
}
}
return major;
}
}

  

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

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

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

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

  4. 23. leetcode 169. Majority Element

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

  5. Java for 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 - majority vote algorithm (Java)

    1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...

  7. ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java

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

  8. LeetCode 169. Majority Element (众数)

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

  9. leetcode 169 Majority Element 冰山查询

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

随机推荐

  1. CSS滤镜详解

    语法:STYLE="filter:filtername(fparameter1, fparameter2...)" (Filtername为滤镜的名称,fparameter1.fp ...

  2. 【BZOJ】【3093】【FDU校赛2012】A Famous Game

    概率论 神题不会捉啊……挖个坑先 orz 贾教 & QuarterGeek /********************************************************* ...

  3. oracle将两个结果连接后进行查询,得到两个查询的联合结果

    一.需求 用户答题,共3道,必须3题都答完才能提交. 目的:要查询用户答对了几题,答错了几题.(当然此处可以只查答对的题目数,用3减即得答错题的题目数) 二.sql select * ) rightC ...

  4. linux系统进程的内存布局

    内存管理模块是操作系统的心脏:它对应用程序和系统管理非常重要.今后的几篇文章中,我将着眼于实际的内存问题,但也不避讳其中的技术内幕.由于不少概念是通用的,所以文中大部分例子取自32位x86平台的Lin ...

  5. 01-08-03【Nhibernate (版本3.3.1.4000) 出入江湖】二级缓存:NHibernate自带的HashtableProvider之缓存管理

    http://www.cnblogs.com/lyj/archive/2008/11/28/1343418.html 管理NHibernate二级缓存 NHibernate二级缓存由ISessionF ...

  6. Unity3d游戏中添加移动MM支付SDK问题处理

    原地址:http://www.tuicool.com/articles/I73QFb 由于移动mm的SDK将部分资源文件放在jar包中,导致Unity无法识别,提示failed to find res ...

  7. jsp bean标签

    jsp中存在一个奇奇怪怪的bean标签. 例如 现在在java包中定义一个类test2 package bean; public class test { private int number; pu ...

  8. 【C++基础】构造函数

    说说你对构造函数的理解? 构造函数:对象创建时,利用特定的值构造对象(不是构造类),将对象初始化(保证数据成员有初始值),是类的一个public 函数 ①   与类同名 ②   无返回值 ③   声明 ...

  9. [转载]Spring Web MVC Framework

    Required Configuration You need to map requests that you want the DispatcherServlet to handle, by us ...

  10. 一些linux的问题

    本文罗列的是我在学习linux与shell编程时所遇到的一些问题.我相信既然存在问题那么就会有需求,记录于此,希望可以快速帮助到大家. 1.在vim中用“/word"查找后,vim会以棕色背 ...