Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Week_04/id_53/LeetCode_169_053.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.HashMap;
import java.util.Map;

/**
* LeetCode 169. 求众数
*
* @author hewei
* @date 2019/5/11 22:03
*/
public class LeetCode_169_053 {
public static int majorityElement(int[] nums) {
Map<Integer,Integer> map = new HashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有不用 map 的解法吗?

for (int num:nums) {
if (map.containsKey(num)) {
map.put(num,map.get(num)+1);
} else {
map.put(num,1);
}
}
for (Integer key : map.keySet()) {
if (map.get(key) > nums.length/2) {
return key;
}
}
return 0;
}

public static void main(String[] args) {
int[] nums = {2,2,1,1,1,2,2};
System.out.println(majorityElement(nums));
}
}
25 changes: 25 additions & 0 deletions Week_04/id_53/LeetCode_746_053.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 746. Min Cost Climbing Stairs
*
* @author hewei
* @date 2019/5/11 22:20
*/
public class LeetCode_746_053 {
public int minCostClimbingStairs(int[] cost) {
int len = cost.length;
if(len==2) {
return Math.min(cost[0], cost[1]);
}
int [] arr = new int[len+1];
arr[0] = cost[0];
arr[1] = cost[1];
for(int i=2;i <= len;i++){
int curr = 0;
if (i != len) {
curr = cost[i];
}
arr[i] = Math.min(arr[i-2],arr[i-1])+curr;
}
return arr[len];
}
}