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.

Hide Tags: Divide and Conquer Array Bit Manipulation

解题思路:

(1)使用HashMap。Map的特点:不同意反复元素,因此在存储前须要推断是否存在

(2)推断HashMap中存在nums[i],假设存在。使用hm.get(nums[i])获取value,即通过key来获得value值,即count(出现的次数)

(3)假设count大于数组长度的一般。即返回该元素

(4)假设count不满足条件,向HashMap存储元素以及出现的次数。

代码例如以下:

	public static int majorityElement(int[] nums)
{
/*
* Map的特点:不同意反复元素。因此在存储前须要推断是否存在
*/
Map<Integer, Integer> hm=new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++)
{
int count=0;
//推断HashMap中存在nums[i],假设存在,使用hm.get(nums[i])获取value
//即通过key来获得value值,即count(出现的次数)
if (hm.containsKey(nums[i]))
{
count=hm.get(nums[i])+1;
}
else
{
count=1;
}
//假设count大于数组长度的一般,即返回该元素
if (count>nums.length/2)
{
return nums[i];
}
//向HashMap存储元素以及出现的次数
hm.put(nums[i], count);
}
return 0; }

leetcode——169 Majority Element(数组中出现次数过半的元素)的更多相关文章

  1. 剑指Offer:找出数组中出现次数超过一半的元素

    题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include <stdio.h> int ha ...

  2. LINQ 获取当前数组中出现次数最多的元素

    LINQ 获取当前数组中出现次数最多的元素 1  List<string> a = new List<string>();              a.Add(        ...

  3. python查找数组中出现次数最多的元素

    方法1-np.argmax(np.bincount()) 看一个例子 array = [0,1,2,2,3,4,4,4,5,6] print(np.bincount(array)) print(np. ...

  4. 23. leetcode 169. Majority Element

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

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

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

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

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

  7. LeetCode 169 Majority Element 解题报告

    题目要求 Given an array of size n, find the majority element. The majority element is the element that a ...

  8. LeetCode 169. Majority Element解题方法

    题目: Given an array of size n, find the majority element. The majority element is the element that ap ...

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

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

随机推荐

  1. python垃圾回收三之标记清除

    #第一组循环引用# a = [1,2] b = [3,4] a.append(b) b.append(a) del a ## #第二组循环引用# c = [4,5] d = [5,6] c.appen ...

  2. Paint House

    There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...

  3. rank over partition by

    高级函数,分组排序 over: 在什么条件之上. partition by e.deptno: 按部门编号划分(分区). order by e.sal desc: 按工资从高到低排序(使用rank() ...

  4. 【Unity_UWP】Unity 工程发布win10 UWP 时的本地文件读取 (下篇)

    Universal Windows Platform(UWP)是微软Windows10专用的通用应用平台,其目的在于在统一操作系统下控制所有智能电子设备. 自从Unity 5.2之后,配合VS 201 ...

  5. 2018ACM/ICPC 青岛现场赛 E题 Plants vs. Zombies

    题意: 你的房子在0点,1,2,3,...,n(n<=1e5)点每个点都有一颗高度为0的花,浇一次水花会长a[i]. 你有一个机器人刚开始在你家,最多走m步,每一步只能往前走或者往后走,每走到一 ...

  6. Mybatis 接口传入多个参数 xml怎么接收

    mybatis 在接口上传入多个参数 1.如果传入的参数类型一样. Map<String, String> queryDkpayBindBankCidByOriBindAndBankCid ...

  7. enum 关键字

    java.lang.Enum.java enum :枚举类型当你需要创建一个整型常量集,但是这些枚举值并不会必然地将其自身的取值限制在这个常量的范围之内,这种情况可以用枚举 package objec ...

  8. docker 的简单操作

    一直说更博,但是一直在delay.... 最近一直用到docker,所以就总结一下吧! docker的介绍 Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源. ...

  9. Java编程的逻辑 (11) - 初识函数

    本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...

  10. UI 框架

    Vue.js 之 iView UI 框架 像我们平日里做惯了 Java 或者 .NET 这种后端程序员,对于前端的认识还常常停留在 jQuery 时代,包括其插件在需要时就引用一下,不需要就删除.故观 ...