169. Majority Element

Easy

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
package leetcode.easy;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random; public class MajorityElement {
@org.junit.Test
public void test() {
int[] nums1 = { 3, 2, 3 };
int[] nums2 = { 2, 2, 1, 1, 1, 2, 2 };
System.out.println(majorityElement1(nums1));
System.out.println(majorityElement1(nums2));
System.out.println(majorityElement2(nums1));
System.out.println(majorityElement2(nums2));
System.out.println(majorityElement3(nums1));
System.out.println(majorityElement3(nums2));
System.out.println(majorityElement4(nums1));
System.out.println(majorityElement4(nums2));
System.out.println(majorityElement5(nums1));
System.out.println(majorityElement5(nums2));
System.out.println(majorityElement6(nums1));
System.out.println(majorityElement6(nums2));
} public int majorityElement1(int[] nums) {
int majorityCount = nums.length / 2; for (int num : nums) {
int count = 0;
for (int elem : nums) {
if (elem == num) {
count += 1;
}
} if (count > majorityCount) {
return num;
}
} return -1;
} private Map<Integer, Integer> countNums(int[] nums) {
Map<Integer, Integer> counts = new HashMap<Integer, Integer>();
for (int num : nums) {
if (!counts.containsKey(num)) {
counts.put(num, 1);
} else {
counts.put(num, counts.get(num) + 1);
}
}
return counts;
} public int majorityElement2(int[] nums) {
Map<Integer, Integer> counts = countNums(nums); Map.Entry<Integer, Integer> majorityEntry = null;
for (Map.Entry<Integer, Integer> entry : counts.entrySet()) {
if (majorityEntry == null || entry.getValue() > majorityEntry.getValue()) {
majorityEntry = entry;
}
} return majorityEntry.getKey();
} public int majorityElement3(int[] nums) {
Arrays.sort(nums);
return nums[nums.length / 2];
} private int randRange(Random rand, int min, int max) {
return rand.nextInt(max - min) + min;
} private int countOccurences(int[] nums, int num) {
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == num) {
count++;
}
}
return count;
} public int majorityElement4(int[] nums) {
Random rand = new Random(); int majorityCount = nums.length / 2; while (true) {
int candidate = nums[randRange(rand, 0, nums.length)];
if (countOccurences(nums, candidate) > majorityCount) {
return candidate;
}
}
} private int countInRange(int[] nums, int num, int lo, int hi) {
int count = 0;
for (int i = lo; i <= hi; i++) {
if (nums[i] == num) {
count++;
}
}
return count;
} private int majorityElementRec(int[] nums, int lo, int hi) {
// base case; the only element in an array of size 1 is the majority
// element.
if (lo == hi) {
return nums[lo];
} // recurse on left and right halves of this slice.
int mid = (hi - lo) / 2 + lo;
int left = majorityElementRec(nums, lo, mid);
int right = majorityElementRec(nums, mid + 1, hi); // if the two halves agree on the majority element, return it.
if (left == right) {
return left;
} // otherwise, count each element and return the "winner".
int leftCount = countInRange(nums, left, lo, hi);
int rightCount = countInRange(nums, right, lo, hi); return leftCount > rightCount ? left : right;
} public int majorityElement5(int[] nums) {
return majorityElementRec(nums, 0, nums.length - 1);
} public int majorityElement6(int[] nums) {
int count = 0;
Integer candidate = null; for (int num : nums) {
if (count == 0) {
candidate = num;
}
count += (num == candidate) ? 1 : -1;
} return candidate;
}
}

LeetCode_169. Majority Element的更多相关文章

  1. [LeetCode] Majority Element II 求众数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  2. [LeetCode] Majority Element 求众数

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

  3. 【leetcode】Majority Element

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

  4. ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java

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

  5. (Array)169. Majority Element

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

  6. LeetCode 169. Majority Element

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

  7. [UCSD白板题] Majority Element

    Problem Introduction An element of a sequence of length \(n\) is called a majority element if it app ...

  8. Leetcode # 169, 229 Majority Element I and II

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

  9. LeetCode【169. Majority Element】

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

随机推荐

  1. webpack 配置react脚手架(四):路由配置

    1. 由于 react-router 是集成了 react-router-dom 和 react-router-native的一起的,所以这里要使用的是 react-router-dom, 2. 安装 ...

  2. P2P system: Napster

    Napster structure client machines之所以叫peers是因为对于server来说这些machines是平等对待的 当你upload一首歌曲如PennyLane.mp3时, ...

  3. vscode源码编译疑难问题

    最近把原来老的源码merge到了新的1.15版本源码,以前的依赖问题会导致各种错误,Loading "gc-signals" failed啦,Error: %1 is not a ...

  4. glog的安装使用

    参考 :https://blog.csdn.net/Pig_Pig_Bang/article/details/81632962 https://blog.csdn.net/cywtd/article/ ...

  5. post提交主订单数据(gateway)实现httpapi

    models.proto syntax = "proto3"; package services; import "google/protobuf/timestamp.p ...

  6. HR#7 题解

    T1 签到题 #include<bits/stdc++.h> #define R register int using namespace std; inline int g() { R ...

  7. MySQL 语句整理 2019-5-3

    MySQL 语句整理 在整理完Oracle的一些常见用语句后,由于MySQL的语法跟Oracle略有不同,随跟PN的MySQL视频进行了间接整理. 查询薪水大于1800, 并且部门编号为20或30的员 ...

  8. jQuery相关方法9----事件相关

    一.事件冒泡和阻止事件冒泡 <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></s ...

  9. (4)打造简单OS-loader硬盘加载和C++写入文件

    0.简要说明: 我们完全可以使用bochs创建映像文件,如https://blog.csdn.net/jadeshu/article/details/89046838   ,那么为什么还去用C++去模 ...

  10. LBA逻辑块地址

    LBA简介 磁盘读取发展 IO操作读取硬盘的三种方式: chs方式 :小于8G (8064MB) LBA28方式:小于137GB LBA48方式:小于144,000,000 GB LBA方式访问使用了 ...