|
12 | 12 |
|
13 | 13 | */ |
14 | 14 | public class _169 { |
15 | | - |
16 | | - public int majorityElement_bit_manipulation(int[] nums){ |
| 15 | + |
| 16 | + public int majorityElement_bit_manipulation(int[] nums) { |
17 | 17 | int[] bit = new int[32];//because an integer is 32 bits, so we use an array of 32 long |
18 | | - for(int num : nums){ |
19 | | - for(int i = 0; i < 32; i++){ |
20 | | - if((num >> (31-i) & 1) == 1) bit[i]++;//this is to compute each number's ones frequency |
| 18 | + for (int num : nums) { |
| 19 | + for (int i = 0; i < 32; i++) { |
| 20 | + if ((num >> (31 - i) & 1) == 1) bit[i]++;//this is to compute each number's ones frequency |
21 | 21 | } |
22 | 22 | } |
23 | 23 | int res = 0; |
24 | 24 | //this below for loop is to construct the majority element: since every bit of this element would have appeared more than n/2 times |
25 | | - for(int i = 0; i < 32; i++){ |
26 | | - bit[i] = bit[i] > nums.length/2 ? 1 : 0;//we get rid of those that bits that are not part of the majority number |
27 | | - res += bit[i]*(1 << (31-i)); |
| 25 | + for (int i = 0; i < 32; i++) { |
| 26 | + bit[i] = bit[i] > nums.length / 2 ? 1 : 0;//we get rid of those that bits that are not part of the majority number |
| 27 | + res += bit[i] * (1 << (31 - i)); |
28 | 28 | } |
29 | 29 | return res; |
30 | 30 | } |
31 | | - |
| 31 | + |
32 | 32 | //saw a really clever solution on Discuss, though it didn't use bit manipulatoin |
33 | 33 | //this is actually applying a famous algorithm called Moore Voting algorithm: http://www.cs.utexas.edu/~moore/best-ideas/mjrty/example.html |
34 | | - public int majorityElement_moore_voting_algorithm(int[] nums){ |
| 34 | + public int majorityElement_moore_voting_algorithm(int[] nums) { |
35 | 35 | int count = 1, majority = nums[0]; |
36 | | - for(int i = 1; i < nums.length; i++){ |
37 | | - if(count == 0){ |
| 36 | + for (int i = 1; i < nums.length; i++) { |
| 37 | + if (count == 0) { |
38 | 38 | count++; |
39 | 39 | majority = nums[i]; |
40 | | - } else if(nums[i] == majority){ |
| 40 | + } else if (nums[i] == majority) { |
41 | 41 | count++; |
42 | 42 | } else count--; |
43 | 43 | } |
44 | 44 | return majority; |
45 | 45 | } |
46 | | - |
47 | | - public static void main(String...strings){ |
48 | | - int[] nums = new int[]{1,2,3,4,2,3,2,2,4,2}; |
| 46 | + |
| 47 | + public static void main(String... strings) { |
| 48 | + int[] nums = new int[]{1, 2, 3, 4, 2, 3, 2, 2, 4, 2}; |
49 | 49 | _169 test = new _169(); |
50 | 50 | System.out.println(test.majorityElement_bit_manipulation(nums)); |
51 | 51 | } |
52 | | - |
| 52 | + |
53 | 53 | //my natural idea is to either compute the frequency of each unique number or sort it and return the median, I can hardly think of |
54 | 54 | //how bit manipulation could come into play for this question |
55 | 55 | //this is O(n) time. |
56 | 56 | public int majorityElement_compute_frequency(int[] nums) { |
57 | 57 | Map<Integer, Integer> map = new HashMap(); |
58 | | - for(int i : nums){ |
| 58 | + for (int i : nums) { |
59 | 59 | map.put(i, map.getOrDefault(i, 0) + 1); |
60 | | - if(map.get(i) > nums.length/2) return i; |
| 60 | + if (map.get(i) > nums.length / 2) return i; |
61 | 61 | } |
62 | 62 | return -1; |
63 | 63 | } |
64 | | - |
| 64 | + |
65 | 65 | //This is O(nlogn) time. |
66 | 66 | public int majorityElement_sort(int[] nums) { |
67 | 67 | Arrays.sort(nums); |
68 | | - return nums[nums.length/2]; |
| 68 | + return nums[nums.length / 2]; |
69 | 69 | } |
70 | 70 | } |
0 commit comments