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 ...
随机推荐
- PARSEC-3.0编译错误
OS: Ubuntu 14.04 LTS (x86_64) ***error 1 OpenSSL 1.0.1e 与 perl5.18 不兼容 POD document had syntax error ...
- 用sql语句建表
CREATE TABLE USER (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(30) NOT NULL, p ...
- 浅谈P NP NPC
P问题:多项式时间内可以找到解的问题,这个解可以在多项式时间内验证. NP问题:有多项式时间内可以验证的解的问题,而并不能保证可以在多项式时间内找到这个解. 比如汉密尔顿回路,如果找到,在多项式时间内 ...
- 替换节点及replaceEach方法
需求: 为所有的 li 节点添加 onclick 响应函数实现 city 子节点和 game 子节点对应位置的元素的互换 window.onload = function(){ //自定义互换两个节点 ...
- [课程设计]Sprint One 总结&发表评论&团队贡献分
一.总结 第一次冲刺结束了,总体来说我们团队还是做得不错的,完成了既定的目标,希望接下来还能保持这样的动力,fighting... ● 二.围观其他组并发表评论 ● http://www.cnblog ...
- js 循环切换图片
function changeLot(){ var minIndex = 1; var maxIndex = 100; var curIndex = 10; var src = $("ul ...
- python之错误和异常
错误 分为语法错误和逻辑错误,如下: 语法错误指示软件的结构上有错误,导致不能被解释器解释或编译器编译. 逻辑错误可能是由于不完整或是不合法的输入所致,或者是无法生成.计算.或是输出结果需要的过程无法 ...
- Python单元测试和Mock测试
单元测试 测试可以保证你的代码在一系列给定条件下正常工作 测试允许人们确保对代码的改动不会破坏现有的功能 测试迫使人们在不寻常条件的情况下思考代码,这可能会揭示出逻辑错误 良好的测试要求模块化,解耦代 ...
- 【转】libevent源码分析
libevent源码分析 转自:http://www.cnblogs.com/hustcat/archive/2010/08/31/1814022.html 这两天没事,看了一下Memcached和l ...
- winform中messageBox七个参数的使用(转载)
private void button1_Click(object sender, EventArgs e) { MessageBox.Show(" 1 个参数 ”); } private ...