From f90729938d121344b7877a8c814483f40d1b7a1b Mon Sep 17 00:00:00 2001 From: yuqiu-pp <41912446@qq.com> Date: Tue, 14 May 2019 21:07:53 +0800 Subject: [PATCH] 169,221,309,714 --- Week_04/id_6/LeetCode_169_6.java | 30 ++++++++++++ Week_04/id_6/LeetCode_221_6.java | 79 ++++++++++++++++++++++++++++++++ Week_04/id_6/LeetCode_309_6.java | 31 +++++++++++++ Week_04/id_6/LeetCode_714_6.java | 60 ++++++++++++++++++++++++ 4 files changed, 200 insertions(+) create mode 100644 Week_04/id_6/LeetCode_169_6.java create mode 100644 Week_04/id_6/LeetCode_221_6.java create mode 100644 Week_04/id_6/LeetCode_309_6.java create mode 100644 Week_04/id_6/LeetCode_714_6.java diff --git a/Week_04/id_6/LeetCode_169_6.java b/Week_04/id_6/LeetCode_169_6.java new file mode 100644 index 00000000..52f82107 --- /dev/null +++ b/Week_04/id_6/LeetCode_169_6.java @@ -0,0 +1,30 @@ + + + // LeetCode 169 + + + + // 此次 从n/2分开,分别统计数字出现次数 + // 多种数字的话,可以用散列表保存 + public int majorityElement(int[] nums) { + int n = nums.length; + if (n <= 2){ + return nums[0]; + } + + HashMap map = new HashMap<>(); + int i = 0; + for (i = 0; i < n; i++) { + if (map.containsKey(nums[i])){ + int m = map.get(nums[i]) + 1; + if (m > n/2){ + break; + } + map.put(nums[i], m); + }else { + map.put(nums[i], 1); + } + } + + return nums[i]; + } \ No newline at end of file diff --git a/Week_04/id_6/LeetCode_221_6.java b/Week_04/id_6/LeetCode_221_6.java new file mode 100644 index 00000000..220bd7bb --- /dev/null +++ b/Week_04/id_6/LeetCode_221_6.java @@ -0,0 +1,79 @@ + + + // LeetCode 309 + + + // [ 1. Trie树 + 回溯算法 ] + + class TrieNode { + + public char data; + public TrieNode[] children = new TrieNode[26]; + public boolean isEndingChar = false; + public TrieNode(char data){ + this.data = data; + } + } + + // root + TrieNode root = new TrieNode('\\'); + + public void addWord(String word){ + char[] wd = word.toCharArray(); + + TrieNode p = root; + // 遍历单词,在Tire中查找 + for (int i = 0; i < wd.length; i++) { + int index = wd[i] - 'a'; + // 字符不存在 + if (p.children[index] == null){ + p.children[index] = new TrieNode(wd[i]); + } + p = p.children[index]; + } + p.isEndingChar = true; + } + + public boolean search(String word){ + TrieNode p = root; + + return find(word, root); + } + + + // 先按前缀匹配在Trie树中查找 + // 遇到‘.’时,从该点children钟任意一个存在字符向下去匹配,dfs+回溯 + public boolean find(String word, TrieNode curr){ + TrieNode p = curr; + + for (int i = 0; i < word.length(); i++) { + char c = word.charAt(i); + if (c == '.'){ + // 从任何一个children[index]!=null 递归的去查找 + // 如果匹配失败,回溯到上一个点,继续 + for (int j = 0; j < p.children.length; j++) { + if (p.children[j] != null){ + String wd = word.substring(i+1, word.length()); + if (find(wd, p.children[j])){ + return true; + } + } + } + return false; + }else { + int index = c - 'a'; + if (p.children[index] != null){ + p = p.children[index]; + }else { + return false; + } + } + + } + // 不是结尾 + if (!p.isEndingChar){ + return false; + } + + return true; + } \ No newline at end of file diff --git a/Week_04/id_6/LeetCode_309_6.java b/Week_04/id_6/LeetCode_309_6.java new file mode 100644 index 00000000..307f05b2 --- /dev/null +++ b/Week_04/id_6/LeetCode_309_6.java @@ -0,0 +1,31 @@ + + + // LeetCode 309 + + + // [ 1. 动态规划 ] + + public int maxProfit(int[] prices) { + int len = prices.length; + if (len == 0){ + return 0; + } + + int[] buy = new int[len]; + int[] sell = new int[len]; + + buy[0] = 0 - prices[0]; + sell[0] = 0; + + for (int i = 1; i < len; i++) { + if (i == 1){ + buy[i] = Math.max(sell[0]-prices[i], buy[i-1]); + }else { + buy[i] = Math.max(sell[i-2]-prices[i], buy[i-1]); + } + + sell[i] = Math.max(prices[i]+buy[i-1], sell[i-1]); + } + + return Math.max(sell[len-1], buy[len-1]); + } \ No newline at end of file diff --git a/Week_04/id_6/LeetCode_714_6.java b/Week_04/id_6/LeetCode_714_6.java new file mode 100644 index 00000000..570b3120 --- /dev/null +++ b/Week_04/id_6/LeetCode_714_6.java @@ -0,0 +1,60 @@ + + + // LeetCode 714 + + + // [ 1. 贪心算法 ] + // 1)问题抽象:n天股票价格中,抽取某些天进行买卖 + // 2)利润最大:最低价买,最高价卖 + // ?? 手续费起什么作用 减手续费后的利润如果小于0,则不卖 + public int maxProfit(int[] prices, int fee) { + if (prices.length <= 1){ + return 0; + } + + int minPirce = prices[0]; + int sum = 0; + + for (int i = 1; i < prices.length; i++) { + if (prices[i] <= minPirce){ + minPirce = prices[i]; + }else { + // 当天价格卖出利润大于0 + int n = prices[i] - minPirce - fee; + if (n > 0){ + sum += n; + // 后面有可能存在利润更大的,如果出现这种情况,把那部分利润补回来就行了 + // ?? + minPirce = prices[i] - fee; + } + } + } + return sum; + } + + + + + // [ 1. 动态规划 ] + + public int maxProfit(int[] prices, int fee) { + if (prices.length <= 1){ + return 0; + } + + int len = prices.length; + int[] buy = new int[len]; + int[] sell = new int[len]; + + // 第一天 + buy[0] = 0-prices[0]; + sell[0] = prices[0]; + + // 推导公式 + for (int i = 1; i < prices.length; i++) { + buy[i] = Math.max(buy[i-1], sell[i-1]-prices[i]); + sell[i] = Math.max(sell[i-1], buy[i-1]+prices[i]-fee); + } + + return Math.max(buy[len-1], sell[len-1]); + } \ No newline at end of file