Skip to content

Commit

Permalink
algo for week24
Browse files Browse the repository at this point in the history
  • Loading branch information
milley committed Sep 10, 2019
1 parent ea557aa commit 259ba47
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Algorithm、Review、Tip、Share
|98|[Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/)|[Java](./week20/algo/lc98/Solution.java)|Medium|
|182|[Duplicate Emails](https://leetcode.com/problems/duplicate-emails/)|[SQL](./week01/algo/lc182/leetcode182.sql)|Easy|
|101|[Symmetric Tree](https://leetcode.com/problems/symmetric-tree/)|[Java](./week12/algo/lc101/Solution.java)|Easy|
|169|[Majority Element](https://leetcode.com/problems/majority-element/)|[Java](./week24/algo/lc169/Solution.java)|Easy|
|226|[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/)|[Java](./week10/algo/lc226/Solution.java)|Easy|
|227|[Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/)|[Java](./week08/algo/lc227/Calculator.java)|Medium|
|232|[Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/)|[Java](./week09/algo/lc232/MyQueue.java)|Easy|
Expand Down
55 changes: 55 additions & 0 deletions week24/algo/lc169/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.milley.majorityelement;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution {
public int majorityElement(int[] nums) {
Map<Integer, Integer> majorityMap = new HashMap<>(nums.length);
for (int num : nums) {
Integer val = majorityMap.get(num);
majorityMap.put(num, val == null ? 1 : val.intValue() + 1);
}

for (Map.Entry<Integer, Integer> entry : majorityMap.entrySet()) {
if (entry.getValue() > nums.length / 2) {
return entry.getKey();
}
}
return 0;
}

public int majorityElementBySort(int[] nums) {
Arrays.sort(nums);
int eqLen = 0;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] == nums[i + 1]) {
eqLen++;
} else {
if (eqLen > nums.length / 2 - 1) {
return nums[i];
} else {
eqLen = 0;
}
}
}
return nums[eqLen];
}

public int majorityElementByMagic(int[] nums) {
Arrays.sort(nums);
return nums[nums.length / 2];
}

public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.majorityElement(new int[]{3, 2, 3}));
System.out.println(solution.majorityElement(new int[]{2, 2, 1, 1, 1, 2, 2}));
System.out.println(solution.majorityElementBySort(new int[]{3, 2, 3}));
System.out.println(solution.majorityElementBySort(new int[]{-1, 1, 1, 1, 2, 1}));
System.out.println(solution.majorityElementByMagic(new int[]{3, 2, 3}));
System.out.println(solution.majorityElementByMagic(new int[]{-1, 1, 1, 1, 2, 1}));
}
}

0 comments on commit 259ba47

Please sign in to comment.