Java for LeetCode 169 Majority Element
2015-06-06 00:06
465 查看
Given an array of size n, find the majority element. The majority element is the element that appears more than
You may assume that the array is non-empty and the majority element always exist in the array.
解题思路:
编程之美P130(寻找发帖水王)原题,如果删除两个不同的num,那么剩下的num,最多的元素依旧出现多于
JAVA实现如下:
⌊ 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); }
相关文章推荐
- 在控制台打印半径为R的圆
- 在控制台用星号打印一个高M的等腰三角形与菱形
- 魔术矩阵
- Java多线程、事件
- Spring @MVC configuration without XML
- MD5加密结果在不同的环境下出现不一致
- 【SSH进阶之路】Struts + Spring + Hibernate 进阶开端(一)
- Eclipse配色方案
- SSO单点登陆环境搭建
- java高级工程师需要掌握的技术
- 斐波那契堆(Fibonacci heap)原理详解(附java代码实现)
- 【JAVA基础知识总结】Java自定义异常
- 【JAVA基础教程】java中异常机制总结
- java project 连接hibernate 出错
- [note] java与DB
- Java for LeetCode 168 Excel Sheet Column Title
- Java学习篇之---LinkedList
- Java for LeetCode 166 Fraction to Recurring Decimal
- java.lang.IllegalArgumentException: Illegal character in scheme at index 0
- java相关知识介绍