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.
思路.1
排序,选择第n/2个数,调用STL的sort,所以时间复杂度是O(nlogn)
class Solution {
public:
int majorityElement(vector<int>& nums) {
sort(nums.begin(),nuns.end());
return nums[nums.size()/2];
}
};
思路2
设置一个计数,当count为0时设置majority的值,如果下一个值与majority相同则count+1,如果不同,则-1,当count==0时,对majority重新赋值,因为majority的个数肯定大于n/2所以最后>0的count的肯定是majority,这样,就只需要遍历一遍就可以求出majority,时间复杂度为O(n)
class Solution {
public:
int majorityElement(vector<int>& nums) {
int Majority = nums[0];
int count = 1;
for( int i = 1; i < nums.size(); i++ ){
if( count == 0 ){
count++;
Majority = nums[i];
}
else if( Majority == nums[i] ){
count++;
}
else if ( Majority != nums[i] ){
count--;
}
}
return Majority;
}
};
LeetCode【169. Majority Element】的更多相关文章
- LeetCode OJ 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode Problem 169: Majority Element查找多数元素
描述:Given an array of size n, find the majority element. The majority element is the element that app ...
- leetcode 【 Find Peak Element 】python 实现
题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...
- 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 Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II
169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...
- Leetcode#169. Majority Element(求众数)
题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...
- 169. Majority Element - LeetCode
Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排 ...
- Week1 - 169.Majority Element
这周刚开始讲了一点Divide-and-Conquer的算法,于是这周的作业就选择在LeetCode上找分治法相关的题目来做. 169.Majority Element Given an array ...
随机推荐
- MemCache的LRU删除机制详解
本节主要讲解一下MC的LRU的删除机制和一些参数的限制 MC的过期数据惰性删除 1.当某个值过期后,并没有从内存中删除,因此,我们在进行st ats统计信息的时候,curr_items有其信息(它的数 ...
- Android Studio编译输出apk文件修改文件名
新建一个Android工程,默认编译会生成一个叫app-debug.apk或者叫app-release.apk文件,说实话,单纯看文件名,我都不到任何有用的信息,我希望输出的文件名是这样的: 模块名- ...
- python核心编程第六章练习6-9
6-9.转换.为练习5-13写一个姊妹函数,接受分钟数,返回小时数和分钟数.总时间不变,并且要求小时尽可能大.[答案]代码如下: #!/usr/bin/env python # translate m ...
- Java过滤器,SpringMVC拦截器之间的一顺序点关系
由于最近做的项目中有一部分是接口远程调用,用到了接入权限和业务权限的鉴定,需要采用SpringMVC的拦截器,以前用Struts2的时候用过拦截器,而SpringMVC的拦截器功能之前没研究过,所以这 ...
- sanBox部署简介
参考资料:1 http://www.kaaproject.org/getting-started/ 此链接告诉我们部署sandbox的两种方法. 2 http://docs.kaaprojec ...
- css\html布局及部分知识小分享~~~
近期发现和总结的知识跟大侠们分享,请大侠们多多评论指教一二? HTML 1.(1)body需设置页面默认字体大小 body{font-size:12px;} (2)IE6下png图片划过切换失效,建 ...
- JS判断form内所有表单是否为空
function checkForm(){ var input_cart=document.getElementsByTagName("INPUT"); for(var i=0 ...
- PDF firefox转换器
- [整]C#获取天气预报信息(baidu api)包括pm2.5
/// <summary> /// 获取天气预报信息 /// </summary> /// <returns></returns> public Bai ...
- jQuery:常用方法一览
Attribute:$(”p”).addClass(css中定义的样式类型); 给某个元素添加样式$(”img”).attr({src:”test.jpg”,alt:”test Image”}); 给 ...