-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update solutions to lc/lcof/lcci problem: Majority Element
- Loading branch information
Showing
22 changed files
with
573 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution { | ||
public: | ||
int majorityElement(vector<int>& nums) { | ||
int cnt = 0, major = 0; | ||
for (int num : nums) { | ||
if (cnt == 0) { | ||
major = num; | ||
cnt = 1; | ||
} else { | ||
cnt += (major == num ? 1 : -1); | ||
} | ||
} | ||
return major; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
public class Solution { | ||
public int MajorityElement(int[] nums) { | ||
int cnt = 0, major = 0; | ||
foreach (int num in nums) | ||
{ | ||
if (cnt == 0) | ||
{ | ||
major = num; | ||
cnt = 1; | ||
} | ||
else | ||
{ | ||
cnt += (major == num ? 1 : -1); | ||
} | ||
} | ||
return major; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
func majorityElement(nums []int) int { | ||
var cnt, major int | ||
for _, num := range nums { | ||
if cnt == 0 { | ||
major = num | ||
cnt = 1 | ||
} else { | ||
if major == num { | ||
cnt++ | ||
} else { | ||
cnt-- | ||
} | ||
} | ||
} | ||
return major | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Solution { | ||
public int majorityElement(int[] nums) { | ||
int cnt = 0, major = 0; | ||
for (int num : nums) { | ||
if (cnt == 0) { | ||
major = num; | ||
cnt = 1; | ||
} else { | ||
cnt += (major == num ? 1 : -1); | ||
} | ||
} | ||
return major; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Solution: | ||
def majorityElement(self, nums: List[int]) -> int: | ||
cnt = major = 0 | ||
for num in nums: | ||
if cnt == 0: | ||
major = num | ||
cnt = 1 | ||
else: | ||
cnt += (1 if major == num else -1) | ||
return major |
Oops, something went wrong.