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

Commitf6160ab

Browse files
author
applewjg
committed
Update
Change-Id: Ie26edc88f441daae6d437a4f6d2443b190de8f51
1 parentc3d3fb0 commitf6160ab

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

‎MajorityElement.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,51 @@
11
/*
22
Author: King, wangjingui@outlook.com
3-
Date: Dec14, 2014
4-
Problem:ZigZag Conversion
3+
Date: Dec20, 2014
4+
Problem:Majority Element
55
Difficulty: Easy
66
Source: https://oj.leetcode.com/problems/majority-element/
77
Notes:
88
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
99
1010
You may assume that the array is non-empty and the majority element always exist in the array.
1111
12-
Solution: Runtime: O(n) — Moore voting algorithm: We maintain a current candidate and a counter initialized to 0. As we iterate the array, we look at the current element x:
12+
Solution:1.Runtime: O(n) — Moore voting algorithm: We maintain a current candidate and a counter initialized to 0. As we iterate the array, we look at the current element x:
1313
If the counter is 0, we set the current candidate to x and the counter to 1.
1414
If the counter is not 0, we increment or decrement the counter based on whether x is the current candidate.
1515
After one pass, the current candidate is the majority element. Runtime complexity = O(n).
16+
2. Runtime: O(n) — Bit manipulation: We would need 32 iterations, each calculating the number of 1's for the ith bit of all n numbers. Since a majority must exist, therefore, either count of 1's > count of 0's or vice versa (but can never be equal). The majority number’s ith bit must be the one bit that has the greater count.
1617
*/
18+
1719
publicclassSolution {
18-
publicintmajorityElement(int[]num) {
20+
publicintmajorityElement_1(int[]num) {
1921
intn =num.length;
2022
if (n ==0)return0;
2123
if (n ==1)returnnum[0];
22-
intcur =num[0],cnt =1;
24+
intres =num[0],cnt =1;
2325
for (inti =1;i <n; ++i) {
2426
if (cnt ==0) {
25-
cur =num[i];
27+
res =num[i];
2628
++cnt;
2729
continue;
2830
}
29-
if (cur ==num[i]) ++cnt;
31+
if (res ==num[i]) ++cnt;
3032
else --cnt;
3133
}
32-
returncur;
34+
returnres;
35+
}
36+
publicintmajorityElement_2(int[]num) {
37+
intn =num.length;
38+
if (n ==0)return0;
39+
if (n ==1)returnnum[0];
40+
intres =0;
41+
for (inti =0;i <32; ++i) {
42+
intone =0,zero =0;
43+
for (intj =0;j <n; ++j) {
44+
if (((num[j]>>i) &1) ==1) ++one;
45+
else ++zero;
46+
}
47+
if (one >zero)res =res | (1<<i);
48+
}
49+
returnres;
3350
}
3451
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp