leetcode 153: 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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
public class Solution {
public int majorityElement(int[] num) {
// assuming the num is not null and empty.
int most = num[0];
int counter = 1;
for(int i=1; i<num.length; i++) {
if(num[i] == most) {
++counter;
} else {
if(counter==0) {
most = num[i];
++counter;
} else {
--counter;
}
}
}
return most;
}
}
leetcode 153: Majority Element的更多相关文章
- [LeetCode] 169. Majority Element 多数元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [LeetCode] 229. Majority Element II 多数元素 II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...
- 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 ...
- 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 - majority vote algorithm (Java)
1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...
- 【leetcode】Majority Element
题目概述: Given an array of size n, find the majority element. The majority element is the element that ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
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 ...
随机推荐
- 转:RHEL6.3 安装GCC 记录
本文参考:http://blog.163.com/phys_atom/blog/static/1676445532012229814992/ 如果直接使用GUN GCC官方的源码来安装是不成功的,因为 ...
- 【APP接口开发】php输出json格式数据
请一定配合使用null转空字符的方法一起使用:(_unsetNull() 和 _json() 配合使用) 在一些接口的调用中,直接查询数据库出来的字段可能为null字段,但是为了简便前端的判断,需要把 ...
- GNOME下也是Alt+F2,输入gnome-terminal
如果桌面有terminal 的话 ,直接用上下键就可以了 Alt + F1 类似Windows下的Win键,在GNOME中打开”应用程序”菜单(Applications) Alt + F2 类似W ...
- 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- jQuery 操作cookie保存用户浏览信息
使用jQuery操作cookie之前需要引入jQuery的一个cookie小组件js,代码如下: /* jQuery cookie plugins */jQuery.cookie ...
- Android—— 定制界面风格
统一的用户界面是可以使得应用程序更友好.要做到用户界面的统一,我们就必须用到风格(style)和主题(theme).OPhone系统提供了很多系统默认的风格和主题,但是很多情况下,这些不能满足我们的需 ...
- Eclipse使用教程之精华篇
插件安装方法 插件大概有三种安装方法: 第一种:知道在线安装地址.Eclipse→Help→Install New Software...→地址栏(Work with)中输入安装地址→勾选要安装的插件 ...
- 支付宝钱包手势密码破解实战(root过的手机可直接绕过手势密码)
/* 本文章由 莫灰灰 编写,转载请注明出处. 作者:莫灰灰 邮箱: minzhenfei@163.com */ 背景 随着移动互联网的普及以及手机屏幕越做越大等特点,在移动设备上购物.消费已是 ...
- StrongLoop
http://loopback.io/getting-started/ 使用 StrongLoop 创建 Node.js MySQL 应用程序 StrongLoop 是 IBM 的一家子公司,Stro ...
- Genral log(普通日志)与 Slow log(慢速日式)
General log: Geleral log记录了服务器接收到的每一个查询或是命令,无论这些查询或是命令是否正确甚至是否包含语法错误,general log 都会将其记录下来 ,记录的格式为 {T ...