Java for LeetCode 169 Majority Element
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的更多相关文章
- Leetcode#169. Majority Element(求众数)
题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- [LeetCode] 169. Majority Element 多数元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 23. leetcode 169. Majority Element
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- LeetCode 169. Majority Element - majority vote algorithm (Java)
1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Java [Leetcode 169]Majority Element
题目描述: Given an array of size n, find the majority element. The majority element is the element that ...
- LeetCode 169. Majority Element (众数)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- leetcode 169 Majority Element 冰山查询
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- 使用ssis完成excel的数据导入
SSIS(SQL Server Integration Service)是从MS SQL 2005开始引入的,是一种ETL(Extract Transform Load)工具,SSIS比普通的ETL更 ...
- 理解SIFT
理解SIFT.tab{font-size:12px; margin-bottom: 10px;}.tab a{cursor:pointer;cursor:pointer;display:inline- ...
- SPOJ Pouring Water
传送门 POUR1 - Pouring water #gcd #recursion Given two vessels, one of which can accommodate a litres o ...
- java 小记
1.获取web项目根目录的绝对路径 request.getContextPath() 获取项目名称,如 /BiYeSheJi getServletContext().getRealPath(& ...
- Java操作Excel(读、写、搜索关键字、插入图片)
import java.io.File; import java.io.IOException; import jxl.Cell; import jxl.Sheet; import jxl.Workb ...
- Spark Graphx In Action
两个重要的技术:Spark和graphs 本章节内容 为什么Spark是最先进的大数据处理系统 是什么让图可以以一种独特的方式来模拟关联数据 GraphX为什么会成规领先的图分析平台
- Java调用动态库方法说明-最详细
Java不能直接调用由c或者c++写得dll(TF_ID.dll),所以只能采用jni得方法,一步一步生成符合规范得dll文件(假设叫FANGJIAN.dll),在FANGJIAN.dll这个文件里来 ...
- shell 删除文件下的* (copy).jpg备份文件
shell编程中, 在for, while循环中为什么不用(), {} 不是没有; 而是因为(), {}做了其他用途: (): 执行命令组, 注意这个命令组是新开一个子shell中执行, 因此,括号 ...
- Highways(prim & MST)
Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23421 Accepted: 10826 Descri ...
- JQuery入门
JQuery入门 1 jQuery的概述 1.1 jQuery简介 jQuery是一个 JavaScript函数库,它是一个“写的更少,但做的更多”的轻量级 JavaScript 库.jQuery 极 ...