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.

解题思路:

编程之美P130(寻找发帖水王)原题,如果删除两个不同的num,那么剩下的num,最多的元素依旧出现多于⌊ n/2 ⌋ times

JAVA实现如下:

	public int majorityElement(int[] nums) {
LinkedList<Integer> list=new LinkedList<Integer>();
for(int num:nums){
if(list.isEmpty())
list.add(num);
else if(num==list.getLast())
list.add(num);
else list.remove(list.size()-1);
}
return list.get(0);
}

Java for LeetCode 169 Majority Element的更多相关文章

  1. Leetcode#169. Majority Element(求众数)

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

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

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

  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. LeetCode 169. Majority Element - majority vote algorithm (Java)

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

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

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

  7. Java [Leetcode 169]Majority Element

    题目描述: Given an array of size n, find the majority element. The majority element is the element that ...

  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. 前端筑基篇(一)->ajax跨域原理以及解决方案

    说明 跨域主要是由于浏览器的“同源策略”引起,分为多种类型,本文主要探讨Ajax请求跨域问题 前言 参考来源 什么是跨域 ajax跨域的表现 跨域的原理 如何解决跨域问题 JSONP方式解决跨域问题 ...

  2. 【poj1274】 The Perfect Stall

    http://poj.org/problem?id=1274 (题目链接) 题意 懒得写了 Solution 二分图匹配裸题.注意清空数组. 代码 // poj3020 #include<alg ...

  3. asp.net input怎么获取值

    前台: <input type="hidden" name="content" value="content"> 后台: Req ...

  4. Code Review Engine Learning

    相关学习资料 https://www.owasp.org/index.php/Code_review https://www.owasp.org/images/8/8e/OWASP_Code_Revi ...

  5. C#文件复制功能

    目的是将用户自定义文件复制到指定文件夹并且能查看该文件,下面是个人做的源码: sing System; using System.Collections.Generic; using System.C ...

  6. IOS基础之 (十二) Block

    一 定义 Block封装了一段代码,可以在任何时候执行. Block可以作为函数参数或者函数的返回值,而其本身又可以带输入参数或返回值. 二 使用 1. 定义函数指针,然后在实现. #import & ...

  7. codeforce626D (概率)

    D. Jerry's Protest time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. codeforce626C.Block Towers(二分)

    C. Block Towers time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  9. Apache22中配置虚拟主机(Apache VirtualHost)

    Apache VirtualHost的作用就是可以让一个apache为多个域名服务,相当于一个服务器挂了N多个网站,举个例子: 我的apache服务器,ip为x.x.x.x,我有两个域名www.too ...

  10. Change ICON of MFC Application and Dialog

    Change ICON of MFC Application and Dialoghttp://www.codeproject.com/Tips/406870/Change-ICON-of-MFC-A ...