题目描述:

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. javascript中alert()与console.log()的区别

    我们在做js调试的时候使用 alert 可以显示信息,调试程序,alert 弹出窗口会中断程序, 如果要在循环中显示信息,手点击关闭窗口都累死.而且 alert 显示对象永远显示为[object ]. ...

  2. pureftpd安装配置-pureftp参数详解(一)

    1. 下载 #cd /usr/local/src/ #wget ftp://ftp.pureftpd.org/pub/pure-ftpd/releases/pure-ftpd-1.0.30.tar.g ...

  3. SQL Server 导数据 Oracle

    1. 使用Sql Server的企业管理器导入(推荐) 优点: 可以指定导入的表. 缺点: 转成Oracle时, 对应的数据类型要一个一个手动修改   2.使用ORACLE官方提供的Sql Devel ...

  4. [设计模式] 22 模板方法模式 template

    转http://www.jellythink.com/archives/407 在GOF的<设计模式:可复用面向对象软件的基础>一书中对模板方法模式是这样说的:定义一个操作中的算法骨架,而 ...

  5. linux 下 apache启动、停止、重启命令

    基本的操作方法: 本文假设你的apahce安装目录为/usr/local/apache2,这些方法适合任何情况 apahce启动命令: 推荐/usr/local/apache2/bin/apachec ...

  6. ext3grep 恢复删除

    Linux ext2/ext3 文件删除恢复工具ext3grep安装使用 2010-08-23 18:03:10|  分类: 默认分类|举报|字号 订阅     一.           安装前系统环 ...

  7. jmeter 使用聚合报告分析jtl文件

    对于jmeter测试生成产生的jtl文件除了使用jemter插件来产生csv或者结果,还可以直接用聚合报告来打开,下面来介绍一下怎么操作. 1. 产生jtl文件 注意,默认情况下聚合报告插件只能分析聚 ...

  8. java实现大数加法、乘法(BigDecimal)

    之前写过用vector.string实现大数加法,现在用java的BigDecimal类,代码简单很多.但是在online-judge上,java的代码运行时间和内存大得多. java大数加法:求a+ ...

  9. lintcode: search for a range 搜索区间

    题目 搜索区间 给定一个包含 n 个整数的排序数组,找出给定目标值 target 的起始和结束位置. 如果目标值不在数组中,则返回[-1, -1] 样例 给出[5, 7, 7, 8, 8, 10]和目 ...

  10. PowerDesigner技巧

    原文:PowerDesigner技巧 1.PowerDesigner使用MySQL的auto_increment  ◇问题描述:  PD怎样能使主键id使用MySQL的auto_increment呢? ...