博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Majority Element
阅读量:4075 次
发布时间:2019-05-25

本文共 925 字,大约阅读时间需要 3 分钟。

Majority Element

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.

Credits:
Special thanks to  for adding this problem and creating all test cases.

Java代码:

public class Solution {    public int majorityElement(int[] num) {        Map map = new HashMap
(); int tmp = 0; for (int i = 0; i < num.length; i++) { if (!map.containsKey(num[i])) { map.put(num[i], 1); } else { tmp = (Integer) map.get(num[i]); tmp = tmp + 1; map.put(num[i], tmp); } } tmp = 0; int val = 0; int key = 0; Iterator ite = map.entrySet().iterator(); while (ite.hasNext()) { Map.Entry entry = (Map.Entry) ite.next(); val = (Integer) entry.getValue(); if (val > tmp) { tmp = val; key = (Integer) entry.getKey(); } } return key; }}

转载地址:http://znuni.baihongyu.com/

你可能感兴趣的文章
Unix + OS IBM Aix FTP / wu-ftp / proftp
查看>>
my read work
查看>>
db db2 base / instance database tablespace container
查看>>
hd disk / disk raid / disk io / iops / iostat / iowait / iotop / iometer
查看>>
project ASP.NET
查看>>
db db2_monitorTool IBM Rational Performace Tester
查看>>
OS + Unix Aix telnet
查看>>
IBM Lotus
查看>>
Linux +Win LAMPP Tools XAMPP 1.7.3 / 5.6.3
查看>>
my read_university
查看>>
network manager
查看>>
OS + Linux Disk disk lvm / disk partition / disk mount / disk io
查看>>
RedHat + OS CPU、MEM、DISK
查看>>
net TCP/IP / TIME_WAIT / tcpip / iperf / cain
查看>>
webServer kzserver/1.0.0
查看>>
OS + Unix IBM Aix basic / topas / nmon / filemon / vmstat / iostat / sysstat/sar
查看>>
my ReadMap subway / metro / map / ditie / gaotie / traffic / jiaotong
查看>>
OS + Linux DNS Server Bind
查看>>
linux下安装django
查看>>
Android 解决TextView设置文本和富文本SpannableString自动换行留空白问题
查看>>