[抄题]:

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. 试玩swoole扩展 第一天

    安装 pecl install swoole 部分过程: configure: creating ./config.statusconfig.status: creating config.hrunn ...

  2. JVM内存管理之GC算法精解(复制算法与标记/整理算法)

    本次LZ和各位分享GC最后两种算法,复制算法以及标记/整理算法.上一章在讲解标记/清除算法时已经提到过,这两种算法都是在此基础上演化而来的,究竟这两种算法优化了之前标记/清除算法的哪些问题呢? 复制算 ...

  3. 在centOS5.9安装mysql

    网上的信息实在是太乱了,好多出了错的,我这个是自己亲自配置,其实就简简单单的几步:如果你的系统里有以前遗留的文件,用rm -rf文件名删除掉 1.安装MySQL客服端和服务器端             ...

  4. Linux常用命令英文缩写

    命令缩写: ls:list(列出目录内容) cd:Change Directory(改变目录) su:switch user 切换用户      (ubuntu 使用管理员权限 是 sudo) rpm ...

  5. PHP环境基础配置

    域名访问设置(本地局域网) 用记事本打开 127.0.0.1是本地回环地址 配置完后 通过在本地浏览器输入www.0705.com就可以访问本地站点了 Wamp集成环境多站点配置 配置条件: 一个服务 ...

  6. web前端 ajax加载动态生成复选框demo

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  7. java实现时钟

    package com.js.ai.modules.pointwall.testxfz; import java.awt.Color; import java.awt.Dimension; impor ...

  8. java后台读取配置文件中key与value -----demo

    public class ResourcesUtils { /* * @description:根据属性获取文件名 * * @param:propertyName文件的属性名 * * @return: ...

  9. mysql导出文本文件,加分隔符

    从mysql导出,再导入到oracle #!/bin/sh cd /u03/tools/machine_info rm -f data/machine_info.txt mysql -u用户名 -p密 ...

  10. 概述XML

    xml概述--->干什么的 存储一对多的数据 作为配置文件存储数据 xml组成---->怎么用 元素的分类 包含标签体的标签(有开始标签和结束标签) 例如: <student> ...