169. Majority Element (Array)
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.
Moore voting algorithm--每找出两个不同的element,就成对删除即count--,最终剩下的一定就是所求的。时间复杂度:O(n)
class Solution {
public:
int majorityElement(vector<int>& nums) {
int count = ;
int preNum = nums[];
for(int i = ; i < nums.size(); i++){
if(nums[i]==preNum){
count++;
}
else{
if(count == ){
preNum = nums[i];
}
else{
count--;
}
}
}
return preNum;
}
};
169. Majority Element (Array)的更多相关文章
- 169. Majority Element(C++)
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- 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] ...
- Week1 - 169.Majority Element
这周刚开始讲了一点Divide-and-Conquer的算法,于是这周的作业就选择在LeetCode上找分治法相关的题目来做. 169.Majority Element Given an array ...
- 169. Majority Element - LeetCode
Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排 ...
- 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 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- (Array)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 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- Actifio CDS 2TB MDisk limits
**** updated 7 Aug 2016 - reformatted and corrected out of date info *** Background Depending on you ...
- 【SpringBoot】服务器端主动推送SSE技术讲解
=====================16.高级篇幅之SpringBoot2.0服务器端主动推送SSE技术讲解 ============================ 1.服务端推送常用技术介绍 ...
- day45 jQuery
在用js写代码时,会遇到一些问题: window.onload 事件有事件覆盖的问题,因此只能写一个事件. 代码容错性差. 浏览器兼容性问题. 书写很繁琐,代码量多. 代码很乱,各个页面到处都是. 动 ...
- 算法笔记 3.2 codeup1934 找X
#include <stdio.h> ; int a[maxn]; int main(void){ int n; while(scanf("%d", &n)!= ...
- 串口接收端verilog代码分析
串口接收端verilog代码分析 `timescale 1ns / 1ps ////////////////////////////////////////////////////////////// ...
- mysql ibdata1
ibdata1是什么? Mysql ibdata1即Innodb data1缩写,是innodb引擎的表空间,用于存放 数据字典Data dictionary: 只读的表,存储对象的相关信息,如占用 ...
- Android开发 :androidstudio device offline
使用设备调试的时候,偶尔的就会遇到androidstudio device offline,尽管尝试开启/关闭 USB调试 .都无济于事. 如果PC安装了360手机助手就好办了(我的360手机助手 ...
- C语言中的作用域,链接属性和存储类型
作用域 当变量在程序的某个部分被声明的时候,他只有在程序的一定渔区才能被访问,编译器可以确认4种不同类型的作用域:文件作用域,函数作用域,代码块作用域和原型作用域 1.代码块作用域:位于一对花括号之间 ...
- 时间的转化 js
php 和java是不一样的 PHP 需要先乘1000 java 不需要 因为PHP传过来的是十位数 java传过来是十三位数 function formatDate() { var now = n ...
- for...in的改进版for...of
for...in 用起来似乎还不错,为什么又弄个 for...of 呢? 来看个例子: 'user strict' var arr = [12,13,14,15,16]; for(var i in a ...