public String replaceSpace(String s){ StringBuilder res = new StringBuilder(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == ' ') { res.append("%20"); } else { res.append(c); } } return res.toString(); }
private LinkedList<Integer> inQueue; private LinkedList<Integer> outQueue; privateint size; publicMyStack(){ inQueue = new LinkedList<>(); outQueue = new LinkedList<>(); size = 0; } // 直接向队列1添加就行 publicvoidpush(int x){ inQueue.add(x); size++; } publicintpop(){ if (size == 0) { return -1; } for (int i = 0; i < size - 1; i++) { outQueue.add(inQueue.remove()); } int res = inQueue.remove(); size--; for (int i = 0; i < size; i++) { inQueue.add(outQueue.remove()); } return res; } publicinttop(){ int res = pop(); inQueue.add(res); size++; return res; } publicbooleanempty(){ return size == 0; } }
递归和循环
9 斐波那契数列
解一
1 2 3 4 5 6 7 8 9 10 11 12 13
publicintfib(int n){ int a = 0; int b = 1; if (n == 0 || n == 1) { return n; } for (int i = 2; i <= n; i++) { int tmp = b % 1000000007; b = (a % 1000000007 + b % 1000000007) % 1000000007; a = tmp; } return b; }
10 青蛙跳台阶问题
解一
1 2 3 4 5 6 7 8 9 10 11 12
publicintnumWays(int n){ int a = 1, b = 2; if (n == 0 || n == 1) { return1; } for (int i = 2; i < n; i++) { int tmp = b % 1000000007; b = (a % 1000000007 + b % 1000000007) % 1000000007; a = tmp; } return b; }
查找和排序
11 旋转数组的最小数字
解一
直接遍历
1 2 3 4 5 6 7 8 9
publicintminArray(int[] numbers){ int minNumber = numbers[0]; for (int i = 1; i < numbers.length; i++) { if (numbers[i] < minNumber) { return numbers[i]; } } return minNumber; }
publicintminArray(int[] numbers){ int len = numbers.length; if (len == 1) { return numbers[0]; } int l = 0; int r = len - 1; // 第一次二分找到转折点 while (l < r) { int mid = (l + r) / 2; if (numbers[mid] > numbers[r]) { l = mid + 1; } elseif (numbers[mid] == numbers[r]) { r = r - 1; } else { r = mid; } } return numbers[l]; }
publicbooleanexist(char[][] board, String word){ char[] wordArray = word.toCharArray(); for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { if (dfs(board, wordArray, i, j, 0)) { returntrue; } } } returnfalse; }
publicbooleandfs(char[][] board, char[] word, int i, int j, int k){ // 剪枝 if (i >= board.length || i < 0 || j >= board[0].length || j < 0 || board[i][j] != word[k]) { returnfalse; } if (k == word.length - 1) { returntrue; }
// 往四个方向继续走 // mask当前值 board[i][j] = '\0'; boolean res = dfs(board, word, i - 1, j, k + 1) || dfs(board, word, i + 1, j, k + 1) || dfs(board, word, i, j - 1, k + 1) || dfs(board, word, i, j + 1, k + 1); board[i][j] = word[k]; return res; }
classSolution{ int res = 0; publicintmovingCount(int m, int n, int k){ boolean[][] visited = newboolean[m][n]; dfs(m, n, 0, 0, k, visited); return res; }
privatevoiddfs(int m, int n, int i, int j, int k, boolean[][] visited){ // 剪枝 if (i < 0 || j < 0 || i >= m || j >= n || visited[i][j]) { return; } if (getSum(i) + getSum(j) > k) { return; } visited[i][j] = true; res++; dfs(m, n, i - 1, j, k, visited); dfs(m, n, i + 1, j, k, visited); dfs(m, n, i, j - 1, k, visited); dfs(m, n, i, j + 1, k, visited); return; }
privateintgetSum(int i){ int count = 0; while (i != 0) { count += (i % 10); i /= 10; } return count; } }
publicdoublemyPow(double x, int n){ if (x == 0) { return0; } if (n == 0) { return1; } long b = n; if (b < 0) { x = 1.0 / x; b = -b; } double res = 1.0; while (b > 0) { if ((b & 1) == 1) { res *= x; } x *=x; b >>= 1; } return res; }
17 打印从1到最大的n位数
解一
注意的两个点:
大数越界问题,可以使用String来解决
1 2 3 4 5 6 7 8
publicint[] printNumbers(int n) { int end = (int)Math.pow(10, n) - 1; int[] res = newint[end]; for (int i = 0; i < end; i++) { res[i] = i + 1; } return res; }
public ListNode mergeKLists(ListNode[] lists){ ListNode cur = new ListNode(0); ListNode res = cur; int n = lists.length; PriorityQueue<ListNode> pq = new PriorityQueue<>((n1, n2) -> n1.val - n2.val); for (ListNode node : lists) { if (node != null) { pq.offer(node); } } while (!pq.isEmpty()) { ListNode curNode = pq.poll(); res.next = curNode; res = res.next; if (curNode.next != null) { pq.offer(curNode.next); } } return cur.next; }
26 树的子结构
解一
先判断当前的节点A和B是否相同,然后比较A.l和B.l 、A.r和B.r
【注意】判断double类型的变量是否相等时,需要判断两数之差是否在一个范围内,不能直接使用==
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
publicbooleanisSubStructure(TreeNode A, TreeNode B){ if (B == null || A == null) { returnfalse; } return hasSubStructure(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B); }
privatebooleanhasSubStructure(TreeNode A, TreeNode B){ if (B == null) { returntrue; } if (A == null || A.val != B.val) { returnfalse; } return hasSubStructure(A.left, B.left) && hasSubStructure(A.right, B.right); }
publicint[] spiralOrder(int[][] matrix) { int m = matrix.length; if (m == 0) { returnnewint[0]; } int n = matrix[0].length; int l = 0, r = n - 1, t = 0, b = m - 1, x = 0; int[] res = newint[m * n]; while (true) { for (int i = l; i <= r; i++) { res[x++] = matrix[t][i]; } if (++t > b) { break; } for (int i = t; i <= b; i++) { res[x++] = matrix[i][r]; } if (l > --r) { break; } for (int i = r; i >= l; i--) { res[x++] = matrix[b][i]; } if (t > --b) { break; } for (int i = b; i >= t; i--) { res[x++] = matrix[i][l]; } if (++l > r) { break; } } return res; }
privatebooleanhelper(int[] postorder, int start, int end){ if (start > end) { returntrue; } int root = postorder[end]; int maxIndex = start; int i = start; while (postorder[i] < root) { i++; } int j = i; while (postorder[i] > root) { i++; } boolean left = helper(postorder, start, j - 1); boolean right = helper(postorder, j, end - 1); return i == end && left && right; } }
classSolution{ public List<List<Integer>> pathSum(TreeNode root, int target) { List<List<Integer>> res = new ArrayList<>(); LinkedList<Integer> list = new LinkedList<>();
path(root, target, res, list); return res; }
privatevoidpath(TreeNode root, int target, List<List<Integer>> res, LinkedList<Integer> list){ if (root == null) { return; } int val = root.val; list.add(val); if (target - val == 0 && root.left == null && root.right == null) { res.add(new ArrayList(list)); } path(root.left, target - val, res, list); path(root.right, target - val, res, list); list.removeLast(); } }
public Node copyRandomList(Node head){ if (head == null) { return head; } Node cur = head; Node res = new Node(0); Node p = res; Map<Node, Node> map = new HashMap<>(); while (cur != null) { Node newNode = new Node(cur.val); p.next = newNode; map.put(cur, newNode); p = p.next; cur = cur.next; } cur = head; p = res.next; while (cur != null) { p.random = map.get(cur.random); cur = cur.next; p = p.next; } return res.next; }