【一天一道LeetCode】#169. Majority Element
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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 len = nums.size();
sort(nums.begin(),nums.end());//调用STL的排序函数
return nums[len/2];
}
};
另一种优化的算法。O(n)复杂度。
定义两个整数j和num,j记录次数,num记录一个数
当遍历到nums数组的下一个数时,如果nums[j]==num,或者j==0,此时j++,并保存num的值为nums[j],
如果j!=0且nums[j]!=num,这时候j–,由于num这个数出现次数超过一半,那么最后一个将j变成1的数就是我们要找的数。
class Solution {
public:
int majorityElement(vector<int>& nums) {
int j = 0;//记录次数
int ret = nums[0];//初始化为第一个数
int size = nums.size();
for(int i =0 ; i < size ; i++)
{
if(j==0||nums[i]== ret){
ret=nums[i];
j++;
}
else j--;
}
return ret;
}
};
【一天一道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 ...
- LeetCode 169. Majority Element
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 ...
随机推荐
- github常用命令
全局配置 git config --global user.name "lewiscutey"git config --global user.email "lewisc ...
- C语言成语设计第一次作业
一 1.求圆面积和周长 输入圆的半径,计算圆的周长和面积. 2.流程图 3.测试数据及运行结果 测试数据:r=7 运行结果 4.实验分析 问题:第一次输入提示时未加双引号 解决办法:发现问题后加了双引 ...
- Maven集成dubbo时报错 Missing artifact com.alibaba:dubbo:jar:2.8.4
1.下载dubbo,地址:https://github.com/dangdangdotcom/dubbox . 2.将下载的压缩包解压. 3.命令行进入下载路径,执行mvn install -Dmav ...
- java表达式类型的自动提升
当一个java算术表达式中包含多个基本类型的值时,整个算术表达式的数据类型将发生自动提升.Java定义如下的自动提升规则:1. 所有byte型.short型和char型将被提升到int型. 2. 整个 ...
- Java获取随机数的3种方法
最小值---最大值(整数)的随机数 方法1 (数据类型)(最小值+Math.random()*(最大值-最小值+1)) 例: (int)(1+Math.random()*(10-1+1)) / ...
- idea,mybatis读取配置文件报错:Could not find resource configuration.xml
在pom.xml中,把xml文件加入编译,成功解决问题. <build> <resources> <resource> <directory>src/m ...
- struts2 Action获取表单传值(属性,类))
http://blog.csdn.net/sd0902/article/details/8393157 求大神告知两种方法的不同点 都是写个set方法就行了
- 前端实现搜索历史和清空历史(angularjs+ionic)
要实现的页面效果: 1.显示历史搜索, 2.最近搜索的排在最前, 2.最多显示8条历史 4.清空历史记录 思路: 1.首先显示历史记录需要一个数组searchItems,通过ng-repeat显示每一 ...
- MySQL/MariaDB 在插入数据的时候提示 Incorrect string value
现象 今天开新工程,建表的时候弹出这玩意: what's this? 看起来好像是说我传入的内容不对? 可是仔细看看内容,没乱码,标准的中文字符串.想来是编码问题. 经过修改编码后,解决了问题. 解决 ...
- python常用执行方式&变量&input函数
linux系统中执行py文件方式: ./a.py 需要执行权限 chmod -R 777(最大权限) 常用执行方式: 1. ./a.py2. python a.py 文件内部头加上 #!/usr/b ...