leetcode 169
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.
找出数列中个数超过一半的数,假定数列不为空且该数一定存在。
代码如下:
class Solution {
public:
int majorityElement(vector<int>& nums) {
int n = nums.size();
int index = ;
int candidate = nums[];
for(int i = ; i < n; i++)
{
if(candidate == nums[i])
{
index ++;
}
else
{
index --;
}
if(index < )
{
candidate = nums[i];
index = ;
}
}
return candidate;
}
};
leetcode 169的更多相关文章
- 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 (众数)
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(求众数)
题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...
- 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 多数元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Java实现 LeetCode 169 多数元素
169. 多数元素 给定一个大小为 n 的数组,找到其中的多数元素.多数元素是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在多数元素. 示例 1: 输 ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- Ubuntu中安装eclipse ,双击eclipse出现invalid configuration location问题
ubuntu invalid configuration location 标签: myeclipse for ubuntu ubuntu myeclipse ubuntu安装myecli ...
- 超级链接a中javascript:void(0)弹出另外一个框问题
转字:http://my.oschina.net/castusz/blog/68186 结果在IE.Firefox.Chrome都是先执行的onclick事件,在项目中我们尽量不要同时使用这两种方式. ...
- Modbus工业协议在Android中的应用
现在工业信息画发展,很多工厂都需要做信息化展示,通常都是利用Android一体机来进行展示和交互. Modbus协议是全球第一个用于工业现场的总线协议,与外设交互可以采用串口通信,tcp等方式:通常在 ...
- JavaScript 运行时错误: 无法获取未定义或 null 一种解决方案
脚本是肯定没有错误的!! 引用了高版本的jquery jquery-1.10.1.min.js 但在ie10下面就是报错 "JavaScript 运行时错误: 无法获取未定义或 null & ...
- 一些sql语句的常用总结(重要)
select primary_flag from tc_contact where primary_flag !=0 select dept_id,dept_name,tree_level,tree_ ...
- Python 决策树的构造
上一节我们学习knn,kNN的最大缺点就是无法给出数据的内在含义,而使用决策树处理分类问题,优势就在于数据形式非常容易理解. 决策树的算法有很多,有CART.ID3和C4.5等,其中ID3和C4.5都 ...
- sscanf格式化输出
char DesChar[20] = {0}; char* SouChar= "1cZCD23456abEFdedfB"; sscanf(SouChar,"%[^A-Z] ...
- 【转载】ANSYS动力学分析-瞬态分析
原文地址:http://www.cnblogs.com/ylhome/archive/2009/12/02/1615172.html 三种求解方法 瞬态动力学分析可采用三种方法:完全(Full)法.缩 ...
- SCRUM项目 6.0
6.0----------------------------------------------------- sprint演示 1.坚持所有的sprint都结束于演示. 团队的成果得到认可,会感觉 ...
- trace工具,c++/c#/python
下载地址: http://files.cnblogs.com/files/wjx0912/xtrace.rar 很方便的调试工具,已在c#, vc2015, python2.7.10环境下测试. 闭 ...