classSolution{publicbooleanrotateString(String A, String B){if (A == null || B == null || A.length() != B.length()) {returnfalse;}String C = B + B;return C.indexOf(A) == -1 ? false : true;}
}
復制代碼
Closest Two Sum max average sum subetree 兩種版本: 1.二叉樹 2.多個子節(jié)點 merge two Sorted List 一排數(shù)據(jù)中心,每個數(shù)據(jù)中心鏈接有cost, 求將所有數(shù)據(jù)中心連接起來并且cost最小的方式 Leetcode 819 Leetcode 973 K Closest Points to Origin
https://en.wikipedia.org/wiki/Lee_algorithm Lee Algorithm:
initialize
wave expansion: repeat mark all unlabeled neighbors of points marked with i with i + 1
go to target point -> repeat go to next node that has a lower mark than the current node
clear marks
Reorder 字符的字典序比較 dictionary order alphabetical order "ah1".compareTo("ahb") 比較的就是兩個字符的ASCII差值,如果小于零說明第一個字符串小于第二個字符串
64. Minimum Path Sum
題目: 給出一個m * n的方格,找出一條從左上到右下和最小的路徑, 只可以向下或向右移動
思路: 局部最小和全局最小之間的關(guān)系?全局最小也是局部最小
publicclassSolution{publicintminPathSum(int[][] grid){if (grid == null || grid.length == 0 || grid[0].length == 0) {return0;}int M = grid.length;int N = grid[0].length;int[][] sum = newint[M][N];sum[0][0] = grid[0][0];for (int i = 1; i < M; i++) {sum[i][0] = sum[i - 1][0] + grid[i][0];}for (int i = 1; i < N; i++) {sum[0][i] = sum[0][i - 1] + grid[0][i];}for (int i = 1; i < M; i++) {for (int j = 1; j < N; j++) {sum[i][j] = Math.min(sum[i - 1][j], sum[i][j - 1]) + grid[i][j];}}return sum[M - 1][N - 1];}
}復制代碼
K - 1 distinct characters
問題: 給一個字符串和一個整數(shù)數(shù)字
輸入: inputString = "awaglk" num = 4
輸出: ["awag"]
思路: 雙指針, 1個指針固定頭, 另一個直接
import java.util.*;
publicclassSolution{publicstatic List<String> Kdistinct(String s, int k){List<String> res = new ArrayList<>();if (s == null || s.length() == 0) {return res;} int size = s.length();int[] count = newint[128];int distinctCount = 0;for (int i = 0; i < size; i++) {Arrays.fill(count, 0);distinctCount = 0;for (int j = i; j < size; j++) {if (count[s.charAt(j)] == 0) {distinctCount++;}System.out.println("i: " + i);System.out.println("j: " + j);System.out.println("distinct: " + distinctCount);if (distinctCount == k - 1 && j - i + 1== k) {res.add(s.substring(i, j + 1));}count[s.charAt(j)]++;}}return res;}publicstaticvoidmain(String[] args){int n = 4;String input = "awagkmg";List<String> res = Kdistinct(input, n);for (String s : res) { System.out.println(s);}}
}
復制代碼
K-Substring with K different characters
publicclassSolution{/*** @param stringIn: The original string.* @param K: The length of substrings.* @return: return the count of substring of length K and exactly K distinct characters.*/publicintKSubstring(String stringIn, int K){if (stringIn == null || stringIn.length() == 0 || K <= 0) {return0;}int count = 0;HashMap<Character, Integer> charMap = new HashMap<>();HashSet<String> resultSet = new HashSet<String>();int len = stringIn.length();int j = 0;for (int i = 0; i < len; i++) {while (j < len && charMap.size() < K) {char c = stringIn.charAt(j);if (charMap.containsKey(c)) {break;}charMap.put(c, 1);j++;// size == k是保證了子串長度為kif (charMap.size() == K) {resultSet.add(stringIn.substring(i, j));}}charMap.remove(stringIn.charAt(i));}return resultSet.size();}
}
復制代碼
937. Reorder Log Files
問題: 本質(zhì)上是字符串的字典序比較 分為三種情況討論 identifier > letter > digit
思路: 自定義一個Comparator進行比較
classSolution{public String[] reorderLogFiles(String[] logs) {Comparator<String> com = new Comparator<String>() {@Overridepublicintcompare(String s1, String s2){int s1Start = s1.indexOf(' ');int s2Start = s2.indexOf(' ');char s1Char = s1.charAt(s1Start + 1);char s2Char = s2.charAt(s2Start + 1);if (s1Char <= '9') {if (s2Char <= '9') { // natural orderreturn0;} else { // s1 is digit-log, s2 is letter-logreturn1;}}if (s2Char <= '9') { // s2 is digit-log, s1 is letter-logreturn -1;}// s1 and s2 are both letter-logint preCompute = s1.substring(s1Start + 1).compareTo(s2.substring(s2Start + 1));// ignore identifier letter-logs are ordered tieif (preCompute == 0) {return s1.substring(0, s1Start).compareTo(s2.substring(0, s2Start));}return preCompute;}};Arrays.sort(logs, com);return logs;}
}
復制代碼