|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/** |
4 |
| - * 169. Majority Element |
5 |
| -
|
6 |
| - Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. |
7 |
| - You may assume that the array is non-empty and the majority element always exist in the array. |
8 |
| -
|
9 |
| - Example 1: |
10 |
| - Input: [3,2,3] |
11 |
| - Output: 3 |
12 |
| -
|
13 |
| - Example 2: |
14 |
| - Input: [2,2,1,1,1,2,2] |
15 |
| - Output: 2 |
16 |
| - */ |
17 | 3 | public class _169 {
|
18 |
| - public static class Solution1 { |
19 |
| - /**Moore Voting Algorithm |
20 |
| - * How to understand this: |
21 |
| - * 1. For a number to qualify as a majority element, it needs to occur more than 1/2 times, which |
22 |
| - * means there are a max of only one such element in any given array. |
23 |
| - * 2. E.g. given this array [1,2,3,1,1,1], two of the 1s will be balanced off by 2 and 3, still there are two 1s remaining in the end |
24 |
| - * which is the majority element*/ |
25 |
| - public int majorityElement(int[] nums) { |
26 |
| - int count = 1; |
27 |
| - int majority = nums[0]; |
28 |
| - for (int i = 1; i < nums.length; i++) { |
29 |
| - if (count == 0) { |
30 |
| - count++; |
31 |
| - majority = nums[i]; |
32 |
| - } else if (nums[i] == majority) { |
33 |
| - count++; |
34 |
| - } else { |
35 |
| - count--; |
| 4 | + public static class Solution1 { |
| 5 | + /** |
| 6 | + * Moore Voting Algorithm |
| 7 | + * How to understand this: |
| 8 | + * 1. For a number to qualify as a majority element, it needs to occur more than 1/2 times, which |
| 9 | + * means there are a max of only one such element in any given array. |
| 10 | + * 2. E.g. given this array [1,2,3,1,1,1], two of the 1s will be balanced off by 2 and 3, still there are two 1s remaining in the end |
| 11 | + * which is the majority element |
| 12 | + */ |
| 13 | + public int majorityElement(int[] nums) { |
| 14 | + int count = 1; |
| 15 | + int majority = nums[0]; |
| 16 | + for (int i = 1; i < nums.length; i++) { |
| 17 | + if (count == 0) { |
| 18 | + count++; |
| 19 | + majority = nums[i]; |
| 20 | + } else if (nums[i] == majority) { |
| 21 | + count++; |
| 22 | + } else { |
| 23 | + count--; |
| 24 | + } |
| 25 | + } |
| 26 | + return majority; |
36 | 27 | }
|
37 |
| - } |
38 |
| - return majority; |
39 | 28 | }
|
40 |
| - } |
41 | 29 |
|
42 |
| - public static class Solution2 { |
43 |
| - //bit manipulation |
44 |
| - public int majorityElement(int[] nums) { |
45 |
| - int[] bit = new int[32];//because an integer is 32 bits, so we use an array of 32 long |
46 |
| - for (int num : nums) { |
47 |
| - for (int i = 0; i < 32; i++) { |
48 |
| - if ((num >> (31 - i) & 1) == 1) { |
49 |
| - bit[i]++;//this is to compute each number's ones frequency |
50 |
| - } |
| 30 | + public static class Solution2 { |
| 31 | + //bit manipulation |
| 32 | + public int majorityElement(int[] nums) { |
| 33 | + int[] bit = new int[32];//because an integer is 32 bits, so we use an array of 32 long |
| 34 | + for (int num : nums) { |
| 35 | + for (int i = 0; i < 32; i++) { |
| 36 | + if ((num >> (31 - i) & 1) == 1) { |
| 37 | + bit[i]++;//this is to compute each number's ones frequency |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + int res = 0; |
| 42 | + //this below for loop is to construct the majority element: since every bit of this element would have appeared more than n/2 times |
| 43 | + for (int i = 0; i < 32; i++) { |
| 44 | + bit[i] = bit[i] > nums.length / 2 ? 1 |
| 45 | + : 0;//we get rid of those that bits that are not part of the majority number |
| 46 | + res += bit[i] * (1 << (31 - i)); |
| 47 | + } |
| 48 | + return res; |
51 | 49 | }
|
52 |
| - } |
53 |
| - int res = 0; |
54 |
| - //this below for loop is to construct the majority element: since every bit of this element would have appeared more than n/2 times |
55 |
| - for (int i = 0; i < 32; i++) { |
56 |
| - bit[i] = bit[i] > nums.length / 2 ? 1 |
57 |
| - : 0;//we get rid of those that bits that are not part of the majority number |
58 |
| - res += bit[i] * (1 << (31 - i)); |
59 |
| - } |
60 |
| - return res; |
61 | 50 | }
|
62 |
| - } |
63 | 51 | }
|
0 commit comments