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]distributed 1.21.8 requires msgpack, which is not installed.

    个人博客原文地址:http://www.bearoom.xyz/2019/08/24/python-devolop-en-msgpack-problem/ 在安装tensorflow的时候,出现了这个 ...

  2. PAT Advanced 1132 Cut Integer (20) [数学问题-简单数学]

    题目 Cutting an integer means to cut a K digits long integer Z into two integers of (K/2) digits long ...

  3. UI Automation编程辅助工具Inspect的下载和使用

    UIAutomation微软提供的UI自动化库,主要用AutomationElement类来表示UI 自动化目录树中的一个UI自动化元素,.NET Windows的窗体应用程序和WPF应用程序. In ...

  4. springBoot中mybatis错误之 Property 'configuration' and 'configLocation' can not specified with together 解决

    mybatis.config-location与mybatis.config-locations不同 mybatis.config-location不加载全局配置文件

  5. Springboot中RestTemplate -- 用更优雅的方式发HTTP请求

    RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率. 我之前的HTTP开发是用ap ...

  6. Java复习(二)类与对象的基本概念

    2.1面向对象的程序设计方法概述 对象 程序中: 一切皆是对象 都具有标识,属性和行为 通过一个或多个变量来保存其状态 通过方法实现他的行为 类 将属性及行为相同或相似的对象归为一类 类可以看成是对象 ...

  7. Matlab高级教程_第二篇:Matlab相见恨晚的模块_02_并行运算-利用GPU并行执行MATLAB程序

    1 MATLAB原文: 如果所有你想使用的函数支持GPU,你能够使用gpuArray把输入的数据传输到GPU,也能够唤起gather命令把传输值GPU的数据回收. 2 通过gpuDevice命令观察当 ...

  8. CentOS下图形界面安装_Orcaale 11g

    1.安装说明 使用到的工具: 软件名称 版本 软件包 系统 centOS6.8 CentOS-6.8-x86_64-bin-DVD1.iso 数据库 ORACLE11g linux.x64_11gR2 ...

  9. 关于css中hover下拉框的一个bug

    写hover下拉框的时候会遇到一个奇怪的bug,就是下拉框下来的时候会被所在位置的div遮挡,哪怕下拉框使用的absolute,也会被遮挡. 如图: 这个语言选择的下拉框会被下面的div挡住(截图是已 ...

  10. ionic3 设置状态栏背景颜色

    this.statusBar.styleDefault(); this.statusBar.overlaysWebView(true); this.statusBar.backgroundColorB ...