[抄题]:

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来存最多次数,节约空间

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 看清要求,最后返回的是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的元素的更多相关文章

  1. Leetcode#169. Majority Element(求众数)

    题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  2. Week1 - 169.Majority Element

    这周刚开始讲了一点Divide-and-Conquer的算法,于是这周的作业就选择在LeetCode上找分治法相关的题目来做. 169.Majority Element Given an array ...

  3. 169. Majority Element - LeetCode

    Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排 ...

  4. 169. Majority Element(C++)

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  5. 23. leetcode 169. Majority Element

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  6. 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 ...

  7. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  8. [LeetCode] 169. Majority Element 多数元素

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  9. ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java

    Given an array of size n, find the majority element. The majority element is the element that appear ...

随机推荐

  1. Spark的CombineByKey

    combineBykey关键是要明白里面的三个函数: 1. 当某个key第一次出现的时候,走的是第一个函数(createCombin):A function that creates a combin ...

  2. kubernetes简单示例

    1. 安装 yum install -y etcd kubernetes 2. 启动 systemctl start etcd systemctl start docker systemctl sta ...

  3. 基于Oracle的EntityFramework的WEBAPI2的实现(四)——自动生成在线帮助文档

    打开我们项目中的Area文件夹,正常情况下,我们会发现已经有了一个名字叫做[HelpPage]的区域(Area),这个区域是vs帮助我们自动建立的,它是一个mvc(不是webapi),它有普通的Con ...

  4. celery制作定时任务

    celery参考地址:http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#starting-the-schedu ...

  5. Unit01: Servlet基础 、 HTTP协议

    Unit01: Servlet基础 . HTTP协议 在页面上输出当前时间 package web; import java.io.IOException; import java.io.PrintW ...

  6. Java的八种基本数据类型及其包装类

    Java有八种基本数据类型,所谓基本类型就是说存储时仅存在栈中,那么与之相对就是引用类型,引用类型既存在栈里又存在堆里,栈内存放堆内地址. 八种基本类型分别为byte short int long f ...

  7. Linux系统命令与脚本开发

    系统命令 # cat EFO cat >> file << EOF neirong EOF # 清空 >file 清空文件 [root@Poppy conf]# sed ...

  8. form中的input的redonly和disable区别

    Readonly和Disabled是用在表单中的两个属性,它们都能够做到使用户不能够更改表单域中的内容.但是它们之间有着微小的差别,总结如下: Readonly只针对input(text / pass ...

  9. ceph---luminous版的安装

    前言 ceph luminous版本新增加了很多有意思的功能,这个也是一个长期支持版本,所以这些新功能的特性还是很值得期待的,从底层的存储改造,消息方式的改变,以及一些之前未实现的功能的完成,都让ce ...

  10. Win8电源选项中没有休眠这一项如何让Win8也能够休眠

    我们都知道,Win8默认的电源选项中是没有休眠这一选项的,即使用Alt+F4打开关闭Windows选项窗口也看不到”休眠“.难道Win8就不能够休眠了吗?答案当然不是,我们只要进行一些设置就能让Win ...