From 918461c5b584891e3dda5a83b55dfa34d79d920d Mon Sep 17 00:00:00 2001 From: liushulong Date: Sun, 8 Dec 2019 20:35:16 +0800 Subject: [PATCH 1/2] add --- .idea/algorithm004-03.iml | 9 ++ .idea/misc.xml | 7 ++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ .idea/workspace.xml | 127 +++++++++++++++++++++++++++ Week 08/id_388/LeeCode_388_10.java | 100 +++++++++++++++++++++ Week 08/id_388/LeeCode_388_1143.java | 103 ++++++++++++++++++++++ Week 08/id_388/LeeCode_388_115.java | 76 ++++++++++++++++ Week 08/id_388/LeeCode_388_125.java | 76 ++++++++++++++++ Week 08/id_388/LeeCode_388_14.java | 61 +++++++++++++ Week 08/id_388/LeeCode_388_151.java | 84 ++++++++++++++++++ Week 08/id_388/LeeCode_388_300.java | 50 +++++++++++ Week 08/id_388/LeeCode_388_32.java | 51 +++++++++++ Week 08/id_388/LeeCode_388_44.java | 88 +++++++++++++++++++ Week 08/id_388/LeeCode_388_45.java | 85 ++++++++++++++++++ Week 08/id_388/LeeCode_388_49.java | 114 ++++++++++++++++++++++++ Week 08/id_388/LeeCode_388_5.java | 65 ++++++++++++++ Week 08/id_388/LeeCode_388_8.java | 97 ++++++++++++++++++++ Week 08/id_388/LeeCode_388_85.java | 65 ++++++++++++++ Week 08/id_388/LeeCode_388_91.java | 71 +++++++++++++++ Week 08/id_388/LeeCode_388_917.java | 85 ++++++++++++++++++ Week 08/id_388/NOTE.md | 37 +++++++- 22 files changed, 1464 insertions(+), 1 deletion(-) create mode 100644 .idea/algorithm004-03.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 Week 08/id_388/LeeCode_388_10.java create mode 100644 Week 08/id_388/LeeCode_388_1143.java create mode 100644 Week 08/id_388/LeeCode_388_115.java create mode 100644 Week 08/id_388/LeeCode_388_125.java create mode 100644 Week 08/id_388/LeeCode_388_14.java create mode 100644 Week 08/id_388/LeeCode_388_151.java create mode 100644 Week 08/id_388/LeeCode_388_300.java create mode 100644 Week 08/id_388/LeeCode_388_32.java create mode 100644 Week 08/id_388/LeeCode_388_44.java create mode 100644 Week 08/id_388/LeeCode_388_45.java create mode 100644 Week 08/id_388/LeeCode_388_49.java create mode 100644 Week 08/id_388/LeeCode_388_5.java create mode 100644 Week 08/id_388/LeeCode_388_8.java create mode 100644 Week 08/id_388/LeeCode_388_85.java create mode 100644 Week 08/id_388/LeeCode_388_91.java create mode 100644 Week 08/id_388/LeeCode_388_917.java diff --git a/.idea/algorithm004-03.iml b/.idea/algorithm004-03.iml new file mode 100644 index 000000000..d6ebd4805 --- /dev/null +++ b/.idea/algorithm004-03.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 000000000..f091b54e2 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 000000000..24b8cf3cb --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 000000000..35eb1ddfb --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 000000000..87dff4dee --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,127 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1571496109469 + + + + + + + + + \ No newline at end of file diff --git a/Week 08/id_388/LeeCode_388_10.java b/Week 08/id_388/LeeCode_388_10.java new file mode 100644 index 000000000..7d48df696 --- /dev/null +++ b/Week 08/id_388/LeeCode_388_10.java @@ -0,0 +1,100 @@ +package com.company.leetcode.editor.cn;//给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 '.' 和 '*' 的正则表达式匹配。 +// +// '.' 匹配任意单个字符 +//'*' 匹配零个或多个前面的那一个元素 +// +// +// 所谓匹配,是要涵盖 整个 字符串 s的,而不是部分字符串。 +// +// 说明: +// +// +// s 可能为空,且只包含从 a-z 的小写字母。 +// p 可能为空,且只包含从 a-z 的小写字母,以及字符 . 和 *。 +// +// +// 示例 1: +// +// 输入: +//s = "aa" +//p = "a" +//输出: false +//解释: "a" 无法匹配 "aa" 整个字符串。 +// +// +// 示例 2: +// +// 输入: +//s = "aa" +//p = "a*" +//输出: true +//解释: 因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 "aa" 可被视为 'a' 重复了一次。 +// +// +// 示例 3: +// +// 输入: +//s = "ab" +//p = ".*" +//输出: true +//解释: ".*" 表示可匹配零个或多个('*')任意字符('.')。 +// +// +// 示例 4: +// +// 输入: +//s = "aab" +//p = "c*a*b" +//输出: true +//解释: 因为 '*' 表示零个或多个,这里 'c' 为 0 个, 'a' 被重复一次。因此可以匹配字符串 "aab"。 +// +// +// 示例 5: +// +// 输入: +//s = "mississippi" +//p = "mis*is*p*." +//输出: false +// Related Topics 字符串 动态规划 回溯算法 + + + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution_10 { + public boolean isMatch(String s, String p) { + if (s == null || p == null) { + return false; + } + boolean[][] dp = new boolean[s.length() + 1][p.length() + 1]; + dp[0][0] = true; + for (int i = 0; i < p.length(); i++) { + if (p.charAt(i) == '*' && dp[0][i - 1]) { + dp[0][i + 1] = true; + } + } + for (int i = 0; i < s.length(); i++) { + for (int j = 0; j < p.length(); j++) { + if (p.charAt(j) == '.') { + dp[i + 1][j + 1] = dp[i][j]; + } + if (p.charAt(j) == s.charAt(i)) { + dp[i + 1][j + 1] = dp[i][j]; + } + if (p.charAt(j) == '*') { + if (p.charAt(j-1) != s.charAt(i) && p.charAt(j - 1) != '.') { + dp[i + 1][j + 1] = dp[i + 1][j - 1]; + } else { + dp[i + 1][j + 1] = (dp[i + 1][j] || dp[i][j + 1] || dp[i + 1][j - 1]); + } + } + } + } + return dp[s.length()][p.length()]; + } + +// public static void main(String[] args) { +// Solution s = new Solution(); +// s.isMatch("aa","a"); +// } +} +//leetcode submit region end(Prohibit modification and deletion) diff --git a/Week 08/id_388/LeeCode_388_1143.java b/Week 08/id_388/LeeCode_388_1143.java new file mode 100644 index 000000000..1acc39253 --- /dev/null +++ b/Week 08/id_388/LeeCode_388_1143.java @@ -0,0 +1,103 @@ +package com.company.leetcode.editor.cn; + +//给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序列。 +//count = 2 +// 一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。 +//例如,"ace" 是 "abcde" 的子序列,但 "aec" 不是 "abcde" 的子序列。两个字符串的「公共子序列」是这两个字符串所共同拥有的子序列。 +// +// 若这两个字符串没有公共子序列,则返回 0。 +// +// +// +// 示例 1: +// +// 输入:text1 = "abcde", text2 = "ace" +//输出:3 +//解释:最长公共子序列是 "ace",它的长度为 3。 +// +// +// 示例 2: +// +// 输入:text1 = "abc", text2 = "abc" +//输出:3 +//解释:最长公共子序列是 "abc",它的长度为 3。 +// +// +// 示例 3: +// +// 输入:text1 = "abc", text2 = "def" +//输出:0 +//解释:两个字符串没有公共子序列,返回 0。 +// +// +// +// +// 提示: +// +// +// 1 <= text1.length <= 1000 +// 1 <= text2.length <= 1000 +// 输入的字符串只含有小写英文字符。 +// +// Related Topics 动态规划 + + + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution_1143 { + + public int longestCommonSubsequence(String text1, String text2) { + + char[] c1 = text1.toCharArray(); + char[] c2 = text2.toCharArray(); + + if (c1.length < 1 || c2.length < 1) { + return 0; + } + + int[][] dp = new int[c1.length][c2.length]; + dp[0][0] = c1[0] == c2[0] ? 1 : 0; + //row0 + for (int i = 1; i < c2.length; i++) { + if (c1[0] == c2[i]) { + dp[0][i] = 1; + } else { + dp[0][i] = dp[0][i - 1]; + } + } + //col0 + for (int j = 1; j < c1.length; j++) { + if (c1[j] == c2[0]) { + dp[j][0] = 1; + } else { + dp[j][0] = dp[j - 1][0]; + } + } + + for (int i = 1; i < c1.length; i++) { + for (int j = 1; j < c2.length; j++) { + if (c1[i] == c2[j]) { + dp[i][j] = dp[i - 1][j - 1] + 1; + } else { + dp[i][j] = Math.max(dp[i - 1][j],dp[i][j - 1]); + } + } + } + + return dp[c1.length - 1][c2.length - 1]; + } + + +// public static void main(String[] args) { +// Solution s = new Solution(); +// String text1 = "dknkdizqxkdczafixidorgfcnkrirmhmzqbcfuvojsxwraxe"; +// String text2 = "dulixqfgvipenkfubgtyxujixspoxmhgvahqdmzmlyhajerqz"; +// //Wrong Answer: input:"bsbininm" "jmjkbkjkv" Output:2 Expected:1 +// //Wrong Answer: input:"dknkdizqxkdczafixidorgfcnkrirmhmzqbcfuvojsxwraxe" +// // "dulixqfgvipenkfubgtyxujixspoxmhgvahqdmzmlyhajerqz" +// // Output:15 Expected:14 +// int len = s.longestCommonSubsequence(text1,text2); +// System.out.println(len); +// } +} +//leetcode submit region end(Prohibit modification and deletion) diff --git a/Week 08/id_388/LeeCode_388_115.java b/Week 08/id_388/LeeCode_388_115.java new file mode 100644 index 000000000..8407d7ec6 --- /dev/null +++ b/Week 08/id_388/LeeCode_388_115.java @@ -0,0 +1,76 @@ +package com.company.leetcode.editor.cn; + +//给定一个字符串 S 和一个字符串 T,计算在 S 的子序列中 T 出现的个数。 +// +// 一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串。(例如,"ACE" 是 "ABCDE" 的一个子序列,而 "AEC" 不是) +// count = 2 +// 示例 1: +// +// 输入: S = "rabbbit", T = "rabbit" +//输出: 3 +//解释: +// +//如下图所示, 有 3 种可以从 S 中得到 "rabbit" 的方案。 +//(上箭头符号 ^ 表示选取的字母) +// +//rabbbit +//^^^^ ^^ +//rabbbit +//^^ ^^^^ +//rabbbit +//^^^ ^^^ +// +// +// 示例 2: +// +// 输入: S = "babgbag", T = "bag" +//输出: 5 +//解释: +// +//如下图所示, 有 5 种可以从 S 中得到 "bag" 的方案。 +//(上箭头符号 ^ 表示选取的字母) +// +//babgbag +//^^ ^ +//babgbag +//^^ ^ +//babgbag +//^ ^^ +//babgbag +// ^ ^^ +//babgbag +// ^^^ +// Related Topics 字符串 动态规划 + + + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution_115 { + public int numDistinct(String s, String t) { + int rows = s.length(); + int cols = t.length(); + int[][] dp = new int[rows + 1][cols + 1]; + dp[0][0] = 1; + for (int i = 1; i <= cols; i++) { + dp[0][i] = 1; + } + for (int i = 1; i <= rows; i++) { + for (int j = 1; j <= cols; j++) { + if (s.charAt(i - 1) == t.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1]; + } else { + dp[i][j] = dp[i][j - 1]; + } + } + } + return dp[rows][cols]; + } + +// public static void main(String[] args) { +// Solution s = new Solution(); +// String S = "babgbag"; +// String T = "bag"; +// System.out.println(s.numDistinct(S,T)); +// } +} +//leetcode submit region end(Prohibit modification and deletion) diff --git a/Week 08/id_388/LeeCode_388_125.java b/Week 08/id_388/LeeCode_388_125.java new file mode 100644 index 000000000..4c84b16f1 --- /dev/null +++ b/Week 08/id_388/LeeCode_388_125.java @@ -0,0 +1,76 @@ +package com.company.leetcode.editor.cn; +//给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。 +// +// 说明:本题中,我们将空字符串定义为有效的回文串。 +// +// 示例 1: +// +// 输入: "A man, a plan, a canal: Panama" +//输出: true +// +// +// 示例 2: +// +// 输入: "race a car" +//输出: false +// +// Related Topics 双指针 字符串 + + + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution_125 { + public boolean isPalindrome(String s) { + if (s == null) { + return false; + } + if (s.length() == 1) { + return true; + } + + int left = 0; + int right = s.length() - 1; + while (left < right) { + char leftChar = s.charAt(left); + while (left < right && !isValidChar(leftChar)) { + left++; + leftChar = s.charAt(left); + } + if (left == right) { + return true; + } + char rightChar = s.charAt(right); + while (left < right && !isValidChar(rightChar)){ + right--; + rightChar = s.charAt(right); + } + if (left == right) { + return true; + } + String leftStr = String.valueOf(leftChar); + String rightStr = String.valueOf(rightChar); + if (!leftStr.equalsIgnoreCase(rightStr)){ + return false; + } + left++; + right--; + } + + return true; + + } + + //字母和数字字符 + private boolean isValidChar(char c){ + boolean b = (c >= '0' && c <= '9') + || (c >= 'A' && c <= 'Z') + || (c >= 'a' && c <= 'z'); + return b; + } + +// public static void main(String[] args) { +// Solution s = new Solution(); +// System.out.println(s.isPalindrome("race a car")); +// } +} +//leetcode submit region end(Prohibit modification and deletion) diff --git a/Week 08/id_388/LeeCode_388_14.java b/Week 08/id_388/LeeCode_388_14.java new file mode 100644 index 000000000..a9453a561 --- /dev/null +++ b/Week 08/id_388/LeeCode_388_14.java @@ -0,0 +1,61 @@ +package com.company.leetcode.editor.cn; +//编写一个函数来查找字符串数组中的最长公共前缀。 +// +// 如果不存在公共前缀,返回空字符串 ""。 +// +// 示例 1: +// +// 输入: ["flower","flow","flight"] +//输出: "fl" +// +// +// 示例 2: +// +// 输入: ["dog","racecar","car"] +//输出: "" +//解释: 输入不存在公共前缀。 +// +// +// 说明: +// +// 所有输入只包含小写字母 a-z 。 +// Related Topics 字符串 + + + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution_14 { + public String longestCommonPrefix(String[] strs) { + if (strs == null || strs.length == 0) { + return ""; + } + if (strs.length == 1) { + return strs[0]; + } + char[] com = strs[0].toCharArray(); + int resLen = com.length; + for (int i = 1; i < strs.length; i++) { + String tmp = strs[i]; + resLen = Math.min(tmp.length(),resLen); + for (int j = 0; j < resLen; j++) { + if (tmp.charAt(j) != com[j]) { + resLen = j; + break; + } + } + + } + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < resLen; i++) { + sb.append(com[i]); + } + return sb.toString(); + } + +// public static void main(String[] args) { +// Solution s = new Solution(); +// String[] strs = new String[]{"aa","a"}; +// System.out.println(s.longestCommonPrefix(strs)); +// } +} +//leetcode submit region end(Prohibit modification and deletion) diff --git a/Week 08/id_388/LeeCode_388_151.java b/Week 08/id_388/LeeCode_388_151.java new file mode 100644 index 000000000..afbadc8f1 --- /dev/null +++ b/Week 08/id_388/LeeCode_388_151.java @@ -0,0 +1,84 @@ +package com.company.leetcode.editor.cn; + +//给定一个字符串,逐个翻转字符串中的每个单词。 +// +// +// +// 示例 1: +// +// 输入: "the sky is blue" +//输出: "blue is sky the" +// +// +// 示例 2: +// +// 输入: "  hello world!  " +//输出: "world! hello" +//解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。 +// +// +// 示例 3: +// +// 输入: "a good   example" +//输出: "example good a" +//解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。 +// +// +// +// +// 说明: +// +// +// 无空格字符构成一个单词。 +// 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。 +// 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。 +// +// +// +// +// 进阶: +// +// 请选用 C 语言的用户尝试使用 O(1) 额外空间复杂度的原地解法。 +// Related Topics 字符串 + + +import java.util.ArrayList; +import java.util.List; + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution_151 { + public String reverseWords(String s) { + List words = new ArrayList<>(); + + int i = 0; + while (i < s.length()) { + if (s.charAt(i) ==' ') { + i++; + continue; + } + StringBuilder tmp = new StringBuilder(); + while (i < s.length() && s.charAt(i) != ' ') { + tmp.append(s.charAt(i)); + i++; + } + + if (!tmp.toString().equals(" ")) { + words.add(tmp); + } + } + StringBuilder res = new StringBuilder(); + for (int j = words.size() - 1; j >= 0; j--) { + res.append(words.get(j).toString()); + if (j != 0) { + res.append(" "); + } + } + return res.toString(); + } + +// public static void main(String[] args) { +// Solution s = new Solution(); +// System.out.println(s.reverseWords("  hello world! ")); +// } +} +//leetcode submit region end(Prohibit modification and deletion) diff --git a/Week 08/id_388/LeeCode_388_300.java b/Week 08/id_388/LeeCode_388_300.java new file mode 100644 index 000000000..8329d2f7e --- /dev/null +++ b/Week 08/id_388/LeeCode_388_300.java @@ -0,0 +1,50 @@ +package com.company.leetcode.editor.cn;//给定一个无序的整数数组,找到其中最长上升子序列的长度。 +// +// 示例: +// +// 输入: [10,9,2,5,3,7,101,18] +//输出: 4 +//解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4。 +// +// 说明: +// +// +// 可能会有多种最长上升子序列的组合,你只需要输出对应的长度即可。 +// 你算法的时间复杂度应该为 O(n2) 。 +// +// +// 进阶: 你能将算法的时间复杂度降低到 O(n log n) 吗? +// Related Topics 二分查找 动态规划 + + + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution_300 { + public int lengthOfLIS(int[] nums) { + if (nums == null || nums.length == 0) { + return 0; + } + + int[] dp = new int[nums.length]; + int maxans = 1; + dp[0] = 1; + for (int i = 1; i < nums.length; i++) { + int maxVal = 0; + for (int j = 0; j < i; j++) { + if (nums[i] > nums[j]) { + maxVal = Math.max(maxVal,dp[j]); + } + } + dp[i] = maxVal + 1; + maxans = Math.max(dp[i],maxans); + } + return maxans; + } + +// public static void main(String[] args) { +// Solution s = new Solution(); +// int[] nums = new int[]{10,9,2,5,3,7,101,18}; +// System.out.println(s.lengthOfLIS(nums)); +// } +} +//leetcode submit region end(Prohibit modification and deletion) diff --git a/Week 08/id_388/LeeCode_388_32.java b/Week 08/id_388/LeeCode_388_32.java new file mode 100644 index 000000000..4efd4d47d --- /dev/null +++ b/Week 08/id_388/LeeCode_388_32.java @@ -0,0 +1,51 @@ +package com.company.leetcode.editor.cn; + +//给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度。 +// +// 示例 1: +// +// 输入: "(()" +//输出: 2 +//解释: 最长有效括号子串为 "()" +// +// +// 示例 2: +// +// 输入: ")()())" +//输出: 4 +//解释: 最长有效括号子串为 "()()" +// +// Related Topics 字符串 动态规划 + + + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution_32 { + public int longestValidParentheses(String s) { + if (s == null || s.length() <= 1) { + return 0; + } + int len = s.length(); + int[] dp = new int[len]; + dp[0] = 0; + int maxans = 0; + for (int i = 1; i < len; i++) { + char c = s.charAt(i); + if (c == ')') { + if (s.charAt(i - 1) == '(') { + dp[i] = 2 + (i >= 2 ? dp[i - 2] : 0); + } else if(i - dp[i - 1] > 0 && s.charAt(i - dp[i - 1] - 1) == '(') { + dp[i] = dp[i - 1] + 2+ ((i - dp[i - 1] - 2) >= 0 ? dp[i - dp[i - 1] - 2] : 0); + } + maxans = Math.max(maxans,dp[i]); + } + } + return maxans; + } + +// public static void main(String[] args) { +// Solution s = new Solution(); +// System.out.println(s.longestValidParentheses("()(()")); +// } +} +//leetcode submit region end(Prohibit modification and deletion) diff --git a/Week 08/id_388/LeeCode_388_44.java b/Week 08/id_388/LeeCode_388_44.java new file mode 100644 index 000000000..213145059 --- /dev/null +++ b/Week 08/id_388/LeeCode_388_44.java @@ -0,0 +1,88 @@ +package com.company.leetcode.editor.cn;//给定一个字符串 (s) 和一个字符模式 (p) ,实现一个支持 '?' 和 '*' 的通配符匹配。 +// +// '?' 可以匹配任何单个字符。 +//'*' 可以匹配任意字符串(包括空字符串)。 +// +// +// 两个字符串完全匹配才算匹配成功。 +// +// 说明: +// +// +// s 可能为空,且只包含从 a-z 的小写字母。 +// p 可能为空,且只包含从 a-z 的小写字母,以及字符 ? 和 *。 +// +// +// 示例 1: +// +// 输入: +//s = "aa" +//p = "a" +//输出: false +//解释: "a" 无法匹配 "aa" 整个字符串。 +// +// 示例 2: +// +// 输入: +//s = "aa" +//p = "*" +//输出: true +//解释: '*' 可以匹配任意字符串。 +// +// +// 示例 3: +// +// 输入: +//s = "cb" +//p = "?a" +//输出: false +//解释: '?' 可以匹配 'c', 但第二个 'a' 无法匹配 'b'。 +// +// +// 示例 4: +// +// 输入: +//s = "adceb" +//p = "*a*b" +//输出: true +//解释: 第一个 '*' 可以匹配空字符串, 第二个 '*' 可以匹配字符串 "dce". +// +// +// 示例 5: +// +// 输入: +//s = "acdcb" +//p = "a*c?b" +//输入: false +// Related Topics 贪心算法 字符串 动态规划 回溯算法 + + + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public boolean isMatch(String s, String p) { + int m = s.length(); + int n = p.length(); + boolean[][] dp = new boolean[m + 1][n + 1]; + dp[0][0] = true; + for (int i = 1; i <= n; i++) { + dp[0][i] = dp[0][i - 1] && p.charAt(i - 1) == '*'; + } + for (int i = 1; i <= m; i++) { + for (int j = 1; j <= n; j++) { + if (p.charAt(j - 1) != '*') { + dp[i][j] = dp[i - 1][j - 1] && (s.charAt(i - 1) == p.charAt(j - 1)||p.charAt(j - 1) == '?'); + } else { + dp[i][j] = dp[i - 1][j] || dp[i][j - 1]; + } + } + } + return dp[m][n]; + } + + public static void main(String[] args) { + Solution s = new Solution(); + s.isMatch("aa","a"); + } +} +//leetcode submit region end(Prohibit modification and deletion) diff --git a/Week 08/id_388/LeeCode_388_45.java b/Week 08/id_388/LeeCode_388_45.java new file mode 100644 index 000000000..b98d4b5e8 --- /dev/null +++ b/Week 08/id_388/LeeCode_388_45.java @@ -0,0 +1,85 @@ +package com.company.leetcode.editor.cn; +//给定一个非负整数数组,你最初位于数组的第一个位置。 +// +// 数组中的每个元素代表你在该位置可以跳跃的最大长度。 +// +// 你的目标是使用最少的跳跃次数到达数组的最后一个位置。 +// +// 示例: +// count = 2 +// 输入: [2,3,1,1,4] +//输出: 2 +//解释: 跳到最后一个位置的最小跳跃数是 2。 +//  从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。 +// +// +// 说明: +// +// 假设你总是可以到达数组的最后一个位置。 +// Related Topics 贪心算法 数组 + + + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution_45 { + + public int jump(int[] nums) { + if (nums == null || nums.length <= 1) { + return 0; + } + int maxIndex = nums[0]; + int current = 1; + int steps = 1; + while (maxIndex < nums.length - 1) { + + int nextMax = maxIndex; + for (int i = current; i <= maxIndex; i++) { + nextMax = Math.max(nums[i] + i,nextMax); + } + steps++; + current = maxIndex; + maxIndex = nextMax; + } + return steps; + } + + public int jump1(int[] nums) { + int end = 0; + int maxIndex = 0; + int steps = 0; + + for (int i = 0; i < nums.length - 1; i++) { + maxIndex = Math.max(nums[i] + i,maxIndex); + if (i == end) { + steps++; + end = maxIndex; + } + } + + return steps; + } + + public int jump2(int[] nums) { + + int position = nums.length - 1; + int steps = 0; + while (position != 0) { + for (int i = 0; i < position; i++) { + if (nums[i] + i >= position) { + position = i; + steps++; + break; + } + } + } + + return steps; + } + +// public static void main(String[] args) { +// Solution s = new Solution(); +// int[] nums = new int[]{2,3,1,1,4}; +// System.out.println(s.jump(nums)); +// } +} +//leetcode submit region end(Prohibit modification and deletion) diff --git a/Week 08/id_388/LeeCode_388_49.java b/Week 08/id_388/LeeCode_388_49.java new file mode 100644 index 000000000..399e3eefd --- /dev/null +++ b/Week 08/id_388/LeeCode_388_49.java @@ -0,0 +1,114 @@ +package com.company.leetcode.editor.cn; +//给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。 +// +// 示例: +// +// 输入: ["eat", "tea", "tan", "ate", "nat", "bat"], +//输出: +//[ +// ["ate","eat","tea"], +// ["nat","tan"], +// ["bat"] +//] +// +// 说明: +// +// +// 所有输入均为小写字母。 +// 不考虑答案输出的顺序。 +// +// Related Topics 哈希表 字符串 + + +import java.util.*; + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution_49 { + + public List> groupAnagrams(String[] strs) { + Map> map = new HashMap<>(); + for (int i = 0; i < strs.length; i++) { + String tmp = strs[i]; + char[] cs = tmp.toCharArray(); + Arrays.sort(cs); + //拼接key + StringBuilder sb = new StringBuilder(); + for (int j = 0; j < cs.length; j++) { + sb.append(cs[j]).append('#'); + } + String key = sb.toString(); + List tmpList; + if (map.containsKey(key)) { + tmpList = map.get(key); + } else { + tmpList = new ArrayList(); + } + tmpList.add(tmp); + map.put(key,tmpList); + } + return new ArrayList<>(map.values()); + } + + //key自己计算 +// public List> groupAnagrams(String[] strs) { +// int[] count = new int[26]; +// Map> resMap = new HashMap<>(); +// for (int i = 0; i < strs.length; i++) { +// Arrays.fill(count,0); +// String s = strs[i]; +// //计算key +// for (int j = 0; j < s.length(); j++) { +// count[s.charAt(j) - 'a']++; +// } +// StringBuilder sb = new StringBuilder(); +// for (int j = 0; j < count.length; j++) { +// sb.append(count[j]).append("#"); +// } +// String key = sb.toString(); +// if (resMap.containsKey(key)) { +// List tmp = resMap.get(key); +// tmp.add(s); +// continue; +// } +// List tmp = new ArrayList(); +// tmp.add(s); +// resMap.put(key,tmp); +// } +// return new ArrayList<>(resMap.values()); +// } + + //counter作为map的key + public List> groupAnagrams1(String[] strs) { + + //计算每一个的counter + Map,List> counterMap = new HashMap<>(); + for (int i = 0; i < strs.length; i++) { + //计算counter + Map counter = new HashMap<>(); + for (int j = 0; j < strs[i].length(); j++) { + Character c = strs[i].charAt(j); + if (counter.containsKey(c)){ + Integer count = counter.get(c) + 1; + counter.put(c,count); + continue; + } + counter.put(c,1); + } + if (counterMap.containsKey(counter)) { + List list = counterMap.get(counter); + list.add(strs[i]); + continue; + } + List list = new ArrayList<>(); + list.add(strs[i]); + counterMap.put(counter,list); + } + List> res = new ArrayList<>(); + for (List value : counterMap.values()) { + res.add(value); + } + return res; + } + +} +//leetcode submit region end(Prohibit modification and deletion) diff --git a/Week 08/id_388/LeeCode_388_5.java b/Week 08/id_388/LeeCode_388_5.java new file mode 100644 index 000000000..e5f4f5782 --- /dev/null +++ b/Week 08/id_388/LeeCode_388_5.java @@ -0,0 +1,65 @@ +package com.company.leetcode.editor.cn; +//给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。 +// +// 示例 1: +// count = 2 +// 输入: "babad" +//输出: "bab" +//注意: "aba" 也是一个有效答案。 +// +// +// 示例 2: +// +// 输入: "cbbd" +//输出: "bb" +// +// Related Topics 字符串 动态规划 + + + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution_5 { + + public String longestPalindrome(String s) { + + if (s == null || s.length() < 1) { + return ""; + } + if (s.length() == 1) { + return s; + } + int start = 0; + int end = 0; + int maxLen = 0; + for (int i = 0; i < s.length(); i++) { + + int len1 = expand(s,i,i); + int len2 = expand(s,i,i+1); + int len = Math.max(len1,len2); + if (len > maxLen) { + start = i - (len - 1)/2; + end = i + len/2; + maxLen = len; + } + + } + + return s.substring(start,end + 1); + } + + private int expand(String s, int left, int right) { + + while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) { + left--; + right++; + } + + return right - left -1; + } + +// public static void main(String[] args) { +// Solution s = new Solution(); +// System.out.println(s.longestPalindrome("cbbd")); +// } +} +//leetcode submit region end(Prohibit modification and deletion) diff --git a/Week 08/id_388/LeeCode_388_8.java b/Week 08/id_388/LeeCode_388_8.java new file mode 100644 index 000000000..b1a586327 --- /dev/null +++ b/Week 08/id_388/LeeCode_388_8.java @@ -0,0 +1,97 @@ +package com.company.leetcode.editor.cn; + +//请你来实现一个 atoi 函数,使其能将字符串转换成整数。 +// +// 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。 +// +// 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。 +// +// 该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。 +// +// 注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。 +// +// 在任何情况下,若函数不能进行有效的转换时,请返回 0。 +// +// 说明: +// +// 假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−231, 231 − 1]。 +// 如果数值超过这个范围,请返回 INT_MAX (231 − 1) 或 INT_MIN (−231) 。 +// +// 示例 1: +// +// 输入: "42" +//输出: 42 +// +// +// 示例 2: +// +// 输入: " -42" +//输出: -42 +//解释: 第一个非空白字符为 '-', 它是一个负号。 +//  我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 -42 。 +// +// +// 示例 3: +// +// 输入: "4193 with words" +//输出: 4193 +//解释: 转换截止于数字 '3' ,因为它的下一个字符不为数字。 +// +// +// 示例 4: +// +// 输入: "words and 987" +//输出: 0 +//解释: 第一个非空字符是 'w', 但它不是数字或正、负号。 +// 因此无法执行有效的转换。 +// +// 示例 5: +// +// 输入: "-91283472332" +//输出: -2147483648 +//解释: 数字 "-91283472332" 超过 32 位有符号整数范围。 +//  因此返回 INT_MIN (−231) 。 +// +// Related Topics 数学 字符串 + + + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution_8 { + public int myAtoi(String str) { + if (str == null || str.length() == 0) { + return 0; + } + //检查空格 + int i = 0; + while (i < str.length() && str.charAt(i) == ' ') { + i++; + } + //检查正负 + int flag = 1; + if (i < str.length() && str.charAt(i) == '-') { + flag = -1; + } + if (i < str.length() && (str.charAt(i) == '+' || str.charAt(i) == '-')) { + i++; + } + int res = 0; + while (i < str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9') { + int n = str.charAt(i) - '0'; + + if (Integer.MAX_VALUE / 10 < res || (Integer.MAX_VALUE/10 == res && Integer.MAX_VALUE%10 < n)) { + return flag == -1 ? Integer.MIN_VALUE : Integer.MAX_VALUE; + } + + res = res * 10 + n; + i++; + } + return res * flag; + } + +// public static void main(String[] args) { +// Solution s = new Solution(); +// System.out.println(s.myAtoi(" ")); +// } +} +//leetcode submit region end(Prohibit modification and deletion) diff --git a/Week 08/id_388/LeeCode_388_85.java b/Week 08/id_388/LeeCode_388_85.java new file mode 100644 index 000000000..1b9eaca00 --- /dev/null +++ b/Week 08/id_388/LeeCode_388_85.java @@ -0,0 +1,65 @@ +package com.company.leetcode.editor.cn; +//给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。 +// +// 示例: +// +// 输入: +//[ +// ["1","0","1","0","0"], +// ["1","0","1","1","1"], +// ["1","1","1","1","1"], +// ["1","0","0","1","0"] +//] +//输出: 6 +// Related Topics 栈 数组 哈希表 动态规划 + + + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution_85 { + public int maximalRectangle(char[][] matrix) { + + if (matrix == null || matrix.length == 0) { + return 0; + } + + int maxarea = 0; + int rows = matrix.length; + int cols = matrix[0].length; + int[][] dp = new int[rows][cols]; + + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + + if (matrix[i][j] == '0') { + continue; + } + + //update width + dp[i][j] = j == 0 ? 1 : (dp[i][j - 1] + 1); + int width = dp[i][j]; + for (int k = i; k >= 0; k--) { + width = Math.min(width,dp[k][j]); + maxarea = Math.max(maxarea,width * (i - k + 1)); + } + } + } + + return maxarea; + + } + +// public static void main(String[] args) { +// Solution s = new Solution(); +// +// char[][] matrix = new char[][]{ +// new char[]{'1','0','1','0','0'}, +// new char[]{'1','0','1','1','1'}, +// new char[]{'1','1','1','1','1'}, +// new char[]{'1','0','0','1','0'} +// }; +// +// System.out.println(s.maximalRectangle(matrix)); +// } +} +//leetcode submit region end(Prohibit modification and deletion) diff --git a/Week 08/id_388/LeeCode_388_91.java b/Week 08/id_388/LeeCode_388_91.java new file mode 100644 index 000000000..1a7ba2a86 --- /dev/null +++ b/Week 08/id_388/LeeCode_388_91.java @@ -0,0 +1,71 @@ +package com.company.leetcode.editor.cn; +//一条包含字母 A-Z 的消息通过以下方式进行了编码: +// +// 'A' -> 1 +//'B' -> 2 +//... +//'Z' -> 26 +// +// +// 给定一个只包含数字的非空字符串,请计算解码方法的总数。 +// +// 示例 1: +// +// 输入: "12" +//输出: 2 +//解释: 它可以解码为 "AB"(1 2)或者 "L"(12)。 +// +// +// 示例 2: +// +// 输入: "226" +//输出: 3 +//解释: 它可以解码为 "BZ" (2 26), "VF" (22 6), 或者 "BBF" (2 2 6) 。 +// +// Related Topics 字符串 动态规划 + + +import java.util.*; + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution_91 { + public int numDecodings(String s) { + if (s == null || s.length() == 0) { + return 0; + } + + int[] dp = new int[s.length() + 1]; + dp[0] = 1; + if (s.charAt(0) == '0') { + return 0; + } else { + dp[1] = 1; + } + for (int i = 2; i <= s.length(); i++) { + char c = s.charAt(i - 1); + char pre = s.charAt( i - 2); + if (c == '0') { + //处理 10,20 + if (pre == '1' || pre == '2') { + dp[i] = dp[i - 2]; + continue; + } + return 0; + } + + + if ((pre == '1') || (pre == '2' && (c >= '1' && c <= '6'))) { //1* 2* + dp[i] = dp[i - 1] + dp[i - 2]; + continue; + } + dp[i] = dp[i - 1]; + } + return dp[s.length()]; + } + +// public static void main(String[] args) { +// Solution s = new Solution(); +// System.out.println(s.numDecodings("100")); +// } +} +//leetcode submit region end(Prohibit modification and deletion) diff --git a/Week 08/id_388/LeeCode_388_917.java b/Week 08/id_388/LeeCode_388_917.java new file mode 100644 index 000000000..0b533a49c --- /dev/null +++ b/Week 08/id_388/LeeCode_388_917.java @@ -0,0 +1,85 @@ +package com.company.leetcode.editor.cn; +//给定一个字符串 S,返回 “反转后的” 字符串,其中不是字母的字符都保留在原地,而所有字母的位置发生反转。 +// +// +// +// +// +// +// 示例 1: +// +// 输入:"ab-cd" +//输出:"dc-ba" +// +// +// 示例 2: +// +// 输入:"a-bC-dEf-ghIj" +//输出:"j-Ih-gfE-dCba" +// +// +// 示例 3: +// +// 输入:"Test1ng-Leet=code-Q!" +//输出:"Qedo1ct-eeLg=ntse-T!" +// +// +// +// +// 提示: +// +// +// S.length <= 100 +// 33 <= S[i].ASCIIcode <= 122 +// S 中不包含 \ or " +// +// Related Topics 字符串 + + +import java.util.ArrayList; +import java.util.List; + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution_917 { + public String reverseOnlyLetters(String S) { + if (S == null || S.equals("")) { + return ""; + } + + if (S.length() == 1) { + return S; + } + + char[] cs = S.toCharArray(); + int i = 0; + int j = cs.length - 1; + while (i < j) { + //sweep + if (isAlpha(S.charAt(i)) && isAlpha(S.charAt(j))) { + char tmp = cs[i]; + cs[i++] = cs[j]; + cs[j--] = tmp; + } + while (i < j && !isAlpha(S.charAt(i))) { + i++; + } + while (i < j && !isAlpha(S.charAt(j))) { + j--; + } + } + + return new String(cs); + } + + private boolean isAlpha(char c) { + return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); + } + +// public static void main(String[] args) { +// Solution s = new Solution(); +// //Wrong Answer: input:"a-bC-dEf-ghIj" Output:"jIhg-fEd-Cb-a" Expected:"j-Ih-gfE-dCba" +// System.out.println(s.reverseOnlyLetters("a-bC-dEf-ghIj")); +// } + +} +//leetcode submit region end(Prohibit modification and deletion) diff --git a/Week 08/id_388/NOTE.md b/Week 08/id_388/NOTE.md index a6321d6e2..eef0375c4 100755 --- a/Week 08/id_388/NOTE.md +++ b/Week 08/id_388/NOTE.md @@ -1,4 +1,39 @@ # NOTE - +### 1、状态转移方程总结 + 爬楼梯:fn = f(n-1) + f(n - 2); + 不同路径:obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1]; + 打家劫舍:dp[n] = MAX( dp[n-1], dp[n-2] + num ) + 股票买卖:dp[i][k][0] = max(dp[i-1][k][0], dp[i-1][k][1] + prices[i]),dp[i][k][1] = max(dp[i-1][k][1], dp[i-1][k-1][0] - prices[i]) + 编辑距离:min(dp[i - 1][j],dp[i][j - 1],dp[i - 1][j - 1]) + 1 + 不同子序列: if (s.charAt(i - 1) == t.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1]; + } else { + dp[i][j] = dp[i][j - 1]; + } + 正则表达式:if (p.charAt(j) == '.') { + dp[i + 1][j + 1] = dp[i][j]; + } + if (p.charAt(j) == s.charAt(i)) { + dp[i + 1][j + 1] = dp[i][j]; + } + if (p.charAt(j) == '*') { + if (p.charAt(j-1) != s.charAt(i) && p.charAt(j - 1) != '.') { + dp[i + 1][j + 1] = dp[i + 1][j - 1]; + } else { + dp[i + 1][j + 1] = (dp[i + 1][j] || dp[i][j + 1] || dp[i + 1][j - 1]); + } + } + +### 2、java 字符串比较 + String x = “abb”; String y = “abb”; + x == y —-> false x.equals(y) —-> true + x.equalsIgnoreCase(y) —-> true + 字符串比较 + + Rabin-Karp 算法的思想: + 1.假设子串的长度为 M (pat),目标字符串的长度为 N (txt) + 2.计算子串的 hash 值 hash_pat + 3.计算目标字符串txt中每个长度为 M 的子串的 hash 值(共需要计算 N-M+1次) + 4.比较 hash 值:如果 hash 值不同,字符串必然不匹配; 如果 hash 值相同,还需要使用朴素算法再次判断 From a65337d44e543229781c08e3dc2a7ab39288766a Mon Sep 17 00:00:00 2001 From: liushulong Date: Tue, 10 Dec 2019 21:51:48 +0800 Subject: [PATCH 2/2] add --- .idea/algorithm004-03.iml | 9 --- .idea/misc.xml | 7 --- .idea/modules.xml | 8 --- .idea/vcs.xml | 6 -- .idea/workspace.xml | 127 -------------------------------------- 5 files changed, 157 deletions(-) delete mode 100644 .idea/algorithm004-03.iml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml delete mode 100644 .idea/workspace.xml diff --git a/.idea/algorithm004-03.iml b/.idea/algorithm004-03.iml deleted file mode 100644 index d6ebd4805..000000000 --- a/.idea/algorithm004-03.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index f091b54e2..000000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 24b8cf3cb..000000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1ddfb..000000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index 87dff4dee..000000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,127 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1571496109469 - - - - - - - - - \ No newline at end of file