1 - 剑指 Offer 09. 用两个栈实现队列
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class CQueue { 2 Stack<Integer> a, b; 3 // a : insert 4 // b : delete 5 public CQueue() { 6 a = new Stack<>(); 7 b = new Stack<>(); 8 } 9 public void appendTail(int value) { 10 a.push(value); 11 } 12 public int deleteHead() { 13 if (b.empty()) 14 { 15 while (!a.empty()) 16 b.push(a.pop()); 17 } 18 return (b.empty()) ? -1 : b.pop(); 19 } 20 }
2 - 剑指 Offer 30. 包含min函数的栈
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class MinStack { 2 /** initialize your data structure here. */ 3 Stack<Integer> a, b; 4 public MinStack() { 5 a = new Stack<>(); 6 b = new Stack<>(); 7 } 8 public void push(int x) { 9 a.push(x); 10 if (b.empty() || x <= b.peek()) 11 b.push(x); 12 } 13 public void pop() { 14 if (a.pop().equals(b.peek())) 15 b.pop(); 16 } 17 public int top() { 18 return a.peek(); 19 } 20 public int min() { 21 return b.peek(); 22 } 23 }
3 - 剑指 Offer 06. 从尾到头打印链表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public int[] reversePrint(ListNode head) { 3 Stack<Integer> a = new Stack<>(); 4 int n = 0; 5 for (; head != null; head = head.next) 6 { 7 a.push(head.val); 8 n ++; 9 } 10 int[] b = new int[n]; 11 int m = 0; 12 for (; !a.empty(); ) 13 { 14 b[m ++] = a.pop(); 15 } 16 return b; 17 } 18 }
4 - 剑指 Offer 24. 反转链表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public ListNode reverseList(ListNode head) { 3 ListNode prev, cur, next; 4 for (prev = null, cur = head; cur != null; prev = cur, cur = next) 5 { 6 next = cur.next; 7 cur.next = prev; 8 } 9 return prev; 10 } 11 }
5 - 剑指 Offer 35. 复杂链表的复制
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public Node copyRandomList(Node head) { 3 HashMap<Node, Node> a = new HashMap<>(); 4 Node cur, b; 5 for (cur = head; cur != null; cur = cur.next) 6 { 7 b = new Node(cur.val); 8 a.put(cur, b); 9 } 10 for (cur = head; cur != null; cur = cur.next) 11 { 12 Node c = a.get(cur); 13 c.next = a.get(cur.next); 14 c.random = a.get(cur.random); 15 } 16 return a.get(head); 17 } 18 }
6 - 剑指 Offer 05. 替换空格
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public String replaceSpace(String s) { 3 return s.replace(" ", "%20"); 4 } 5 }
7 - 剑指 Offer 58 - II. 左旋转字符串
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public String reverseLeftWords(String s, int n) { 3 int m = s.length(); 4 StringBuilder a = new StringBuilder(m); 5 int i, j = 0; 6 for (i = n; j < m; j ++) 7 { 8 a.append(s.charAt(i)); 9 i = (i + 1) % m; 10 } 11 return a.toString(); 12 } 13 }
8 - 剑指 Offer 03. 数组中重复的数字
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public int findRepeatNumber(int[] nums) { 3 int n = nums.length; 4 boolean[] a = new boolean[n]; 5 for (int i = 0; i < n; i ++) 6 { 7 if (a[nums[i]] == false) 8 a[nums[i]] = true; 9 else 10 return nums[i]; 11 } 12 return -1; 13 } 14 }
9 - 剑指 Offer 53 - I. 在排序数组中查找数字 I
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public int bSearch(int[] nums, int target) 3 { 4 int l = 0, n = nums.length; 5 int r = n - 1, ans = -1; 6 for (; l <= r; ) 7 { 8 int mid = (l + r) / 2; 9 if (target <= nums[mid]) 10 { 11 ans = mid; 12 r = mid - 1; 13 } 14 else 15 l = mid + 1; 16 } 17 return ans; 18 } 19 public int search(int[] nums, int target) { 20 return ((bSearch(nums, target + 1) == -1) ? nums.length : bSearch(nums, target + 1)) 21 - ((bSearch(nums, target) == -1) ? nums.length : bSearch(nums, target)); 22 } 23 }
10 - 剑指 Offer 53 - II. 0~n-1中缺失的数字
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public int missingNumber(int[] nums) { 3 int l = 0, r = nums.length - 1, ans = -1; 4 for (; l <= r; ) 5 { 6 int mid = (l + r) / 2; 7 if (nums[mid] == mid) l = mid + 1; 8 else 9 { 10 ans = mid; 11 r = mid - 1; 12 } 13 } 14 return (ans == -1) ? nums.length : ans; 15 } 16 }
11 - 剑指 Offer 04. 二维数组中的查找
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public boolean find(int[][] matrix, int l, int r, int u, int d, int target) 3 { 4 // System.out.println("" + l + "," + r + "," + u + "," + d); 5 if (l < 0 || u < 0 || r >= matrix[0].length || d >= matrix.length || r < l || d < u) 6 return false; 7 if ((r - l + 1) * (d - u + 1) <= 4) 8 { 9 for (int i = l; i <= r; i ++) 10 for (int j = u; j <= d; j ++) 11 if (matrix[j][i] == target) 12 return true; 13 return false; 14 } 15 int mid_lr = (l + r) / 2; 16 int mid_ud = (u + d) / 2; 17 if (matrix[mid_ud][mid_lr] == target) 18 return true; 19 else if (matrix[mid_ud][mid_lr] < target) 20 return find(matrix, mid_lr, r, mid_ud, d, target) || 21 find(matrix, l, mid_lr, mid_ud + 1, d, target) || 22 find(matrix, mid_lr, r, u, mid_ud - 1, target); 23 else 24 return find(matrix, l, mid_lr, u, mid_ud, target) || 25 find(matrix, mid_lr, r, u, mid_ud - 1, target) || 26 find(matrix, l, mid_lr, mid_ud + 1, d, target); 27 } 28 public boolean findNumberIn2DArray(int[][] matrix, int target) { 29 if (matrix.length == 0) 30 return false; 31 int l = 0, u = 0, r = matrix[0].length - 1, d = matrix.length - 1; 32 return find(matrix, l, r, u, d, target); 33 } 34 }
12 - 剑指 Offer 11. 旋转数组的最小数字
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public int minArray(int[] numbers) { 3 int n = numbers.length; 4 int l = 0, r = n - 1, ans = numbers[0]; 5 for (; l <= r; ) 6 { 7 int mid = (l + r) / 2; 8 ans = Math.min(ans, numbers[mid]); 9 if (numbers[mid] < numbers[r]) 10 r = mid - 1; 11 else if (numbers[mid] > numbers[r]) 12 l = mid + 1; 13 else 14 r --; 15 } 16 return ans; 17 } 18 }
13 - 剑指 Offer 50. 第一个只出现一次的字符
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public char firstUniqChar(String s) { 3 int n = s.length(); 4 int[] cnt = new int[26]; 5 for (int i = 0; i < n; i ++) 6 cnt[s.charAt(i) - 'a'] ++; 7 for (int i = 0; i < n; i ++) 8 if (cnt[s.charAt(i) - 'a'] == 1) 9 return s.charAt(i); 10 return ' '; 11 } 12 }
14 - 面试题32 - I. 从上到下打印二叉树
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public int[] levelOrder(TreeNode root) { 3 if (root == null) 4 return new int[0]; 5 Deque<TreeNode> a = new ArrayDeque<>(); 6 ArrayList<Integer> b = new ArrayList<>(); 7 a.addLast(root); 8 for (; !a.isEmpty(); ) 9 { 10 TreeNode x = a.removeFirst(); 11 b.add(x.val); 12 if (x.left != null) a.addLast(x.left); 13 if (x.right != null) a.addLast(x.right); 14 } 15 int[] c = new int[b.size()]; 16 for (int i = 0; i < b.size(); i ++) 17 { 18 c[i] = b.get(i); 19 } 20 return c; 21 } 22 }
15 - 剑指 Offer 32 - II. 从上到下打印二叉树 II
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public List<List<Integer>> levelOrder(TreeNode root) { 3 List<List<Integer>> a = new ArrayList<>(); 4 List<TreeNode> b = new ArrayList<>(); 5 if (root == null) 6 return a; 7 b.add(root); 8 for (; !b.isEmpty(); ) 9 { 10 List<TreeNode> c = new ArrayList<>(); 11 List<Integer> d = new ArrayList<>(); 12 for (int i = 0; i < b.size(); i ++) 13 { 14 TreeNode x = b.get(i); 15 d.add(x.val); 16 if (x.left != null) 17 c.add(x.left); 18 if (x.right != null) 19 c.add(x.right); 20 } 21 a.add(d); 22 b = c; 23 } 24 return a; 25 } 26 }
16 - 剑指 Offer 32 - III. 从上到下打印二叉树 III
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public List<List<Integer>> levelOrder(TreeNode root) { 3 List<List<Integer>> a = new ArrayList<>(); 4 List<TreeNode> b = new ArrayList<>(); 5 int level = 0; 6 if (root == null) 7 return a; 8 b.add(root); 9 for (; !b.isEmpty(); level ++) 10 { 11 List<TreeNode> c = new ArrayList<>(); 12 List<Integer> d = new ArrayList<>(); 13 for (int i = 0; i < b.size(); i ++) 14 { 15 TreeNode x = b.get(i); 16 if (x.left != null) 17 c.add(x.left); 18 if (x.right != null) 19 c.add(x.right); 20 } 21 if (level % 2 == 1) 22 for (int i = b.size() - 1; i >= 0; i --) 23 d.add(b.get(i).val); 24 else 25 for (int i = 0; i < b.size(); i ++) 26 d.add(b.get(i).val); 27 a.add(d); 28 b = c; 29 } 30 return a; 31 } 32 }
17 - 剑指 Offer 26. 树的子结构
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public boolean S(TreeNode A, TreeNode B) 3 { 4 if (A == null && B != null) 5 return false; 6 if (B != null) 7 return (A.val == B.val) && S(A.left, B.left) && S(A.right, B.right); 8 else 9 return true; 10 } 11 public boolean isSubStructure(TreeNode A, TreeNode B) { 12 return (A != null) && (B != null) && ((S(A, B) == true) || isSubStructure(A.left, B) 13 || isSubStructure(A.right, B)); 14 } 15 }
18 - 剑指 Offer 27. 二叉树的镜像
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public TreeNode mirrorTree(TreeNode root) { 3 if (root == null) 4 return root; 5 TreeNode t = root.left; 6 root.left = root.right; 7 root.right = t; 8 mirrorTree(root.left); 9 mirrorTree(root.right); 10 return root; 11 } 12 }
19 - 剑指 Offer 28. 对称的二叉树
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public boolean S(TreeNode A, TreeNode B) 3 { 4 if (A == null && B != null || A != null && B == null) 5 return false; 6 if (A == null && B == null) 7 return true; 8 return (A.val == B.val) && S(A.left, B.right) && S(A.right, B.left); 9 } 10 public boolean isSymmetric(TreeNode root) { 11 if (root == null) 12 return true; 13 return S(root.left, root.right); 14 } 15 }
20 - 剑指 Offer 10- I. 斐波那契数列
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public int fib(int n) { 3 int[] F = new int[101]; 4 int mod = 1000000007; 5 F[0] = 0; F[1] = 1; 6 for (int i = 2; i <= n; i ++) F[i] = (F[i - 1] + F[i - 2]) % mod; 7 return F[n]; 8 } 9 }
21 - 剑指 Offer 10- II. 青蛙跳台阶问题
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public int numWays(int n) { 3 int[] F = new int[101]; 4 int mod = 1000000007; 5 F[0] = 1; F[1] = 1; 6 for (int i = 2; i <= n; i ++) F[i] = (F[i - 1] + F[i - 2]) % mod; 7 return F[n]; 8 } 9 }
22 - 剑指 Offer 63. 股票的最大利润
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public int maxProfit(int[] prices) { 3 int n = prices.length; 4 if (n == 0) return 0; 5 int[] F = new int[n + 1]; 6 F[n] = 0; 7 int Ans = 0; 8 for (int i = n - 1; i >= 0; i --) 9 F[i] = Math.max(F[i + 1], prices[i]); 10 for (int i = 0; i < n; i ++) 11 Ans = Math.max(Ans, F[i + 1] - prices[i]); 12 return Ans; 13 } 14 }
23 - 剑指 Offer 42. 连续子数组的最大和
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public int maxSubArray(int[] nums) { 3 int n = nums.length; 4 int[] Dp1 = new int[n]; 5 int[] Dp2 = new int[n]; 6 Dp1[0] = Math.max(0, nums[0]); 7 Dp2[0] = nums[0]; 8 for (int i = 1; i < n; i ++) 9 { 10 Dp1[i] = Math.max(nums[i], Dp1[i - 1] + nums[i]); 11 Dp2[i] = Math.max(Dp2[i - 1], Dp1[i]); 12 } 13 return Dp2[n - 1]; 14 } 15 }
24 - 剑指 Offer 47. 礼物的最大价值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public int maxValue(int[][] grid) { 3 int n = grid.length; 4 int m = grid[0].length; 5 int[][] F = new int[n][m]; 6 F[0][0] = grid[0][0]; 7 for (int i = 0; i < n; i ++) 8 for (int j = 0; j < m; j ++) 9 { 10 if (i != 0) F[i][j] = Math.max(F[i][j], F[i - 1][j] + grid[i][j]); 11 if (j != 0) F[i][j] = Math.max(F[i][j], F[i][j - 1] + grid[i][j]); 12 } 13 return F[n - 1][m - 1]; 14 } 15 }
25 - 剑指 Offer 46. 把数字翻译成字符串
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public int translateNum(int num) { 3 String s = String.valueOf(num); 4 int n = s.length(); 5 int[] F = new int[n + 1]; 6 F[0] = 1; 7 for (int i = 1; i <= n; i ++) 8 { 9 F[i] = F[i - 1]; 10 if (i >= 2 && Integer.valueOf(s.substring(i - 2, i)) >= 10 11 && Integer.valueOf(s.substring(i - 2, i)) <= 25) 12 F[i] += F[i - 2]; 13 } 14 return F[n]; 15 } 16 }
26 - 剑指 Offer 48. 最长不含重复字符的子字符串
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public int lengthOfLongestSubstring(String s) { 3 int n = s.length(); 4 int ans = 0; 5 int[] cnt = new int[256]; 6 int j = 0; 7 for (int i = 0; i < n; i ++) 8 { 9 cnt[s.charAt(i)] ++; 10 if (cnt[s.charAt(i)] == 2) 11 { 12 for (; j < i && s.charAt(i) != s.charAt(j); j ++) 13 cnt[s.charAt(j)] --; 14 cnt[s.charAt(j ++)] --; 15 } 16 ans = Math.max(ans, i - j + 1); 17 // System.out.println(i + " " + j); 18 } 19 return ans; 20 } 21 }
27 - 剑指 Offer 18. 删除链表的节点
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public ListNode deleteNode(ListNode head, int val) { 3 ListNode i = head, j = new ListNode(); 4 for (; i != null; j = i, i = i.next) 5 if (i.val == val) 6 break; 7 if (i == head) 8 return head.next; 9 else 10 { 11 j.next = i.next; 12 return head; 13 } 14 } 15 }
28 - 剑指 Offer 22. 链表中倒数第k个节点
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public ListNode getKthFromEnd(ListNode head, int k) { 3 ListNode i = head, j = head; 4 for (int s = 0; s < k; s ++, j = j.next); 5 for (; i != null && j != null; i = i.next, j = j.next); 6 return i; 7 } 8 }
29 - 剑指 Offer 25. 合并两个排序的链表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Definition for singly-linked list. 3 * function ListNode(val) { 4 * this.val = val; 5 * this.next = null; 6 * } 7 */ 8 /** 9 * @param {ListNode} l1 10 * @param {ListNode} l2 11 * @return {ListNode} 12 */ 13 var mergeTwoLists = function(l1, l2) { 14 var cur = new ListNode(-1); 15 let head = cur; 16 for (; l1 != null && l2 != null; ) 17 { 18 if (l1.val < l2.val) 19 cur.next = l1, l1 = l1.next; 20 else 21 cur.next = l2, l2 = l2.next; 22 cur = cur.next; 23 } 24 for (; l1 != null; l1 = l1.next) 25 cur.next = l1, cur = cur.next; 26 for (; l2 != null; l2 = l2.next) 27 cur.next = l2, cur = cur.next; 28 return head.next; 29 };
30 - 剑指 Offer 52. 两个链表的第一个公共节点
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Definition for singly-linked list. 3 * function ListNode(val) { 4 * this.val = val; 5 * this.next = null; 6 * } 7 */ 8 9 /** 10 * @param {ListNode} headA 11 * @param {ListNode} headB 12 * @return {ListNode} 13 */ 14 var getIntersectionNode = function(headA, headB) { 15 var S = new Set(); 16 for (; headA != null; headA = headA.next) 17 S.add(headA); 18 for (; headB != null; headB = headB.next) 19 if (S.has(headB)) 20 return headB; 21 return null; 22 };
31 - 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number[]} nums 3 * @return {number[]} 4 */ 5 var exchange = function(nums) { 6 var a = new Array(); 7 var l = 0; 8 var r = nums.length - 1; 9 for (let i = 0; i < nums.length; i ++) 10 { 11 if (nums[i] % 2 == 1) 12 a[l ++] = nums[i]; 13 else 14 a[r --] = nums[i]; 15 } 16 return a; 17 };
32 - 剑指 Offer 57. 和为s的两个数字
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number[]} nums 3 * @param {number} target 4 * @return {number[]} 5 */ 6 var twoSum = function(nums, target) { 7 var i = 0; 8 var j = nums.length - 1; 9 for (; i < j; ) 10 { 11 if (nums[i] + nums[j] == target) 12 return new Array(nums[i], nums[j]); 13 else if (nums[i] + nums[j] < target) 14 i ++; 15 else 16 j --; 17 } 18 };
33 - 剑指 Offer 58 - I. 翻转单词顺序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {string} s 3 * @return {string} 4 */ 5 var reverseWords = function(s) { 6 var a = s.split(" ").reverse(); 7 var S = new String(); 8 for (let i = 0; i < a.length; i ++) 9 { 10 if (a[i].length == 0) continue; 11 S += " " + a[i]; 12 } 13 return S.substring(1); 14 };
34 - 剑指 Offer 12. 矩阵中的路径
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {character[][]} board 3 * @param {string} word 4 * @return {boolean} 5 */ 6 var exist = function(board, word) { 7 var n = board.length; 8 var m = board[0].length; 9 var vis = new Array(); 10 for (let i = 0; i < n; i ++) 11 vis[i] = new Array(); 12 function dfs(x, y, cur, fin) 13 { 14 if (vis[x][y] == true) 15 return false; 16 if (board[x][y] != word[cur]) 17 return false; 18 if (cur == fin - 1) 19 return true; 20 vis[x][y] = true; 21 var ans = false; 22 if (y + 1 < m) ans |= dfs(x, y + 1, cur + 1, fin); 23 if (!ans && x + 1 < n) ans |= dfs(x + 1, y, cur + 1, fin); 24 if (!ans && x >= 1) ans |= dfs(x - 1, y, cur + 1, fin); 25 if (!ans && y >= 1) ans |= dfs(x, y - 1, cur + 1, fin); 26 vis[x][y] = false; 27 return ans; 28 } 29 for (let i = 0; i < n; i ++) 30 for (let j = 0; j < m; j ++) 31 if (dfs(i, j, 0, word.length)) 32 return true; 33 return false; 34 }
35 - 剑指 Offer 13. 机器人的运动范围
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number} m 3 * @param {number} n 4 * @param {number} k 5 * @return {number} 6 */ 7 var movingCount = function(m, n, k) { 8 var dp = new Array(m); 9 for (let i = 0; i < m; i ++) 10 dp[i] = new Array(n); 11 function cal(x) 12 { 13 let t = 0; 14 for (; x; x = Math.floor(x / 10)) 15 t += x % 10; 16 return t; 17 } 18 dp[0][0] = true; 19 var ans = 0; 20 for (let i = 0; i < m; i ++) 21 for (let j = 0; j < n; j ++) 22 { 23 dp[i][j] = (cal(i) + cal(j) <= k) && ((i > 0 && dp[i - 1][j]) || (j > 0 && dp[i][j - 1]) || (i == 0 && j == 0)); 24 // console.log(i, j, dp[i][j], cal(i), cal(j), k); 25 ans += (dp[i][j] == true) ? 1 : 0; 26 } 27 return ans; 28 };
36 - 剑指 Offer 34. 二叉树中和为某一值的路径
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Definition for a binary tree node. 3 * function TreeNode(val, left, right) { 4 * this.val = (val===undefined ? 0 : val) 5 * this.left = (left===undefined ? null : left) 6 * this.right = (right===undefined ? null : right) 7 * } 8 */ 9 /** 10 * @param {TreeNode} root 11 * @param {number} target 12 * @return {number[][]} 13 */ 14 var pathSum = function(root, target) { 15 var Ans = new Array(); 16 var cur = new Array(); 17 function dfs(x, y) 18 { 19 if (x == null) 20 return; 21 if (x.left == null && x.right == null) 22 { 23 if (y + x.val == target) 24 { 25 cur.push(x.val); 26 Ans.push(cur.slice()); 27 cur.pop(); 28 } 29 return; 30 } 31 cur.push(x.val); 32 dfs(x.left, y + x.val); 33 dfs(x.right, y + x.val); 34 cur.pop(); 35 } 36 dfs(root, 0); 37 return Ans; 38 };
37 - 剑指 Offer 36. 二叉搜索树与双向链表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * // Definition for a Node. 3 * function Node(val,left,right) { 4 * this.val = val; 5 * this.left = left; 6 * this.right = right; 7 * }; 8 */ 9 /** 10 * @param {Node} root 11 * @return {Node} 12 */ 13 var treeToDoublyList = function(root) { 14 var s = null; 15 var head = null; 16 if (root == null) 17 return null; 18 function dfs(x) 19 { 20 if (x == null) 21 return; 22 dfs(x.left); 23 if (head == null) 24 { 25 head = x; 26 s = x; 27 } 28 else 29 { 30 x.left = s; 31 s.right = x; 32 s = x; 33 } 34 dfs(x.right); 35 } 36 dfs(root); 37 // console.log(s, head == null); 38 s.right = head; 39 head.left = s; 40 return head; 41 };
38 - 剑指 Offer 54. 二叉搜索树的第k大节点
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Definition for a binary tree node. 3 * function TreeNode(val) { 4 * this.val = val; 5 * this.left = this.right = null; 6 * } 7 */ 8 /** 9 * @param {TreeNode} root 10 * @param {number} k 11 * @return {number} 12 */ 13 var kthLargest = function(root, k) { 14 var Ans = null; 15 function dfs(x) 16 { 17 if (x == null) 18 return; 19 dfs(x.right); 20 k --; 21 if (k == 0) 22 Ans = x.val; 23 dfs(x.left); 24 } 25 dfs(root); 26 return Ans; 27 };
39 - 剑指 Offer 45. 把数组排成最小的数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number[]} nums 3 * @return {string} 4 */ 5 var minNumber = function(nums) { 6 var a = new Array(); 7 for (let i = 0; i < nums.length; i ++) 8 a.push(nums[i].toString()); 9 a.sort(function(x, y){ 10 return x + y < y + x ? -1 : 1; 11 }); 12 // console.log(a); 13 return a.join(""); 14 };
40 - 剑指 Offer 61. 扑克牌中的顺子
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number[]} nums 3 * @return {boolean} 4 */ 5 var isStraight = function(nums) { 6 // var x = (nums.map((x)=>(x > 0 ? x : 14))); 7 // console.log(x); 8 var a = new Array(14).fill(0); 9 for (let i = 0; i < nums.length; i ++) 10 a[nums[i]] ++; 11 // var L = Math.min(...((nums.map((x)=>(x > 0 ? x : 14))))); 12 // console.log(...nums, L); 13 // var R = Math.max(...nums); 14 // console.log(L, R); 15 for (let i = 1; i <= 9; i ++) 16 { 17 let Flag = true; 18 let b = a.slice(); 19 for (let j = i; j <= i + 4; j ++) 20 { 21 if (b[j] >= 1) 22 b[j] --; 23 else 24 b[0] --; 25 if (b[0] < 0) 26 { 27 Flag = false; 28 break; 29 } 30 } 31 if (Flag) return true; 32 } 33 return false; 34 };
41 - 剑指 Offer 40. 最小的k个数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number[]} arr 3 * @param {number} k 4 * @return {number[]} 5 */ 6 var getLeastNumbers = function(arr, k) { 7 var a = new Array(10001).fill(0); 8 var ans = new Array(); 9 for (let i = 0; i < arr.length; i ++) 10 a[arr[i]] ++; 11 for (let i = 0; i <= 10000; i ++) 12 for (let j = 0; j < a[i] && k > 0; j ++) 13 { 14 ans.push(i); 15 k --; 16 } 17 return ans; 18 };
42 - 剑指 Offer 41. 数据流中的中位数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * initialize your data structure here. 3 */ 4 5 class TreeNode { 6 constructor(x) 7 { 8 this.val = x; 9 this.left = null; 10 this.right = null; 11 this.size = 1; 12 } 13 }; 14 15 class Tree { 16 ins(x, y) 17 { 18 if (x == null) 19 return new TreeNode(y); 20 x.size ++; 21 if (y <= x.val) 22 x.left = this.ins(x.left, y); 23 else 24 x.right = this.ins(x.right, y); 25 // console.log("ins", x); 26 if (Math.max(((x.left == null) ? 0 : x.left.size), ((x.right == null) ? 0 : x.right.size)) > 0.7 * x.size) 27 return this.rebuild(x); 28 else 29 return x; 30 } 31 update(x) 32 { 33 x.size = ((x.left == null) ? 0 : x.left.size) + ((x.right == null) ? 0 : x.right.size) + 1; 34 } 35 build(l, r) 36 { 37 if (l > r) return; 38 var mid = Math.floor((l + r) / 2); 39 var x = new TreeNode(); 40 x.val = this.a[mid]; 41 x.left = this.build(l, mid - 1); 42 x.right = this.build(mid + 1, r); 43 this.update(x); 44 return x; 45 } 46 dfs(x) 47 { 48 if (x == null) 49 return; 50 this.dfs(x.left); 51 this.a.push(x.val); 52 this.dfs(x.right); 53 } 54 rebuild(x) 55 { 56 this.a = new Array(); 57 this.dfs(x); 58 return this.build(0, this.a.length - 1); 59 } 60 find(x, k) 61 { 62 // console.log("find", x, k); 63 if (((x.left == null) ? 0 : x.left.size) + 1 == k) 64 return x.val; 65 else if (k <= ((x.left == null) ? 0 : x.left.size)) 66 return this.find(x.left, k); 67 else 68 return this.find(x.right, k - (((x.left == null) ? 0 : x.left.size) + 1)); 69 } 70 }; 71 72 var root = null; 73 var T = new Tree(); 74 75 var MedianFinder = function() { 76 root = null; 77 T = new Tree(); 78 }; 79 80 /** 81 * @param {number} num 82 * @return {void} 83 */ 84 MedianFinder.prototype.addNum = function(num) { 85 root = T.ins(root, num); 86 }; 87 88 /** 89 * @return {number} 90 */ 91 MedianFinder.prototype.findMedian = function() { 92 var n = (root == null) ? 0 : root.size; 93 // console.log("findMedian", n); 94 if (n == 0) 95 return null; 96 if (n % 2 == 1) 97 return T.find(root, Math.floor(n / 2) + 1); 98 else 99 return (T.find(root, n / 2) + T.find(root, n / 2 + 1)) / 2; 100 }; 101 102 /** 103 * Your MedianFinder object will be instantiated and called as such: 104 * var obj = new MedianFinder() 105 * obj.addNum(num) 106 * var param_2 = obj.findMedian() 107 */
43 - 剑指 Offer 55 - I. 二叉树的深度
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Definition for a binary tree node. 3 * function TreeNode(val) { 4 * this.val = val; 5 * this.left = this.right = null; 6 * } 7 */ 8 /** 9 * @param {TreeNode} root 10 * @return {number} 11 */ 12 var maxDepth = function(root) { 13 var ans = 0; 14 function dfs(x, dep) 15 { 16 if (x == null) 17 { 18 ans = Math.max(ans, dep); 19 return; 20 } 21 dfs(x.left, dep + 1); 22 dfs(x.right, dep + 1); 23 } 24 dfs(root, 0); 25 return ans; 26 };
44 - 剑指 Offer 55 - II. 平衡二叉树
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Definition for a binary tree node. 3 * function TreeNode(val) { 4 * this.val = val; 5 * this.left = this.right = null; 6 * } 7 */ 8 /** 9 * @param {TreeNode} root 10 * @return {boolean} 11 */ 12 var isBalanced = function(root) { 13 var ans = true; 14 function dfs(x) 15 { 16 if (ans == false) 17 return; 18 if (x == null) 19 return 0; 20 let left = dfs(x.left); 21 let right = dfs(x.right); 22 if (Math.abs(left - right) > 1) ans = false; 23 return Math.max(left, right) + 1; 24 } 25 dfs(root); 26 return ans; 27 };
45 - 剑指 Offer 64. 求1+2+…+n
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number} n 3 * @return {number} 4 */ 5 var sumNums = function(n) { 6 function solve(n) { 7 n && (n += solve(n - 1)); 8 return n; 9 } 10 return solve(n); 11 };
46 - 剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Definition for a binary tree node. 3 * function TreeNode(val) { 4 * this.val = val; 5 * this.left = this.right = null; 6 * } 7 */ 8 /** 9 * @param {TreeNode} root 10 * @param {TreeNode} p 11 * @param {TreeNode} q 12 * @return {TreeNode} 13 */ 14 var lowestCommonAncestor = function(root, p, q) { 15 function solve(x, p, q) 16 { 17 if (x.val < p.val && x.val < q.val) 18 return solve(x.right, p, q); 19 else if (x.val > p.val && x.val > q.val) 20 return solve(x.left, p, q); 21 else 22 return x; 23 } 24 return solve(root, p, q); 25 };
47 - 剑指 Offer 68 - II. 二叉树的最近公共祖先
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Definition for a binary tree node. 3 * function TreeNode(val) { 4 * this.val = val; 5 * this.left = this.right = null; 6 * } 7 */ 8 /** 9 * @param {TreeNode} root 10 * @param {TreeNode} p 11 * @param {TreeNode} q 12 * @return {TreeNode} 13 */ 14 var lowestCommonAncestor = function(root, p, q) { 15 function solve(root, p, q) 16 { 17 if (root == null) 18 return null; 19 if (root == p || root == q) 20 return root; 21 var L = solve(root.left, p, q); 22 var R = solve(root.right, p, q); 23 if (L && R) 24 return root; 25 else return (L) ? L : R; 26 } 27 return solve(root, p, q); 28 };
48 - 剑指 Offer 07. 重建二叉树
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Definition for a binary tree node. 3 * function TreeNode(val) { 4 * this.val = val; 5 * this.left = this.right = null; 6 * } 7 */ 8 /** 9 * @param {number[]} preorder 10 * @param {number[]} inorder 11 * @return {TreeNode} 12 */ 13 var buildTree = function(preorder, inorder) { 14 function solve(preO, inO) 15 { 16 if (preO.length == 0) 17 return null; 18 var x = preO[0]; 19 var node = new TreeNode(x); 20 if (preO.length == 1) 21 return node; 22 for (var i = 0; i < inO.length; i ++) 23 if (inO[i] == x) 24 break; 25 node.left = solve(preO.slice(1, i + 1), inO.slice(0, i)); 26 node.right = solve(preO.slice(i + 1), inO.slice(i + 1)); 27 return node; 28 } 29 return solve(preorder, inorder); 30 };
49 - 剑指 Offer 16. 数值的整数次方
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number} x 3 * @param {number} n 4 * @return {number} 5 */ 6 var myPow = function(x, n) { 7 var flag = false; 8 if (n < 0) 9 { 10 flag = true; 11 n = -n; 12 } 13 var ans = 1; 14 for (; n; n = Math.floor(n / 2)) 15 { 16 // console.log(n); 17 if (n % 2 == 1) ans = ans * x; 18 x = x * x; 19 } 20 return (flag ? 1. / ans : ans); 21 };
50 - 剑指 Offer 33. 二叉搜索树的后序遍历序列
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number[]} postorder 3 * @return {boolean} 4 */ 5 var verifyPostorder = function(postorder) { 6 function solve(a) 7 { 8 if (a.length == 0) 9 return true; 10 var x = a[a.length - 1]; 11 for (var i = 0; i < a.length - 1; i ++) 12 if (a[i] > x) break; 13 var y = i; 14 for (; i < a.length - 1; i ++) 15 if (a[i] < x) return false; 16 return solve(a.slice(0, y)) && solve(a.slice(y, a.length - 1)); 17 } 18 return solve(postorder); 19 };
51 - 剑指 Offer 15. 二进制中1的个数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number} n - a positive integer 3 * @return {number} 4 */ 5 var hammingWeight = function(n) { 6 var ans = 0; 7 for (let i = 31; i >= 0; i --) 8 { 9 if (n & (1 << i)) 10 ans ++; 11 } 12 return ans; 13 };
52 - 剑指 Offer 65. 不用加减乘除做加法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number} a 3 * @param {number} b 4 * @return {number} 5 */ 6 var add = function(a, b) { 7 var c = 0; 8 var ans = 0; 9 for (let i = 0; i < 32; i ++) 10 { 11 let x = a & (1 << i) ? 1 : 0; 12 let y = b & (1 << i) ? 1 : 0; 13 ans |= ((x ^ y ^ c) << i); 14 c = (x & y) | (x & c) | (y & c); 15 } 16 return ans; 17 };
53 - 剑指 Offer 56 - I. 数组中数字出现的次数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number[]} nums 3 * @return {number[]} 4 */ 5 var singleNumbers = function(nums) { 6 var ret = 0; 7 for (var x of nums) 8 ret ^= x; 9 var i = 0; 10 for (; !(ret & 1); ret >>= 1) 11 i ++; 12 var res1 = 0; 13 var res2 = 0; 14 for (var x of nums) 15 if ((1 << i) & x) 16 res1 ^= x; 17 else 18 res2 ^= x; 19 return [res1, res2]; 20 };
54 - 剑指 Offer 56 - II. 数组中数字出现的次数 II
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number[]} nums 3 * @return {number} 4 */ 5 var singleNumber = function(nums) { 6 var a = new Array(31).fill(0); 7 for (var x of nums) 8 for (let i = 0; i < 31; i ++) 9 if (x & (1 << i)) 10 a[i] = (a[i] + 1) % 3; 11 var ans = 0; 12 for (let i = 0; i < 31; i ++) 13 if (a[i]) 14 ans |= (1 << i); 15 return ans; 16 };
55 - 剑指 Offer 39. 数组中出现次数超过一半的数字
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number[]} nums 3 * @return {number} 4 */ 5 var majorityElement = function(nums) { 6 var num = 0; 7 var numc = 0; 8 for (let x of nums) 9 { 10 if (numc == 0) 11 num = x; 12 if (x == num) 13 numc ++; 14 else 15 numc --; 16 } 17 return num; 18 };
56 - 剑指 Offer 66. 构建乘积数组
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number[]} a 3 * @return {number[]} 4 */ 5 var constructArr = function(a) { 6 var b = new Array(); 7 var L = new Array(); 8 var R = new Array(); 9 if (a.length == 0) 10 return []; 11 if (a.length == 1) 12 return [1]; 13 L[0] = a[0]; 14 for (let i = 1; i < a.length; i ++) 15 { 16 L[i] = L[i - 1] * a[i]; 17 } 18 R[a.length - 1] = a[a.length - 1]; 19 for (let i = a.length - 2; i >= 0; i --) 20 { 21 R[i] = R[i + 1] * a[i]; 22 } 23 b[0] = R[1]; 24 b[a.length - 1] = L[a.length - 2]; 25 for (let i = 1; i < a.length - 1; i ++) 26 { 27 b[i] = L[i - 1] * R[i + 1]; 28 } 29 return b; 30 };
57 - 剑指 Offer 14- I. 剪绳子
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number} n 3 * @return {number} 4 */ 5 var cuttingRope = function(n) { 6 // 2 * a + 3 * b 7 // 2 * (a - 3) + 3 * (b + 2) 8 // 3 * 3 / 2 / 2 / 2 > 1 9 var ans = 1; 10 if (n == 2) 11 return 1; 12 if (n == 3) 13 return 2; 14 for (; n >= 3; n -= 3) 15 ans *= 3; 16 if (n == 2) ans *= 2; 17 if (n == 1) ans = ans / 3 * 4; 18 return ans; 19 };
58 - 剑指 Offer 57 - II. 和为s的连续正数序列
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number} target 3 * @return {number[][]} 4 */ 5 var findContinuousSequence = function(target) { 6 // i L i * L + L(L - 1) / 2 7 var ans = []; 8 for (let i = 1; 2 * i + 1 <= target; i ++) 9 { 10 let cur = 0; 11 let a = new Array(); 12 for (let j = i; cur < target; j ++) 13 { 14 cur += j; 15 a.push(j); 16 } 17 if (cur == target) 18 ans.push(a); 19 } 20 return ans; 21 };
59 - 剑指 Offer 62. 圆圈中最后剩下的数字
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number} n 3 * @param {number} m 4 * @return {number} 5 */ 6 var lastRemaining = function(n, m) { 7 ans = 0; 8 for (let i = 2; i <= n; i ++) 9 ans = (ans + m) % i; 10 return ans; 11 };
60 - 剑指 Offer 29. 顺时针打印矩阵
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number[][]} matrix 3 * @return {number[]} 4 */ 5 var spiralOrder = function(matrix) { 6 let n = matrix.length; 7 if (n == 0) 8 return []; 9 let m = matrix[0].length; 10 if (m == 0) 11 return []; 12 let cnt = 1; 13 let dx = [0, 1, 0, -1]; 14 let dy = [1, 0, -1, 0]; 15 let A = []; 16 let flag = new Array(); 17 for (let i = 0; i < n; i ++) 18 flag[i] = new Array(); 19 let x = 0, y = -1; 20 let c = 0; 21 for (; cnt <= n * m; cnt ++) 22 { 23 nx = x + dx[c]; 24 ny = y + dy[c]; 25 while (nx < 0 || nx >= n || ny < 0 || ny >= m || flag[nx][ny] == true) 26 c = (c + 1) % 4, nx = x + dx[c], ny = y + dy[c]; 27 x = nx; 28 y = ny; 29 flag[x][y] = true; 30 A.push(matrix[x][y]); 31 } 32 return A; 33 };
61 - 剑指 Offer 31. 栈的压入、弹出序列
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number[]} pushed 3 * @param {number[]} popped 4 * @return {boolean} 5 */ 6 var validateStackSequences = function(pushed, popped) { 7 let A = []; 8 let top = 0, i = 0; 9 for (let x of pushed) 10 { 11 A[++ top] = x; 12 while (top > 0 && A[top] == popped[i]) 13 top --, i ++; 14 } 15 if (top == 0) 16 return true; 17 else 18 return false; 19 };
62 - 剑指 Offer 20. 表示数值的字符串
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {string} s 3 * @return {boolean} 4 */ 5 var isNumber = function(s) { 6 s = s.trim(); 7 function checkI(s) 8 { 9 if (s.charAt(0) == '+' || s.charAt(0) == '-') 10 s = s.slice(1); 11 if (s.length == 0) 12 return false; 13 for (let i = 0; i < s.length; i ++) 14 { 15 let c = s.charAt(i); 16 if (c < '0' || c > '9') return false; 17 } 18 return true; 19 } 20 function checkS(s) 21 { 22 if (s.charAt(0) == '+' || s.charAt(0) == '-') 23 s = s.slice(1); 24 let dot = s.indexOf('.'); 25 if (s.length <= 1) 26 return false; 27 if (dot == -1) 28 return false; 29 // console.log(s, dot); 30 for (let i = 0; i < dot; i ++) 31 { 32 let c = s.charAt(i); 33 if (c < '0' || c > '9') return false; 34 } 35 for (let i = dot + 1; i < s.length; i ++) 36 { 37 let c = s.charAt(i); 38 if (c < '0' || c > '9') return false; 39 } 40 return true; 41 } 42 let x = s.search("e|E"); 43 if (x == -1) 44 return checkI(s) || checkS(s); 45 else 46 return (checkI(s.slice(0, x)) || checkS(s.slice(0, x))) && 47 (checkI(s.slice(x + 1))); 48 };
63 - 剑指 Offer 67. 把字符串转换成整数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {string} str 3 * @return {number} 4 */ 5 var strToInt = function(str) { 6 str = str.trimLeft(); 7 if (str.length == 0 || str.trim().length == 0) 8 return 0; 9 let res = 0, F = 0; 10 if (str.charAt(0) == '+') ; 11 else if (str.charAt(0) == '-') F = 1; 12 else if (str.charAt(0) >= '0' && str.charAt(0) <= '9'); 13 else return 0; 14 // console.log(str); 15 if (str.charAt(0) == '+' || str.charAt(0) == '-') 16 str = str.slice(1); 17 for (let i = 0; i < str.length; i ++) 18 { 19 let c = str.charAt(i); 20 if (c < '0' || c > '9') 21 return res * (F == 1 ? -1 : 1); 22 res = res * 10 + (c - '0'); 23 res = Math.min(res, 2147483647 + F); 24 } 25 // console.log(res, F); 26 return res * (F == 1 ? -1 : 1); 27 };
64 - 剑指 Offer 59 - I. 滑动窗口的最大值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number[]} nums 3 * @param {number} k 4 * @return {number[]} 5 */ 6 var maxSlidingWindow = function(nums, k) { 7 let A = [], B = []; 8 if (nums.length == 0) 9 return []; 10 for (let i = 0; i < k - 1; i ++) 11 { 12 for (; A.length >= 1 && A[A.length - 1].val <= nums[i]; A.pop()); 13 A.push({index : i, val: nums[i]}); 14 } 15 for (let i = k - 1; i < nums.length; i ++) 16 { 17 for (; A.length >= 1 && A[A.length - 1].val <= nums[i]; A.pop()); 18 A.push({index : i, val: nums[i]}); 19 if (A[0].index <= i - k) A.shift(); 20 B.push(A[0].val); 21 } 22 return B; 23 };
65 - 剑指 Offer 59 - II. 队列的最大值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 var MaxQueue = function() { 2 this.a = [], this.b = []; 3 }; 4 5 /** 6 * @return {number} 7 */ 8 MaxQueue.prototype.max_value = function() { 9 if (this.b.length == 0) 10 return -1; 11 else 12 return this.b[0]; 13 }; 14 15 /** 16 * @param {number} value 17 * @return {void} 18 */ 19 MaxQueue.prototype.push_back = function(value) { 20 for (; this.b.length >= 1 && this.b[this.b.length - 1] < value; this.b.pop()); 21 this.b.push(value), this.a.push(value); 22 }; 23 24 /** 25 * @return {number} 26 */ 27 MaxQueue.prototype.pop_front = function() { 28 if (this.a.length == 0) 29 return -1; 30 let c = this.a.shift(); 31 if (c == this.b[0]) this.b.shift(); 32 return c; 33 }; 34 35 /** 36 * Your MaxQueue object will be instantiated and called as such: 37 * var obj = new MaxQueue() 38 * var param_1 = obj.max_value() 39 * obj.push_back(value) 40 * var param_3 = obj.pop_front() 41 */
66 - 剑指 Offer 37. 序列化二叉树
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Definition for a binary tree node. 3 * function TreeNode(val) { 4 * this.val = val; 5 * this.left = this.right = null; 6 * } 7 */ 8 9 /** 10 * Encodes a tree to a single string. 11 * 12 * @param {TreeNode} root 13 * @return {string} 14 */ 15 var serialize = function(root) { 16 let ans = new String(); 17 function dfs(x) 18 { 19 if (x == null) 20 { 21 ans = ans.concat(",null"); 22 return; 23 } 24 ans = ans.concat("," + x.val); 25 dfs(x.left), dfs(x.right); 26 } 27 dfs(root); 28 return ans.slice(1); 29 }; 30 31 /** 32 * Decodes your encoded data to tree. 33 * 34 * @param {string} data 35 * @return {TreeNode} 36 */ 37 var deserialize = function(data) { 38 let a = data.split(","); 39 function dfs() 40 { 41 let c = a.shift(); 42 if (c == "null") 43 return null; 44 var x = new TreeNode(Number(c)); 45 x.left = dfs(); 46 x.right = dfs(); 47 return x; 48 } 49 var root = dfs(); 50 return root; 51 }; 52 53 /** 54 * Your functions will be called as such: 55 * deserialize(serialize(root)); 56 */
67 - 剑指 Offer 38. 字符串的排列
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {string} s 3 * @return {string[]} 4 */ 5 var permutation = function(s) { 6 let a = Array.from(s).sort(); 7 let ans = []; 8 let n = a.length; 9 let next = true; 10 for (; next; ) 11 { 12 ans.push(a.join("")); 13 for (var i = n - 1; i > 0; i --) 14 if (a[i - 1] < a[i]) 15 break; 16 i --; 17 if (i < 0) 18 next = false; 19 for (var j = n - 1; j >= i; j --) 20 if (a[j] > a[i]) 21 break; 22 let t = a[i]; a[i] = a[j]; a[j] = t; 23 let s1 = a.slice(0, i + 1); 24 let s2 = a.slice(i + 1); 25 a = s1.concat(s2.reverse()); 26 } 27 return ans; 28 };
68 - 面试题19. 正则表达式匹配
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public boolean isMatch(String s, String p) { 3 int n = s.length(); 4 int m = p.length(); 5 boolean[][] Dp = new boolean[n + 1][m + 1]; 6 Dp[0][0] = true; 7 for (int i = 0; i <= n; i ++) 8 for (int j = 0; j <= m; j ++) 9 { 10 if (j == 0) continue; 11 if (p.charAt(j - 1) == '.') 12 { 13 if (i >= 1 && j >= 1) Dp[i][j] |= Dp[i - 1][j - 1]; 14 } 15 else if (p.charAt(j - 1) != '*') 16 { 17 if (i >= 1 && j >= 1) Dp[i][j] |= Dp[i - 1][j - 1] && (s.charAt(i - 1) == p.charAt(j - 1) || p.charAt(j - 1) == '.'); 18 } 19 else 20 { 21 if (j >= 2) 22 Dp[i][j] |= Dp[i][j - 2]; 23 if (i >= 1 && j >= 2) 24 Dp[i][j] |= (Dp[i - 1][j] && (s.charAt(i - 1) == p.charAt(j - 2) || p.charAt(j - 2) == '.')); 25 } 26 // System.out.println(i + " " + j + " " + Dp[i][j]); 27 } 28 return Dp[n][m]; 29 } 30 }
69 - 剑指 Offer 49. 丑数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number} n 3 * @return {number} 4 */ 5 var nthUglyNumber = function(n) { 6 let a = [1]; 7 let p1 = 0, p2 = 0, p3 = 0; 8 for (; a.length < n; ) 9 { 10 let x = a[p1] * 2, y = a[p2] * 3, z = a[p3] * 5; 11 a.push(Math.min(x, y, z)); 12 if (x == a[a.length - 1]) p1 ++; 13 if (y == a[a.length - 1]) p2 ++; 14 if (z == a[a.length - 1]) p3 ++; 15 } 16 return a[n - 1]; 17 };
70 - 剑指 Offer 60. n个骰子的点数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public double[] dicesProbability(int n) { 3 int[][] Dp = new int[n + 1][6 * n + 1]; 4 double[] answer = new double[6 * n - n + 1]; 5 int ans = 0; 6 Dp[0][0] = 1; 7 for (int i = 1; i <= n; i ++) 8 for (int j = 1; j <= 6; j ++) 9 for (int k = i + j - 1; k <= 6 * i; k ++) 10 Dp[i][k] += Dp[i - 1][k - j]; 11 for (int i = n; i <= 6 * n; i ++) 12 ans += Dp[n][i]; 13 for (int i = n; i <= 6 * n; i ++) 14 answer[i - n] = 1.0 * Dp[n][i] / ans; 15 return answer; 16 } 17 }
71 - 剑指 Offer 17. 打印从1到最大的n位数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number} n 3 * @return {number[]} 4 */ 5 var printNumbers = function(n) { 6 let limit = 1; 7 for (let i = 0; i < n; i ++) limit *= 10; 8 a = new Array(limit - 1); 9 for (let i = 1; i < limit; i ++) 10 a[i - 1] = i; 11 return a; 12 };
72 - 剑指 Offer 51. 数组中的逆序对
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number[]} nums 3 * @return {number} 4 */ 5 var reversePairs = function(nums) { 6 var ans = 0; 7 function Solve(l, r) 8 { 9 if (l >= r) return; 10 let mid = Math.floor((l + r) / 2); 11 Solve(l, mid), Solve(mid + 1, r); 12 let L = l, R = mid + 1, cnt = 0, temp = []; 13 for (; L <= mid && R <= r; ) 14 { 15 if (nums[L] <= nums[R]) ans += R - 1 - mid, temp[cnt ++] = nums[L ++]; 16 else temp[cnt ++] = nums[R ++]; 17 } 18 for (; L <= mid; L ++) temp[cnt ++] = nums[L], ans += R - 1 - mid; 19 for (; R <= r; R ++) temp[cnt ++] = nums[R]; 20 for (let i = l; i <= r; i ++) nums[i] = temp[i - l]; 21 // console.log(l, r, nums, ans); 22 } 23 Solve(0, nums.length - 1); 24 return ans; 25 };
73 - 剑指 Offer 14- II. 剪绳子 II
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number} n 3 * @return {number} 4 */ 5 var cuttingRope = function(n) { 6 if (n == 2) 7 return 1; 8 else if (n == 3) 9 return 2; 10 else 11 { 12 let ans = 1, mod = 1000000007; 13 for (; n > 4; n -= 3) 14 ans = ans * 3 % mod; 15 ans = ans * n % mod; 16 return ans; 17 } 18 };
74 - 剑指 Offer 43. 1~n 整数中 1 出现的次数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number} n 3 * @return {number} 4 */ 5 var countDigitOne = function(n) { 6 let res = 1, ans = 0; 7 for (; res <= n; res *= 10) 8 { 9 let d = Math.floor(n / (res * 10)); 10 ans += d * res; 11 let s = n % (res * 10); 12 if (s >= 2 * res) ans += res; 13 else if (s >= res) ans += s - res + 1; 14 } 15 return ans; 16 };
75 - 剑指 Offer 44. 数字序列中某一位的数字
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * @param {number} n 3 * @return {number} 4 */ 5 var findNthDigit = function(n) { 6 let a = [1], s = 1; 7 for (let i = 1; i <= 10; i ++) 8 a[i] = (s * 10 - s) * i, s *= 10; 9 for (let i = 1; i <= 10; i ++) 10 a[i] += a[i - 1]; 11 for (var i = 10; i >= 0; i --) 12 if (n >= a[i]) 13 break; 14 let temp = Math.floor((n - a[i]) / (i + 1)), mod = (n - a[i]) % (i + 1); 15 s = 1; for (let j = 0; j < i; j ++) s *= 10; s --; 16 s += temp; s ++; 17 // console.log(s); 18 let b = []; 19 for (; s; s = Math.floor(s / 10)) 20 b.unshift(s % 10); 21 // console.log(b[mod]); 22 return b[mod]; 23 };