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.

Example 1:

Input: [3,2,3]
Output: 3

Example 2:

Input: [2,2,1,1,1,2,2]
Output: 2
class Solution {
public int majorityElement(int[] nums) {
int tmp = nums[0];
int count = 0;
for (int num : nums) {
if (count == 0) {
tmp = num;
} if (num == tmp) {
count += 1;
} else {
count -= 1;
}
}
return tmp;
}
}

[LC] 169. Majority Element的更多相关文章

  1. 169. Majority Element(C++)

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

  2. 23. leetcode 169. Majority Element

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

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

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

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

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

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

  6. Week1 - 169.Majority Element

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

  7. 169. Majority Element - LeetCode

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

  8. 169. Majority Element 出现次数超过n/2的元素

    [抄题]: Given an array of size n, find the majority element. The majority element is the element that ...

  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. Python—在Django中使用Celery

    一.Django中的请求 Django Web中从一个http请求发起,到获得响应返回html页面的流程大致如下: http请求发起 经过中间件 http handling(request解析) ur ...

  2. 将微服务注册到Euraka

    1.添加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...

  3. sqlserver2008的sql语句支持的最大长度

    想写一个sql语句,很长,主要是in后跟着无数个用户ID,(虽然实现方式很低级,但是还是凑合着用吧) 不知道sql最大长度是多少,看了 SQL Server 的最大容量规范,写的是 包含 SQL 语句 ...

  4. 在WSL Ubuntu1804中安装Docker

    一.系统环境 1.1 环境准备: Windows10 企业版 1909 Docker for Windows WSL Ubuntu1804 1.2 下载安装 Docker for Windows 1. ...

  5. MQL4编程—值传递和引用传递

    定义 简单的说就是给一个变量或者对象取一个别名(引用和被引用的共享存储单元,要用修饰符&). 引用的用法 在MQL4中通过调试只接受引用作为函数参数的用法,在函数参数传递过程中,有两种传递方式 ...

  6. jq监控滑动

    $(window).scroll(function () { if ($(window).scrollTop() == $(document).height() - $(window).height( ...

  7. FJ的字符串-简单递归

    FJ的字符串-简单递归 问题描述FJ在沙盘上写了这样一些字符串: A1 = “A” A2 = “ABA” A3 = “ABACABA” A4 = “ABACABADABACABA” … … 你能找出其 ...

  8. UVA 10273

    我是用暴力过的,虽然网上说刘汝佳出的这道题考的是堆,我不太懂,..用暴力时间复杂度高一些,但是一样能过 所要注意的就是周期问题,因为只要同时存在某一天超过一头牛产奶量最小,就不会杀牛,而每头牛的周期和 ...

  9. 吴裕雄--天生自然TensorFlow高层封装:Estimator-自定义模型

    # 1. 自定义模型并训练. import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist i ...

  10. [PHP防火墙]输入内容存在危险字符,安全起见,已被本站拦截

    之前在很多的网站都看到了360webscan的攻击拦截脚本,正好分析并学习一下. 下载地址:http ://webscan.360.cn/protect/down?domain = blog.dybo ...