169. Majority Element 出现次数超过n/2的元素
[抄题]:
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.
[暴力解法]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
以为n个元素的出现次数一定也要用n个变量来存。
[一句话思路]:
用一个count来存最多次数,节约空间
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 看清要求,最后返回的是major元素,而不是次数
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
用一个count的++--来节约存储空间
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
229. Majority Element II 数学题,也是醉了
[代码风格] :
class Solution {
public int majorityElement(int[] nums) {
//ini
int major = nums[0];
int count = 1;
//1 - n
for (int i = 1; i < nums.length; i++) {
//nums[i] != major, count--,if count == 0
if (nums[i] != major) {
if (count == 0) {
major = nums[i];
count++;
}
count--;
}else {
//nums[i] == major, count++
count++;
}
}
return major;
}
}
169. Majority Element 出现次数超过n/2的元素的更多相关文章
- 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返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排 ...
- 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 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 ...
- [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 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- 浅谈splay
\(BST\) 二叉查找树,首先它是一颗二叉树,其次它里面每个点都满足以该点左儿子为根的子树里结点的值都小于自己的值,以该点右儿子为根的子树里结点的值都大于自己的值.如果不进行修改,每次查询都是\(O ...
- Unit02: jQuery事件处理 、 jQuery动画
Unit02: jQuery事件处理 . jQuery动画 jQuery实现购物车案例 <!DOCTYPE html> <html> <head> <titl ...
- TCP之二:TCP的三次握手与四次分手
一.TCP是什么? 具体的关于TCP是什么,我不打算详细的说了:当你看到这篇文章时,我想你也知道TCP的概念了,想要更深入的了解TCP的工作,我们就继续.它只是一个超级麻烦的协议,而它又是互联网的基础 ...
- Python文件操作,with open as追加文本内容实例
最常见的读写操作 import re with open('/Users/Mr.Long/Desktop/data.txt', 'w') as f: f.write('hello world') 就这 ...
- 竖屏拍照,但是sd卡中却是横屏解决方法
protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (resultCode) ...
- 安装配置solr
1.由于用户是普通用户,没有root一些权限,所以修改hadoop用户权限 用root权限,修改sudoers文件 nano /etc/sudoers 打开文件,修改hadoop用户权限,如 ...
- HTTP客户端之使用request方法向其他网站请求数据
在node中,可以很轻松的向任何网站发送请求并读取该网站的响应数据. var req=http.request(options,callback); options是一个字符串或者是对象.如果是字符串 ...
- Oracle查看和修改连接数
1.查询数据库当前进程的连接数: select count(*) from v$process; 2.查看数据库当前会话的连接数: elect count(*) from v$sessio ...
- linux中awk工具
awk sed以行为单位处理文件,awk比sed强的地方在于不仅能以行为单位还能以列为单位处理文件.awk缺省的行分隔符是换行,缺省的列分隔符是连续的空格和Tab,但是行分隔符和列分隔符都可以自定义, ...
- react之本地图片引用
react之本地图片引用 <img src="../images/photo.png"/> 这种写法在react中是不支持的,所以引用本地图片需要用import或者re ...