diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..e830cf11 Binary files /dev/null and b/.DS_Store differ diff --git a/.idea/$CACHE_FILE$ b/.idea/$CACHE_FILE$ new file mode 100644 index 00000000..b3418529 --- /dev/null +++ b/.idea/$CACHE_FILE$ @@ -0,0 +1,19 @@ + + + + + + + + + + + + + Android + + + + + + \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..5c98b428 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/algorithm007-class02.iml b/.idea/algorithm007-class02.iml new file mode 100644 index 00000000..d6ebd480 --- /dev/null +++ b/.idea/algorithm007-class02.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/checkstyle-idea.xml b/.idea/checkstyle-idea.xml new file mode 100644 index 00000000..e61c523d --- /dev/null +++ b/.idea/checkstyle-idea.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 00000000..681f41ae --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,116 @@ + + + + + + + +
+ + + + xmlns:android + + ^$ + + + +
+
+ + + + xmlns:.* + + ^$ + + + BY_NAME + +
+
+ + + + .*:id + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + .*:name + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + name + + ^$ + + + +
+
+ + + + style + + ^$ + + + +
+
+ + + + .* + + ^$ + + + BY_NAME + +
+
+ + + + .* + + http://schemas.android.com/apk/res/android + + + ANDROID_ATTRIBUTE_ORDER + +
+
+ + + + .* + + .* + + + BY_NAME + +
+
+
+
+
+
\ No newline at end of file diff --git a/.idea/dictionaries b/.idea/dictionaries new file mode 100644 index 00000000..7dde272c --- /dev/null +++ b/.idea/dictionaries @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 00000000..97626ba4 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..51cc4e93 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..712e4eb4 --- /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 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Week_01/.DS_Store b/Week_01/.DS_Store new file mode 100644 index 00000000..0d4294ea Binary files /dev/null and b/Week_01/.DS_Store differ diff --git a/Week_03/.DS_Store b/Week_03/.DS_Store new file mode 100644 index 00000000..e2f9dbe7 Binary files /dev/null and b/Week_03/.DS_Store differ diff --git a/Week_04/.DS_Store b/Week_04/.DS_Store new file mode 100644 index 00000000..017ae2cd Binary files /dev/null and b/Week_04/.DS_Store differ diff --git a/Week_06/.DS_Store b/Week_06/.DS_Store new file mode 100644 index 00000000..47735388 Binary files /dev/null and b/Week_06/.DS_Store differ diff --git a/Week_06/G20200343040202/LeetCode_221_0202.java b/Week_06/G20200343040202/LeetCode_221_0202.java new file mode 100644 index 00000000..e5892482 --- /dev/null +++ b/Week_06/G20200343040202/LeetCode_221_0202.java @@ -0,0 +1,16 @@ +class Solution { + public int maximalSquare(char[][] matrix) { + int rows = matrix.length, cols = rows > 0 ? matrix[0].length : 0; + int[][] dp = new int[rows + 1][cols + 1]; + int maxsqlen = 0; + for (int i = 1; i <= rows; i++) { + for (int j = 1; j <= cols; j++) { + if (matrix[i-1][j-1] == '1'){ + dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1; + maxsqlen = Math.max(maxsqlen, dp[i][j]); + } + } + } + return maxsqlen * maxsqlen; + } +} \ No newline at end of file diff --git a/Week_06/G20200343040202/LeetCode_621_0202.java b/Week_06/G20200343040202/LeetCode_621_0202.java new file mode 100644 index 00000000..684b2087 --- /dev/null +++ b/Week_06/G20200343040202/LeetCode_621_0202.java @@ -0,0 +1,56 @@ +class Solution { + public int[][] imageSmoother(int[][] M) { + int r = M.length; + int c = M[0].length; + int[][] res = new int[r][c]; + for(int i = 0; i < r;i++){ + for(int j = 0;j < c;j++){ + res[i][j] = add(M,i,j,r,c); + } + } + return res; + } + + private int add(int[][] M,int i,int j,int m ,int n){ + int res = 0; + int num = 0; + if(i-1>=0){ + res += M[i-1][j]; + num++; + } + if(i+1 < m){ + res += M[i+1][j]; + num++; + } + if(j-1 >= 0){ + res += M[i][j-1]; + num++; + } + if(j+1 < n){ + res += M[i][j+1]; + num++; + } + + if(i+1 < m &&j-1 >=0){ + res += M[i+1][j-1]; + num++; + } + if(i-1>=0&&j-1>=0){ + res += M[i-1][j-1]; + num++; + } + if(i-1>=0&&j+1= 0; i--) { + if (s.charAt(i) == s.charAt(j) && ((j - i < 2) || dp[i + 1][j - 1])) { + dp[i][j] = true; + res++; + } + } + } + return res; + } +} \ No newline at end of file diff --git a/Week_06/G20200343040202/LeetCode_64_0202.java b/Week_06/G20200343040202/LeetCode_64_0202.java new file mode 100644 index 00000000..99007ef0 --- /dev/null +++ b/Week_06/G20200343040202/LeetCode_64_0202.java @@ -0,0 +1,20 @@ +class Solution { + public int minPathSum(int[][] grid) { + + int dp[][] = new int[grid.length][grid[0].length]; + dp[0][0] = grid[0][0]; + + for (int i = 1; i < grid.length; i++) { + dp[i][0] = grid[i][0] + dp[i - 1][0]; + } + for (int i = 1; i < grid[0].length; i++) { + dp[0][i] = grid[0][i] + dp[0][i - 1]; + } + for (int i = 1; i < grid.length; i++) { + for (int j = 1; j < grid[0].length; j++) { + dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]; + } + } + return dp[grid.length - 1][grid[0].length - 1]; + } +} \ No newline at end of file diff --git a/Week_06/G20200343040202/LeetCode_76_0202.java b/Week_06/G20200343040202/LeetCode_76_0202.java new file mode 100644 index 00000000..af0c5ef1 --- /dev/null +++ b/Week_06/G20200343040202/LeetCode_76_0202.java @@ -0,0 +1,49 @@ +class Solution { + public String minWindow(String s, String t) { + if (s == null || s == "" || t == null || t == "" || s.length() < t.length()) { + return ""; + } + + int[] needs = new int[128]; + int[] window = new int[128]; + + for (int i = 0; i < t.length(); i++) { + needs[t.charAt(i)]++; + } + + int left = 0; + int right = 0; + + String res = ""; + + int count = 0; + + int minLength = s.length() + 1; + + while (right < s.length()) { + char ch = s.charAt(right); + window[ch]++; + if (needs[ch] > 0 && needs[ch] >= window[ch]) { + count++; + } + + while (count == t.length()) { + ch = s.charAt(left); + if (needs[ch] > 0 && needs[ch] >= window[ch]) { + count--; + } + if (right - left + 1 < minLength) { + minLength = right - left + 1; + res = s.substring(left, right + 1); + + } + window[ch]--; + left++; + + } + right++; + + } + return res; + } +} \ No newline at end of file diff --git a/Week_06/G20200343040202/LeetCode_91_0202.java b/Week_06/G20200343040202/LeetCode_91_0202.java new file mode 100644 index 00000000..d589bf38 --- /dev/null +++ b/Week_06/G20200343040202/LeetCode_91_0202.java @@ -0,0 +1,25 @@ +class Solution { + public int numDecodings(String s) { + char[] arr = s.toCharArray(); + int[] dp = new int[s.length() + 1]; + dp[0] = 1; + dp[1] = arr[0] == '0' ? 0 : 1; + if (s.length() <= 1) return dp[1]; + for (int i = 2; i <= s.length(); i++) { + int n = (arr[i - 2] - '0') * 10 + (arr[i - 1] - '0'); + if (arr[i - 1] == '0' && arr[i - 2] == '0') { + return 0; + } else if (arr[i - 2] == '0') { + dp[i] = dp[i - 1]; + } else if (arr[i - 1] == '0') { + if (n > 26) return 0; + dp[i] = dp[i - 2]; + } else if (n > 26) { + dp[i] = dp[i - 1]; + } else { + dp[i] = dp[i - 1] + dp[i - 2]; + } + } + return dp[dp.length - 1]; + } +} \ No newline at end of file diff --git a/Week_07/G20200343040202/LeetCode_127_0202.java b/Week_07/G20200343040202/LeetCode_127_0202.java new file mode 100644 index 00000000..9bdd7f4d --- /dev/null +++ b/Week_07/G20200343040202/LeetCode_127_0202.java @@ -0,0 +1,91 @@ +class Solution { + + public int ladderLength(String beginWord, String endWord, List wordList) { + HashSet start = new HashSet<>(); + start.add(beginWord); + HashSet end = new HashSet<>(); + end.add(endWord); + HashSet words = new HashSet<>(wordList); + if (!words.contains(endWord)) { + return 0; + } + + return deBfs(start, end, words, 2); + } + + private int deBfs(HashSet start, HashSet end, HashSet words, int depth) { + + if (start.size() > end.size()) { + return deBfs(end, start, words, depth); + } + words.removeAll(start); + HashSet next = new HashSet<>(); + for (String str : start) { + char[] chars = str.toCharArray(); + for (int i = 0; i < chars.length; i++) { + char temp = chars[i]; + for (char j = 'a'; j <= 'z'; j++) { + chars[i] = j; + String word = new String(chars); + if (words.contains(word)) { + if (end.contains(word)) { + return depth; + } + next.add(word); + } + } + chars[i] = temp; + } + } + if (start.isEmpty()) { + return 0; + } + return deBfs(next, end, words, depth + 1); + + } +// public int ladderLength(String beginWord, String endWord, List wordList) { +// if (!wordList.contains(endWord)) +// return 0; +// +// Set visited = new HashSet<>(); +// Queue queue = new LinkedList<>(); +// queue.add(beginWord); +// +// int res = 0; +// while (!queue.isEmpty()) { +// res++; +// for (int i = 0; i < queue.size(); i++) { +// String start = queue.poll(); +// for (String string : wordList) { +// if (visited.contains(string)) { +// continue; +// } +// if (!progress(start, string)) { +// continue; +// } +// if (string.equals(endWord)) { +// return res + 1; +// } +// visited.add(string); +// queue.offer(string); +// } +// } +// } +// return 0; +// } +// private boolean progress(String s1, String s2) { +// if (s1.length() != s2.length()) { +// return false; +// } +// int count = 0; +// for (int i = 0; i < s1.length(); i++) { +// if (s1.charAt(i) != s2.charAt(i)) { +// count++; +// if (count > 1) { +// return false; +// } +// } +// } +// return count == 1; +// } +} \ No newline at end of file diff --git a/Week_07/G20200343040202/LeetCode_130_0202.java b/Week_07/G20200343040202/LeetCode_130_0202.java new file mode 100644 index 00000000..5ed4b309 --- /dev/null +++ b/Week_07/G20200343040202/LeetCode_130_0202.java @@ -0,0 +1,29 @@ +class Solution { + + public void solve(char[][] board) { + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[i].length; j++) { + boolean isEdge = i == 0 || j == 0 || i == board.length - 1 || j == board[i].length - 1; + if (isEdge && board[i][j] == 'O') { + solveDfs(board, i, j); + } + } + } + + for(int i=0;i= board.length || j < 0 || j >= board[0].length || board[i][j] == 'X' || board[i][j] == '1') { + return; + } + board[i][j] = '1'; + solveDfs(board, i - 1, j); + solveDfs(board, i + 1, j); + solveDfs(board, i, j - 1); + solveDfs(board, i, j + 1); + } +} \ No newline at end of file diff --git a/Week_07/G20200343040202/LeetCode_200_0202.java b/Week_07/G20200343040202/LeetCode_200_0202.java new file mode 100644 index 00000000..dc718ed6 --- /dev/null +++ b/Week_07/G20200343040202/LeetCode_200_0202.java @@ -0,0 +1,28 @@ +class Solution { + + public int numIslands(char[][] grid) { + + int count = 0; + + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[i].length; j++) { + if (grid[i][j] == '1') { + count++; + dfs(grid, i, j); + } + } + } + return count; + } + + private void dfs(char[][] grid, int i, int j) { + if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == '0') { + return; + } + grid[i][j] = '0'; + dfs(grid, i - 1, j); + dfs(grid, i + 1, j); + dfs(grid, i, j - 1); + dfs(grid, i, j + 1); + } +} \ No newline at end of file diff --git a/Week_07/G20200343040202/LeetCode_208_0202.java b/Week_07/G20200343040202/LeetCode_208_0202.java new file mode 100644 index 00000000..3ade0388 --- /dev/null +++ b/Week_07/G20200343040202/LeetCode_208_0202.java @@ -0,0 +1,62 @@ +public class Trie { + + private TreeNode node; + + /** Initialize your data structure here. */ + public Trie() { + node = new TreeNode(); + } + + /** Inserts a word into the trie. */ + public void insert(String word) { + TreeNode node = this.node; + for (char c:word.toCharArray()){ + if (node.child[c - 'a'] == null){ + node.child[c - 'a'] = new TreeNode(); + } + node = node.child[c - 'a']; + } + node.value = word; + } + + /** Returns if the word is in the trie. */ + public boolean search(String word) { + TreeNode node = this.node; + for (char c:word.toCharArray()){ + if (node.child[c - 'a'] == null){ + return false; + } + node = node.child[c - 'a']; + } + return node.value.equals(word); + } + + /** Returns if there is any word in the trie that starts with the given prefix. */ + public boolean startsWith(String prefix) { + TreeNode node = this.node; + for (char c:prefix.toCharArray()){ + if (node.child[c - 'a'] == null){ + return false; + } + node = node.child[c - 'a']; + } + return true; + } + + public TreeNode next(char c){ + if ( 'a' <= c && c <= 'z'){ + return node.child[c]; + } + return null; + } + + class TreeNode{ + String value; + TreeNode[] child; + + public TreeNode(){ + value = ""; + child = new TreeNode[26]; + } + } +} \ No newline at end of file diff --git a/Week_07/G20200343040202/LeetCode_212_0202.java b/Week_07/G20200343040202/LeetCode_212_0202.java new file mode 100644 index 00000000..0028e3d0 --- /dev/null +++ b/Week_07/G20200343040202/LeetCode_212_0202.java @@ -0,0 +1,48 @@ +class Solution { + + public List findWords(char[][] board, String[] words) { + List list = new ArrayList<>(); + for (String word : words) { + if (exist(board, word) && !list.contains(word)) { + list.add(word); + } + } + return list; + } + + private boolean exist(char[][] board, String word) { + if (word.length() == 0 || board.length == 0) { + return false; + } + + boolean[][] flag = new boolean[board.length][board[0].length]; + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[i].length; j++) { + if (board[i][j] == word.charAt(0)) { + if (find(board, word, 0, i, j, flag)) { + return true; + } + } + } + } + return false; + } + + private boolean find(char[][] board, String word, int index, int i, int j, boolean[][] flag) { + if (index == word.length()) { + return true; + } + boolean result = false; + if (i >= 0 && i < board.length && j >= 0 && j < board[i].length && !flag[i][j] && word.charAt(index) == board[i][j]) { + flag[i][j] = true; + index += 1; + result = find(board, word, index, i + 1, j, flag) || find(board, word, index, i - 1, j, flag) || + find(board, word, index, i, j + 1, flag) || find(board, word, index, i, j - 1, flag); + if (result == false) { + index -= 1; + flag[i][j] = false; + } + } + return result; + } +} \ No newline at end of file diff --git a/Week_07/G20200343040202/LeetCode_22_0202.java b/Week_07/G20200343040202/LeetCode_22_0202.java new file mode 100644 index 00000000..aa0d6a17 --- /dev/null +++ b/Week_07/G20200343040202/LeetCode_22_0202.java @@ -0,0 +1,23 @@ +class Solution { + + public List generateParenthesis(int n) { + List list = new ArrayList<>(); + generate(0,0,n,"",list); + return list; + } + + private void generate(int left, int right, int n,String s, List list) { + + //条件 + if (left == n && right == n){ + list.add(s); + return; + } + + if (left < n) + generate(left+1,right,n,s+"(",list); + if (left > right) + generate(left,right+1,n,s+")",list); + + } +} \ No newline at end of file diff --git a/Week_07/G20200343040202/LeetCode_36_0202.java b/Week_07/G20200343040202/LeetCode_36_0202.java new file mode 100644 index 00000000..35493e97 --- /dev/null +++ b/Week_07/G20200343040202/LeetCode_36_0202.java @@ -0,0 +1,22 @@ +class Solution { + + public boolean isValidSudoku(char[][] board) { + int[][] rows = new int[9][10]; + int[][] cols = new int[9][10]; + int[][] boxs = new int[9][10]; + + for (int i = 0; i < 9; ++i) { + for (int j = 0; j < 9; ++j) { + if (board[i][j] == '.') continue; + int num = board[i][j] - '0'; + rows[i][num]++; + cols[j][num]++; + boxs[(i/3)*3 + j/3][num]++; + if (rows[i][num] + cols[j][num] + boxs[(i/3)*3 + j/3][num] > 3) { + return false; + } + } + } + return true; + } +} \ No newline at end of file diff --git a/Week_07/G20200343040202/LeetCode_433_0202.java b/Week_07/G20200343040202/LeetCode_433_0202.java new file mode 100644 index 00000000..4561881d --- /dev/null +++ b/Week_07/G20200343040202/LeetCode_433_0202.java @@ -0,0 +1,48 @@ +class Solution { + + public int minMutation(String start, String end, String[] bank) { + + boolean[] visited = new boolean[bank.length]; + Queue queue = new LinkedList<>(); + queue.add(start); + //深度 + int res = 0; + while (!queue.isEmpty()) { + int size = queue.size(); + res++; + for (int i = 0; i < size; i++) { + String poll = queue.poll(); + for (int j = 0; j < bank.length; j++) { + //已经访问过的元素跳过,树中不存在重复节点 + if (visited[j]) { + continue; + } + //孩子节点只能改变一个字符转换为s,不满足则跳过 + String string = bank[j]; + if (!progress(poll, string)) { + continue; + } + //如果节点s等于endWord,接龙完成 + if (string.equals(end)) { + return res; + } + visited[j] = true; + queue.add(string); + } + } + } + return 0; + } + private boolean progress(String poll, String s) { + int count = 0; + for (int i = 0; i < s.length(); i++) { + if (poll.charAt(i) != s.charAt(i)) { + count++; + if (count > 1) { + return false; + } + } + } + return count == 1; + } +} \ No newline at end of file diff --git a/Week_07/G20200343040202/LeetCode_547_0202.java b/Week_07/G20200343040202/LeetCode_547_0202.java new file mode 100644 index 00000000..8af7b2f7 --- /dev/null +++ b/Week_07/G20200343040202/LeetCode_547_0202.java @@ -0,0 +1,51 @@ +class Solution { + + public int find(int parent[], int i) { + if (parent[i] == -1) + return i; + return find(parent, parent[i]); + } + public void union(int parent[], int x, int y) { + int xset = find(parent, x); + int yset = find(parent, y); + if (xset != yset) + parent[xset] = yset; + } + public int findCircleNum(int[][] M) { + int[] parent = new int[M.length]; + Arrays.fill(parent, -1); + for (int i = 0; i < M.length; i++) { + for (int j = 0; j < M.length; j++) { + if (M[i][j] == 1 && i != j) { + union(parent, i, j); + } + } + } + int count = 0; + for (int i = 0; i < parent.length; i++) { + if (parent[i] == -1) + count++; + } + return count; + } + + // public int findCircleNum(int[][] M) { + // int[] visited = new int[M.length]; + // int count = 0; + // for (int i = 0; i < M.length; i++) { + // if (visited[i] == 0) { + // findCircleNumDfs(M, visited, i); + // count++; + // } + // } + // return count; + // } + // private void findCircleNumDfs(int[][] M, int[] visited, int i) { + // for (int j = 0; j < M.length; j++) { + // if (M[i][j] == 1 && visited[j] == 0) { + // visited[j] = 1; + // findCircleNumDfs(M, visited, j); + // } + // } + // } +} \ No newline at end of file diff --git a/Week_07/G20200343040202/LeetCode_70_0202.java b/Week_07/G20200343040202/LeetCode_70_0202.java new file mode 100644 index 00000000..9e1e5fd7 --- /dev/null +++ b/Week_07/G20200343040202/LeetCode_70_0202.java @@ -0,0 +1,24 @@ +class Solution { + public int climbStairs(int n) { + if (n == 1) return 1; + if (n == 2) return 2; + int p = 2, q = 1; + for (int i = 2; i < n; i++) { + int sum = p + q; + q = p; + p = sum; + } + return p; + } +// public int climbStairs(int n) { +// if (n == 1) return 1; +// if (n == 2) return 2; +// 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]; +// } +} \ No newline at end of file diff --git a/Week_08/G20200343040202/LeetCode_1122_0202.java b/Week_08/G20200343040202/LeetCode_1122_0202.java new file mode 100644 index 00000000..f625a46f --- /dev/null +++ b/Week_08/G20200343040202/LeetCode_1122_0202.java @@ -0,0 +1,25 @@ +class Solution { + + public int[] relativeSortArray(int[] arr1, int[] arr2) { + int[] temp = new int[1001]; + for (int i : arr1) { + temp[i]++; + } + int index=0; + for (int j : arr2) { + while (temp[j] >0) { + arr1[index] = j; + temp[j]--; + index++; + } + } + for (int i=0; i0) { + arr1[index] = i; + temp[i]--; + index++; + } + } + return arr1; + } +} \ No newline at end of file diff --git a/Week_08/G20200343040202/LeetCode_146_0202.java b/Week_08/G20200343040202/LeetCode_146_0202.java new file mode 100644 index 00000000..000cef9f --- /dev/null +++ b/Week_08/G20200343040202/LeetCode_146_0202.java @@ -0,0 +1,104 @@ +class LRUCache { + + /** + * 链表节点 + */ + class CacheNode { + CacheNode prev;//前一节点 + CacheNode next;//后一节点 + int value;//值 + int key;//键 + CacheNode() { + } + } + public LRUCache(int capacity) { + currentSize = 0; + cacheSize = capacity; + nodes = new LinkedHashMap();//缓存容器 + } + + /** + * 获取缓存中对象 + * @param key + * @return + */ + public int get(int key) { + CacheNode node = (CacheNode) nodes.get(key); + if (node != null) { + moveToHead(node); + return node.value; + } else { + return -1; + } + } + + /** + * 添加缓存 + * @param key + * @param value + */ + public void put(int key, int value) { + CacheNode node = (CacheNode) nodes.get(key); + + if (node == null) { + //缓存容器是否已经超过大小. + if (currentSize >= cacheSize) { + if (last != null)//将最少使用的删除 + nodes.remove(last.key); + removeLast(); + } else { + currentSize++; + } + + node = new CacheNode(); + } + node.value = value; + node.key = key; + //将最新使用的节点放到链表头,表示最新使用的. + moveToHead(node); + nodes.put(key, node); + } + + /** + * 删除链表尾部节点 + * 表示 删除最少使用的缓存对象 + */ + private void removeLast() { + //链表尾不为空,则将链表尾指向null. 删除连表尾(删除最少使用的缓存对象) + if (last != null) { + if (last.prev != null) + last.prev.next = null; + else + first = null; + last = last.prev; + } + } + + /** + * 移动到链表头,表示这个节点是最新使用过的 + * @param node + */ + private void moveToHead(CacheNode node) { + if (node == first) + return; + if (node.prev != null) + node.prev.next = node.next; + if (node.next != null) + node.next.prev = node.prev; + if (last == node) + last = node.prev; + if (first != null) { + node.next = first; + first.prev = node; + } + first = node; + node.prev = null; + if (last == null) + last = first; + } + private int cacheSize; + private LinkedHashMap nodes;//缓存容器 + private int currentSize; + private CacheNode first;//链表头 + private CacheNode last;//链表尾 +} \ No newline at end of file diff --git a/Week_08/G20200343040202/LeetCode_190_0202.java b/Week_08/G20200343040202/LeetCode_190_0202.java new file mode 100644 index 00000000..2885bb3c --- /dev/null +++ b/Week_08/G20200343040202/LeetCode_190_0202.java @@ -0,0 +1,12 @@ +class Solution { + + public static int reverseBits(int n) { + int result = 0; + for (int i = 0; i < Integer.SIZE; i++) { + result <<= 1; + result |= n & 1; + n >>>= 1; + } + return result; + } +} \ No newline at end of file diff --git a/Week_08/G20200343040202/LeetCode_191_0202.java b/Week_08/G20200343040202/LeetCode_191_0202.java new file mode 100644 index 00000000..8f673deb --- /dev/null +++ b/Week_08/G20200343040202/LeetCode_191_0202.java @@ -0,0 +1,11 @@ +class Solution { + + public static int hammingWeight(int n) { + int count = 0; + while (n != 0) { + count++; + n = n & (n - 1); + } + return count; + } +} \ No newline at end of file diff --git a/Week_08/G20200343040202/LeetCode_231_0202.java b/Week_08/G20200343040202/LeetCode_231_0202.java new file mode 100644 index 00000000..6da415ab --- /dev/null +++ b/Week_08/G20200343040202/LeetCode_231_0202.java @@ -0,0 +1,11 @@ +class Solution { + + public boolean isPowerOfTwo(int n) { + if (n == 0) + return false; + while (n % 2 == 0) { + n /= 2; + } + return n == 1; + } +} \ No newline at end of file diff --git a/Week_08/G20200343040202/LeetCode_242_0202.java b/Week_08/G20200343040202/LeetCode_242_0202.java new file mode 100644 index 00000000..a44131df --- /dev/null +++ b/Week_08/G20200343040202/LeetCode_242_0202.java @@ -0,0 +1,15 @@ +class Solution { + + public static boolean isAnagram(String s, String t) { + if (s.length() != t.length()) return false; + char[] chars1 = s.toCharArray(); + Arrays.sort(chars1); + char[] chars2 = t.toCharArray(); + Arrays.sort(chars2); + + if (!String.valueOf(chars1).equals(String.valueOf(chars2))) { + return false; + } + return true; + } +} \ No newline at end of file diff --git a/Week_08/G20200343040202/LeetCode_56_0202.java b/Week_08/G20200343040202/LeetCode_56_0202.java new file mode 100644 index 00000000..5ff2d568 --- /dev/null +++ b/Week_08/G20200343040202/LeetCode_56_0202.java @@ -0,0 +1,31 @@ +class Solution { + + public int[][] merge(int[][] intervals) { + if (intervals == null || intervals.length <= 1) { + return intervals; + } + int mergeCount = 0; + for (int i = 0; i < intervals.length; i++) { + for (int j = i + 1; j < intervals.length; j++) { + if (intervals[i][1] >= intervals[j][0] && intervals[i][0] <= intervals[j][1]) { + if (intervals[i][1] > intervals[j][1]) { + intervals[j][1] = intervals[i][1]; + } + if (intervals[i][0] < intervals[j][0]) { + intervals[j][0] = intervals[i][0]; + } + intervals[i] = null; + mergeCount++; + break; + } + } + } + int[][] result = new int[intervals.length - mergeCount][]; + for (int i = 0, j = 0; j < intervals.length; j++) { + if (intervals[j] != null) { + result[i++] = intervals[j]; + } + } + return result; + } +} \ No newline at end of file diff --git a/Week_09/G20200343040202/LeetCode_151_0202.java b/Week_09/G20200343040202/LeetCode_151_0202.java new file mode 100644 index 00000000..22ac0bb8 --- /dev/null +++ b/Week_09/G20200343040202/LeetCode_151_0202.java @@ -0,0 +1,13 @@ +class Solution { + + public String reverseWords(String s) { + String[] arr = s.trim().split("\\s+"); + + StringBuilder builder = new StringBuilder(); + + for (int i = arr.length - 1; i >= 0; i--) { + builder.append(arr[i]).append(" "); + } + return builder.toString().trim(); + } +} \ No newline at end of file diff --git a/Week_09/G20200343040202/LeetCode_205_0202.java b/Week_09/G20200343040202/LeetCode_205_0202.java new file mode 100644 index 00000000..a4524511 --- /dev/null +++ b/Week_09/G20200343040202/LeetCode_205_0202.java @@ -0,0 +1,18 @@ +class Solution { + public boolean isIsomorphic(String s, String t) { + int[] arr1 = new int[256]; + int[] arr2 = new int[256]; + char[] s1 = s.toCharArray(); + char[] t1 = t.toCharArray(); + for (int i = 0; i < s1.length; i++) { + char c1 = s1[i]; + char c2 = t1[i]; + if (arr1[c1] != arr2[c2]) { + return false; + } + arr1[c1] = i + 1; + arr2[c2] = i + 1; + } + return true; + } +} \ No newline at end of file diff --git a/Week_09/G20200343040202/LeetCode_300_0202.java b/Week_09/G20200343040202/LeetCode_300_0202.java new file mode 100644 index 00000000..63b29b4e --- /dev/null +++ b/Week_09/G20200343040202/LeetCode_300_0202.java @@ -0,0 +1,16 @@ +class Solution { + public int lengthOfLIS(int[] nums) { + int[] dp = new int[nums.length]; + int max = 0; + for (int i=0;i map = new HashMap<>(); +// for (int i=0;i Integer.MAX_VALUE) return Integer.MAX_VALUE; + if (num < Integer.MIN_VALUE) return Integer.MIN_VALUE; + return num.intValue(); + } + + private static boolean isNumber(char c) { + String number = "0123456789"; + if (number.indexOf(c) == -1) { + return false; + } + return true; + } +} \ No newline at end of file diff --git a/Week_09/G20200343040202/LeetCode_917_0202.java b/Week_09/G20200343040202/LeetCode_917_0202.java new file mode 100644 index 00000000..0dd91a4a --- /dev/null +++ b/Week_09/G20200343040202/LeetCode_917_0202.java @@ -0,0 +1,18 @@ +class Solution { + public String reverseOnlyLetters(String S) { + Stack letters = new Stack(); + for (char c: S.toCharArray()) + if (Character.isLetter(c)) + letters.push(c); + + StringBuilder ans = new StringBuilder(); + for (char c: S.toCharArray()) { + if (Character.isLetter(c)) + ans.append(letters.pop()); + else + ans.append(c); + } + + return ans.toString(); + } +} \ No newline at end of file diff --git a/Week_09/G20200343040202/LeetCode_91_0202.java b/Week_09/G20200343040202/LeetCode_91_0202.java new file mode 100644 index 00000000..9cb9f904 --- /dev/null +++ b/Week_09/G20200343040202/LeetCode_91_0202.java @@ -0,0 +1,21 @@ +class Solution { + public int numDecodings(String s) { + + if (s == null || s.length() == 0) { + return 0; + } + int n = s.length(); + int[] dp = new int[n + 1]; + dp[0] = 1; + dp[1] = s.charAt(0) == '0' ? 0 : 1; + for (int i = 1; i < n; i++) { + char c = s.charAt(i); + char pre = s.charAt(i - 1); + dp[i + 1] = c == '0' ? 0 : dp[i]; + if (pre == '1' || (pre == '2' && c <= '6')) { + dp[i + 1] += dp[i - 1]; + } + } + return dp[n]; + } +} \ No newline at end of file