Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7bda7f1

Browse files
majority element
1 parent086f35e commit7bda7f1

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

‎Common/src/utils/CommonUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ public class CommonUtils {
1111
privatestaticfinalintDEFAULT_TREE_SIZE =10;
1212
privatestaticfinalintDEFAULT_UPPER_BOUND =100;
1313

14+
//How to make a method generic: declare <T> in its method signature
15+
publicstatic <T>voidprintArray_generic_type(T[]nums) {
16+
for(Ti :nums){
17+
System.out.print(i +", ");
18+
}
19+
System.out.println();
20+
}
21+
22+
publicstaticvoidmain(String...strings){
23+
Integer[]nums =newInteger[]{1,2,3,4,5};
24+
printArray_generic_type(nums);
25+
}
26+
1427
publicstaticvoidprintArray(int[]nums) {
1528
for(inti :nums){
1629
System.out.print(i +", ");

‎EASY/src/easy/MajorityElement.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
packageeasy;
2+
3+
importjava.util.Arrays;
4+
importjava.util.HashMap;
5+
importjava.util.Map;
6+
7+
importutils.CommonUtils;
8+
9+
/**169. Majority Element QuestionEditorial Solution My Submissions
10+
Total Accepted: 132691
11+
Total Submissions: 309653
12+
Difficulty: Easy
13+
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
14+
15+
You may assume that the array is non-empty and the majority element always exist in the array.
16+
17+
*/
18+
publicclassMajorityElement {
19+
20+
publicintmajorityElement_bit_manipulation(int[]nums){
21+
int[]bit =newint[32];//because an integer is 32 bits, so we use an array of 32 long
22+
for(intnum :nums){
23+
for(inti =0;i <32;i++){
24+
if((num >> (31-i) &1) ==1)bit[i]++;//this is to compute each number's ones frequency
25+
}
26+
}
27+
intres =0;
28+
//this below for loop is to construct the majority element: since every bit of this element would have appeared more than n/2 times
29+
for(inti =0;i <32;i++){
30+
bit[i] =bit[i] >nums.length/2 ?1 :0;//we get rid of those that bits that are not part of the majority number
31+
res +=bit[i]*(1 << (31-i));
32+
}
33+
returnres;
34+
}
35+
36+
//saw a really clever solution on Discuss, though it didn't use bit manipulatoin
37+
//this is actually applying a famous algorithm called Moore Voting algorithm: http://www.cs.utexas.edu/~moore/best-ideas/mjrty/example.html
38+
publicintmajorityElement_moore_voting_algorithm(int[]nums){
39+
intcount =1,majority =nums[0];
40+
for(inti =1;i <nums.length;i++){
41+
if(count ==0){
42+
count++;
43+
majority =nums[i];
44+
}elseif(nums[i] ==majority){
45+
count++;
46+
}elsecount--;
47+
}
48+
returnmajority;
49+
}
50+
51+
publicstaticvoidmain(String...strings){
52+
int[]nums =newint[]{1,2,3,4,2,3,2,2,4,2};
53+
MajorityElementtest =newMajorityElement();
54+
System.out.println(test.majorityElement_bit_manipulation(nums));
55+
}
56+
57+
//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
58+
//how bit manipulation could come into play for this question
59+
//this is O(n) time.
60+
publicintmajorityElement_compute_frequency(int[]nums) {
61+
Map<Integer,Integer>map =newHashMap();
62+
for(inti :nums){
63+
map.put(i,map.getOrDefault(i,0) +1);
64+
if(map.get(i) >nums.length/2)returni;
65+
}
66+
return -1;
67+
}
68+
69+
//This is O(nlogn) time.
70+
publicintmajorityElement_sort(int[]nums) {
71+
Arrays.sort(nums);
72+
returnnums[nums.length/2];
73+
}
74+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp