【一天一道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 IO流笔记
File 类 ps:注意file的delete失败可能是输入输出流还在用这个file Java实现简单文件管理 package fileDemo; import java.io.File; impor ...
- win 10 和 CentOS 7 双系统安装
工具及材料 1.一台PC 2.一个U盘,8G以上 3.需要的文件:CentOS-7-x86_64-DVD-1511.iso 4.需要的软件:UltraI ...
- PHP $_GET 变量
$_GET 变量 预定义的 $_GET 变量用于收集来自 method="get" 的表单中的值. 从带有 GET 方法的表单发送的信息,对任何人都是可见的(会显示在浏览器的地址栏 ...
- Bash shell中的位置参数$#,$*,$@,$0,$1,$2...及特殊参数$?,$-等的含义
http://hi.baidu.com/lolorosa/blog/item/5775a608bd670d33b0351da7.html $# 是传给脚本的参数个数 $@ 是传给脚本的所有参数的列表 ...
- Eclipse调试(2)——各种类型断点设置
本文是Eclipse调试(1)--基础篇 的提高篇.分两个部分: 1) Debug视图下的3个小窗口视图:变量视图.断点视图和表达式视图 2) 设置各种类型的断点 变量视图.断点视图和表达式视图 1. ...
- Objective-C构造方法
Objective-C构造方法 构造方法:用来初始化的方法 创建对象的原理 之前我们创建对象的方式一直是使用[Xxx new] 但是使用 new 创建的对象,都是给我们默认做了初始化的. 有的时候,我 ...
- Swift:Minimizing Annotation with Type Inference
许多程序猿更喜欢比如Python和Javascript这样的动态语言,因为这些语言并不要求程序猿为每个变量声明和管理它们的类型. 在大多数动态类型的语言里,变量可以是任何类型,而类型声明是可选的或者根 ...
- PGM:图模型学习概述
http://blog.csdn.net/pipisorry/article/details/52571640 动机 前面我们讨论的问题出发点是给定一个图模型.如在独立性和推理讨论中,假定模型--结构 ...
- Android图表库MPAndroidChart(十三)——简约的底部柱状图
Android图表库MPAndroidChart(十三)--简约的底部柱状图 我们继续上一讲,今天还是说下柱状图,这个图的话应该是用的比较多的,所有拿出来溜溜,先看下效果 我们还是来看下基本实现 一. ...
- Android图表库MPAndroidChart(十二)——来点不一样的,正负堆叠条形图
Android图表库MPAndroidChart(十二)--来点不一样的,正负堆叠条形图 接上篇,今天要说的,和上篇的类似,只是方向是有相反的两面,我们先看下效果 实际上这样就导致了我们的代码是比较类 ...