Free Talk
最近一直刷 Array Tags,有点无聊了,找了一份常见算法数组题图片,打算开启 String Tags 新篇章了。
Finished Problem
最长有效括号
32. 最长有效括号
错误解法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 class Solution { public int longestValidParentheses (String s) { Stack<Character> st = new Stack<Character>(); int number = 0 ; for (int i = 0 ; i < s.length(); i ++ ){ char ch = s.charAt(i); if (ch == '(' ){ st.push(ch); continue ; } if (ch == ')' ){ if (!st.empty() && st.pop() == '(' ){ number ++; } } } return number * 2 ; } }
我看到这题是困难问题,先紧张了一下,后面马上就想出了上面的思路,一开始信心慢慢,顺利通过了测试,但是提交的时候就一直过不去了。去看了一下题解,发现自己理解错题目意思了。是计算最长子串 ‘()’ 长度,而不是我理解的合理的字符串长度。
正确解法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 class Solution { public int longestValidParentheses (String s) { int maxLength = 0 ; Stack<Integer> st = new Stack<Integer>(); st.push(-1 ); for (int i = 0 ; i < s.length(); i ++ ){ if (s.charAt(i) == '(' ){ st.push(i); }else { st.pop(); if (st.empty()){ st.push(i); } else { maxLength = Math.max(maxLength, i - st.peek()); } } } return maxLength; } }
执行结果
字母异位词分组
49. 字母异位词分组
排序数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class Solution { public List<List<String>> groupAnagrams(String[] strs) { if (strs.length == 0 ) return new ArrayList(); Map<String, List> ans = new HashMap<String, List>(); for (String s: strs){ char [] ch = s.toCharArray(); Arrays.sort(ch); String key = String.valueOf(ch); if (!ans.containsKey(key)) ans.put(key, new ArrayList()); ans.get(key).add(s); } return new ArrayList(ans.values()); } }
执行结果
常用API
获取 String 数组长度:String.length;
字符串 转换为 字符数组:char[] = String.toCharArray();
字符数组 转为 字符串:String = String.valueOf( char[] ) ;
HashMap
Put:HashMap.put(key, value);
Get:HashMap.get(key) return value;
Key 的存在:HashMap.containsKey(key);
编辑距离
72. 编辑距离
动态规划
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 class Solution { public int minDistance (String word1, String word2) { int m = word1.length(); int n = word2.length(); int [][] dp = new int [m + 1 ][n + 1 ]; if (m * n == 0 ){ return m + n; } for (int i = 0 ; i < m + 1 ; i ++ ) dp[i][0 ] = i; for (int j = 0 ; j < n + 1 ; j ++ ) dp[0 ][j] = j; for (int i = 1 ; i < m + 1 ; i ++ ){ for (int j = 1 ; j < n + 1 ; j ++){ int left = dp[i - 1 ][j] + 1 ; int down = dp[i][j - 1 ] + 1 ; int left_down = dp[i - 1 ][j - 1 ]; if (word1.charAt(i - 1 ) != word2.charAt(j - 1 )) { left_down = dp[i - 1 ][j - 1 ] + 1 ; } dp[i][j] = Math.min(left, Math.min(down, left_down)); } } return dp[m][n]; } }
执行结果
回文子串
647. 回文子串
动态规划
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 class Solution { public int countSubstrings (String s) { int n = s.length(); int count = 0 ; boolean [][] dp = new boolean [n][n]; for (int j = 0 ; j < n ; j ++ ){ for (int i = 0 ; i <= j; i ++ ){ if (s.charAt(i) == s.charAt(j) && (((j - i) < 2 ) || dp[i + 1 ][j - 1 ])){ dp[i][j] = true ; count ++; } } } return count; } }
执行结果
覆盖最小子串
76. 最小覆盖子串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 class Solution { public String minWindow (String s, String t) { if (s.length() == 0 || t.length() == 0 || s.length() < t.length()){ return "" ; } int [] need = new int [128 ]; int [] window = new int [128 ]; int count = 0 ; int left = 0 ; int right = 0 ; int minLength = s.length(); String minString = "" ; for (int i = 0 ; i < t.length(); i ++ ){ need[t.charAt(i)] ++; } while (right < s.length()){ char ch = s.charAt(right); window[ch] ++; if (need[ch] > 0 && need[ch] >= window[ch]){ count ++; } while (count == t.length()){ ch = s.charAt(left); if (need[ch] > 0 && need[ch] == window[ch]){ count --; } if (right - left + 1 <= minLength){ minLength = right - left + 1 ; minString = s.substring(left, right + 1 ); } window[ch] --; left ++; } right ++; } return minString; } }
执行结果
完结打卡
把 LeetCode Hot 100 里面的 String Tags 都刷完啦!!!