【一天一道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 ...
随机推荐
- java集合之HashMap源码解读
源自:jdk1.8.0_121 HashMap继承自AbstractMap,实现了Map.Cloneable.Serializable. HashMap内部是由数组.链表.红黑树实现的 变量 // 默 ...
- 阿里 & 酷家乐:实习生面试
最近海投了十家公司,暂时有阿里两面(已凉).酷家乐两面(大概凉了).网易一面.前两个都是基础知识发挥得还可以,两家公司二面都凉凉. 阿里一面(3.21 26min) 刚好买了中饭回宿舍打开正准备吃的时 ...
- 点(x1, y1)关于点(x0, y0)逆时针旋转a度后的坐标求解
问题描述: 求点(x1, y1)关于点(x0, y0)逆时针旋转a度后的坐标 思路: 1.首先可以将问题简化,先算点(x1, y1)关于源点逆时针旋转a度后的坐标,求出之后加上x0,y0即可. 2.关 ...
- idea热部署
<!-- 热部署模块 --> <dependency> <groupId>org.springframework.boot</groupId> < ...
- Servlet生命周期与工作原理(转载)
Servlet生命周期分为三个阶段: 1,初始化阶段 调用init()方法 2,响应客户请求阶段 调用service()方法 3,终止阶段 调用destroy()方法 Servlet初始化阶段: 在 ...
- setuptools安装和错误解决
错误解决:ImportError No module named setuptools GitHub: https://github.com/pypa/setuptools 下载安装 wget htt ...
- Asp.Net Core 2.0 项目实战(7)MD5加密、AES&DES对称加解密
本文目录 1. 摘要 2. MD5加密封装 3. AES的加密.解密 4. DES加密/解密 5. 总结 1. 摘要 C#中常用的一些加密和解密方案,如:md5加密.RSA加密与解密和DES加密等, ...
- kafka简单回顾
先说说遇到的坑 回顾下kafka topic:生产组:P0\P1----P14 一个消费组:c0 c1 c2 依据Consumer的负载均衡分配 消费顺序"c0:p0-p4 c1:p5-p9 ...
- Node.js Buffer(缓冲区)
JavaScript 语言自身只有字符串数据类型,没有二进制数据类型. 但在处理像TCP流或文件流时,必须使用到二进制数据.因此在 Node.js中,定义了一个 Buffer 类,该类用来创建一个专门 ...
- Docker有用的资源
资源链接 Docker 主站点: https://www.docker.io Docker 注册中心API: http://docs.docker.com/reference/api/registry ...