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.
分析:
遍历数组,每当发现一对儿不相同的element时就成对儿删除,最后剩下的一定是个数过半儿的element。
public class Solution {
public int majorityElement(int[] nums) {
int count = 0;
int majElem = 0;
for(int i=0; i<nums.length; i++) {
if(count == 0) {
majElem = nums[i];
count++;
} else {
if(majElem == nums[i]) {
count++;
} else {
count--;
}
}
}
return majElem;
}
}
LeetCode 169. Majority Element的更多相关文章
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- 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(求众数)
题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...
- [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 冰山查询
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 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 for LeetCode 169 Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- 腾讯优测优分享 | Android适配中的一些特殊情况小结
腾讯优测是专业的自动化测试平台,提供全面兼容适配测试,远程真机租用等多维度的测试服务! 作为一名"艰苦卓绝"的软件工程师,我在开发路上经常被各种奇葩情况虐的体无完肤...今天就想与 ...
- Spring mvc shiro 整合
参考 : http://www.360doc.com/content/14/0722/10/18637323_396209195.shtml http://www.360doc.com/content ...
- Python2.7.12开发环境构建(自动补全)
一.安装readline-devel包 Python的编译安装依赖于这个包 yum -y install readline-devel 二.安装Python2.7.12 Python官方网站(到此处下 ...
- Linux LVM硬盘管理及LVM扩容
LVM磁盘管理 一.LVM简介... 1 二. LVM基本术语... 2 三. 安装LVM... 3 四. 创建和管理LVM... 4 2. 创建PV.. 6 3. 创建VG.. 7 4. 创建LV. ...
- 在指定的div中搜索内容,并滚动显示到当前搜索到的内容处
我想要的是页面中有个带滚动条的div对象,里面有很多内容,想要用js搜索到div中的某个字符串内容,然后将div的滚动条滚动到搜索到的内容处显示,自动定位.先是查找页面中的内容,然后将找到的内容创建t ...
- java如何在eclipse编译时自动生成代码
用eclipse写java代码,自动编译时,如何能够触发一个动作,这个动作是生成本项目的代码,并且编译完成后,自动生成的代码也编译好了, java编辑器中就可以做到对新生成的代码的自动提示? 不生成代 ...
- VS2008基于对话框的MFC上位机串口通信(C++实现)简单例程
首先,在 vs2008 环境下创建 MFC 运用程序 设置项目名称为 ComTest(这个地方随意命名,根据个人习惯),点击确定后,点击下一步 出现如下界面 选择"基于对话框"模式 ...
- mvc模型验证
自定义的ValidationAttribute类, Compare [Compare("MyOtherProperty")] 两个属性必须相同值,比如我们要求用户重复输入两次邮 ...
- CentOS 7 vs CentOS 6的不同
(1)桌面系统 [CentOS6] GNOME 2.x [CentOS7] GNOME 3.x(GNOME Shell) (2)文件系统 [CentOS6] ext4 [CentOS7] xfs (3 ...
- SQL Server 优化-执行计划
对于SQL Server的优化来说,优化查询可能是很常见的事情.由于数据库的优化,本身也是一个涉及面比较的广的话题, 因此本文只谈优化查询时如何看懂SQL Server查询计划.毕竟我对SQL Ser ...