From 2ae22d9100f326f00fc29ca7bba6091b32b3eb48 Mon Sep 17 00:00:00 2001 From: "wei.he" Date: Sat, 11 May 2019 22:25:26 +0800 Subject: [PATCH] update LeetCode_169_053 & LeetCode_746_053 --- Week_04/id_53/LeetCode_169_053.java | 32 +++++++++++++++++++++++++++++ Week_04/id_53/LeetCode_746_053.java | 25 ++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 Week_04/id_53/LeetCode_169_053.java create mode 100644 Week_04/id_53/LeetCode_746_053.java diff --git a/Week_04/id_53/LeetCode_169_053.java b/Week_04/id_53/LeetCode_169_053.java new file mode 100644 index 00000000..ea3bd44a --- /dev/null +++ b/Week_04/id_53/LeetCode_169_053.java @@ -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 map = new HashMap<>(); + 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)); + } +} diff --git a/Week_04/id_53/LeetCode_746_053.java b/Week_04/id_53/LeetCode_746_053.java new file mode 100644 index 00000000..e8c79ef3 --- /dev/null +++ b/Week_04/id_53/LeetCode_746_053.java @@ -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]; + } +}