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 ...
随机推荐
- golang回调函数的例子
package main import "fmt" type TestStruct struct { } func (object *TestStruct) test(msg st ...
- ipvsadm的命令参考
相信很多同学和我差不多,半桶水,貌似在配置lvs双机的时候,直接用的keepalived,ipvsadm就用来看看,感觉没啥用,今天无聊到处逛发现,某大神说,keepalived只是ipvsadm的一 ...
- linux tcp server demo
#include <sys/types.h> #include <sys/socket.h> #include <string.h> #include <ne ...
- 029:高可用之MHA
高可用之MHA 一.MHA 简介 MHA(Master High Availability)是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件.在MySQL故障切换过程中,MHA能 ...
- TCP/IP协议:最大传输单元MTU 和 最大分节大小MSS
MTU = MSS + TCP Header + IP Header. mtu是网络传输最大报文包. mss是网络传输数据最大值. MTU:maximum transmission unit,最大传输 ...
- Druid.io系列(三): Druid集群节点
原文链接: https://blog.csdn.net/njpjsoftdev/article/details/52955937 1 Historical Node Historical Node的职 ...
- 在linux下使用debugfs恢复rm删除的文件
原理主要是删除的文件并没有实际上从硬盘上摸去,只是inode索引删除了相关的信息,因此只要找到刚删除文件的block上,就可以恢复已经删除的文件. 以下方法在ext3的文件系统上测试通过,ext2的没 ...
- python socket打造一个定位工具
前言: 刚刚学习socket,打算后期得学习 怎么写exploit. 原理: 其实很简单,客户端写个爬虫.然后将获取到的IP放入高德地图 在通过socket发送.利用ngrok达到能外网搞事. 准备: ...
- 浅析Web Services
Web Services 可使您的应用程序成为 Web 应用程序. Web Services 通过 Web 进行发布.查找和使用. 1.什么是Web Services? Web Services 是应 ...
- 第四篇 Flask 中的模板语言 Jinja2 及 render_template 的深度用法
是时候开始写个前端了,Flask中默认的模板语言是Jinja2 现在我们来一步一步的学习一下 Jinja2 捎带手把 render_template 中留下的疑问解决一下 首先我们要在后端定义几个字符 ...