From 5b473408b4b16fd18b8bf9cf00718a29ca455cbc Mon Sep 17 00:00:00 2001 From: flamencoy <332863724@qq.com> Date: Sun, 30 Jun 2019 22:03:37 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E5=91=A8=E4=BD=9C=E4=B8=9A#3?= =?UTF-8?q?3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_04/id_33/Leetcode_46_33.java | 27 +++++++++++++++++++++++++++ Week_04/id_33/Leetcode_53_33.java | 17 +++++++++++++++++ Week_04/id_33/Leetcode_62_33.java | 19 +++++++++++++++++++ Week_04/id_33/Leetcode_70_33.java | 18 ++++++++++++++++++ Week_04/id_33/Leetcode_714_33.java | 21 +++++++++++++++++++++ 5 files changed, 102 insertions(+) create mode 100644 Week_04/id_33/Leetcode_46_33.java create mode 100644 Week_04/id_33/Leetcode_53_33.java create mode 100644 Week_04/id_33/Leetcode_62_33.java create mode 100644 Week_04/id_33/Leetcode_70_33.java create mode 100644 Week_04/id_33/Leetcode_714_33.java diff --git a/Week_04/id_33/Leetcode_46_33.java b/Week_04/id_33/Leetcode_46_33.java new file mode 100644 index 00000000..4da5fbd3 --- /dev/null +++ b/Week_04/id_33/Leetcode_46_33.java @@ -0,0 +1,27 @@ +/* + * @lc app=leetcode id=46 lang=java + * + * [46] Permutations + */ +class Solution { + public List> permute(int[] nums) { + List> result = new ArrayList<>(); + backTrace(nums, result, new ArrayList<>()); + return result; + } + + public void backTrace(int[] nums, List> result, List tempList){ + if(tempList.size() == nums.length){ + result.add(new ArrayList(tempList)); + return; + } + + for(int i = 0; i< nums.length; i++){ + if(tempList.contains(nums[i])) continue; + tempList.add(nums[i]); + backTrace(nums, result, tempList); + tempList.remove(tempList.size()-1); + } + } +} + diff --git a/Week_04/id_33/Leetcode_53_33.java b/Week_04/id_33/Leetcode_53_33.java new file mode 100644 index 00000000..68295e42 --- /dev/null +++ b/Week_04/id_33/Leetcode_53_33.java @@ -0,0 +1,17 @@ +/* + * @lc app=leetcode id=53 lang=java + * + * [53] Maximum Subarray + */ +class Solution { + public int maxSubArray(int[] nums) { + if(nums.length==0) return 0; + int maxSum = nums[0], sum = nums[0]; + for (int i = 1; i < nums.length; i++){ + sum = Math.max(nums[i], sum + nums[i]); + maxSum = Math.max(sum, maxSum); + } + return maxSum; + } +} + diff --git a/Week_04/id_33/Leetcode_62_33.java b/Week_04/id_33/Leetcode_62_33.java new file mode 100644 index 00000000..49a54d19 --- /dev/null +++ b/Week_04/id_33/Leetcode_62_33.java @@ -0,0 +1,19 @@ +/* + * @lc app=leetcode id=62 lang=java + * + * [62] Unique Paths + */ +class Solution { + public int uniquePaths(int m, int n) { + int[][] dp = new int[n][m]; + for (int i = 0; i < n; i++) dp[i][0] = 1; + for (int i = 0; i < m; i++) dp[0][i] = 1; + for (int i = 1; i < n; i++){ + for (int j = 1; j < m; j++){ + dp[i][j] = dp[i-1][j] + dp[i][j-1]; + } + } + return dp[n-1][m-1]; + } +} + diff --git a/Week_04/id_33/Leetcode_70_33.java b/Week_04/id_33/Leetcode_70_33.java new file mode 100644 index 00000000..64f1210b --- /dev/null +++ b/Week_04/id_33/Leetcode_70_33.java @@ -0,0 +1,18 @@ +/* + * @lc app=leetcode id=70 lang=java + * + * [70] Climbing Stairs + */ +class Solution { + public int climbStairs(int n) { + if (n == 1) return 1; + int[] dp = new int[n + 1]; + dp[1] = 1; + dp[2] = 2; + for (int i = 3; i <= n; i++) { + dp[i] = dp[i - 1] + dp[i - 2]; + } + return dp[n]; + } +} + diff --git a/Week_04/id_33/Leetcode_714_33.java b/Week_04/id_33/Leetcode_714_33.java new file mode 100644 index 00000000..fdf26e8b --- /dev/null +++ b/Week_04/id_33/Leetcode_714_33.java @@ -0,0 +1,21 @@ +/* + * @lc app=leetcode id=714 lang=java + * + * [714] Best Time to Buy and Sell Stock with Transaction Fee + */ +class Solution { + public int maxProfit(int[] prices, int fee) { + if(prices.length <= 1) return 0; + int profit = 0, min = prices[0]; + for(int i = 0;i < prices.length;i++){ + if(prices[i] < min){ + min = prices[i]; + }else if(prices[i] > min + fee){ + profit += prices[i] - min - fee; + min = prices[i] - fee; + } + } + return profit; + } +} +