Skip to content

Commit 9264e3e

Browse files
refactor 169
1 parent 598de6d commit 9264e3e

File tree

1 file changed

+66
-55
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+66
-55
lines changed

src/main/java/com/fishercoder/solutions/_169.java

Lines changed: 66 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,83 @@
44
import java.util.HashMap;
55
import java.util.Map;
66

7-
/**169. Majority Element
8-
*
9-
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
7+
/**
8+
* 169. Majority Element
109
11-
You may assume that the array is non-empty and the majority element always exist in the array.
10+
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
1211
13-
*/
12+
You may assume that the array is non-empty and the majority element always exist in the array.
13+
14+
Example 1:
15+
16+
Input: [3,2,3]
17+
Output: 3
18+
19+
Example 2:
20+
21+
Input: [2,2,1,1,1,2,2]
22+
Output: 2
23+
*/
1424
public class _169 {
15-
public static class Solution1 {
16-
// Moore Voting Algorithm
17-
public int majorityElement(int[] nums) {
18-
int count = 1;
19-
int majority = nums[0];
20-
for (int i = 1; i < nums.length; i++) {
21-
if (count == 0) {
22-
count++;
23-
majority = nums[i];
24-
} else if (nums[i] == majority) {
25-
count++;
26-
} else {
27-
count--;
28-
}
29-
}
30-
return majority;
25+
public static class Solution1 {
26+
/**Moore Voting Algorithm*/
27+
public int majorityElement(int[] nums) {
28+
int count = 1;
29+
int majority = nums[0];
30+
for (int i = 1; i < nums.length; i++) {
31+
if (count == 0) {
32+
count++;
33+
majority = nums[i];
34+
} else if (nums[i] == majority) {
35+
count++;
36+
} else {
37+
count--;
3138
}
39+
}
40+
return majority;
3241
}
42+
}
3343

34-
public static class Solution2 {
35-
public int majorityElement(int[] nums) {
36-
Map<Integer, Integer> map = new HashMap();
37-
for (int i : nums) {
38-
map.put(i, map.getOrDefault(i, 0) + 1);
39-
if (map.get(i) > nums.length / 2) {
40-
return i;
41-
}
42-
}
43-
return -1;
44+
public static class Solution2 {
45+
public int majorityElement(int[] nums) {
46+
Map<Integer, Integer> map = new HashMap();
47+
for (int i : nums) {
48+
map.put(i, map.getOrDefault(i, 0) + 1);
49+
if (map.get(i) > nums.length / 2) {
50+
return i;
4451
}
52+
}
53+
return -1;
4554
}
55+
}
4656

47-
public static class Solution3 {
48-
//This is O(nlogn) time.
49-
public int majorityElement(int[] nums) {
50-
Arrays.sort(nums);
51-
return nums[nums.length / 2];
52-
}
57+
public static class Solution3 {
58+
//This is O(nlogn) time.
59+
public int majorityElement(int[] nums) {
60+
Arrays.sort(nums);
61+
return nums[nums.length / 2];
5362
}
63+
}
5464

55-
public static class Solution4 {
56-
//bit manipulation
57-
public int majorityElement(int[] nums) {
58-
int[] bit = new int[32];//because an integer is 32 bits, so we use an array of 32 long
59-
for (int num : nums) {
60-
for (int i = 0; i < 32; i++) {
61-
if ((num >> (31 - i) & 1) == 1) {
62-
bit[i]++;//this is to compute each number's ones frequency
63-
}
64-
}
65-
}
66-
int res = 0;
67-
//this below for loop is to construct the majority element: since every bit of this element would have appeared more than n/2 times
68-
for (int i = 0; i < 32; i++) {
69-
bit[i] = bit[i] > nums.length / 2 ? 1 : 0;//we get rid of those that bits that are not part of the majority number
70-
res += bit[i] * (1 << (31 - i));
71-
}
72-
return res;
65+
public static class Solution4 {
66+
//bit manipulation
67+
public int majorityElement(int[] nums) {
68+
int[] bit = new int[32];//because an integer is 32 bits, so we use an array of 32 long
69+
for (int num : nums) {
70+
for (int i = 0; i < 32; i++) {
71+
if ((num >> (31 - i) & 1) == 1) {
72+
bit[i]++;//this is to compute each number's ones frequency
73+
}
7374
}
75+
}
76+
int res = 0;
77+
//this below for loop is to construct the majority element: since every bit of this element would have appeared more than n/2 times
78+
for (int i = 0; i < 32; i++) {
79+
bit[i] = bit[i] > nums.length / 2 ? 1
80+
: 0;//we get rid of those that bits that are not part of the majority number
81+
res += bit[i] * (1 << (31 - i));
82+
}
83+
return res;
7484
}
85+
}
7586
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy