
剑指Offer
Desperate_gh
这个作者很懒,什么都没留下…
展开
-
剑指offer-3.26-49
暴力法:会超时class Solution { public int nthUglyNumber(int n) { int[] mark = new int[n]; int index = 0; for (int i = 1; ; i++) { int temp = i; while (temp != 1 && temp % 5 == 0) { temp.原创 2021-03-26 10:24:45 · 126 阅读 · 0 评论 -
剑指offer-3.25-48
class Solution { public int getIndex(char target, char[] c_s, int start, int end) { // 包含 start 和 end for (int i = end; i >= start; i--) { if (c_s[i] == target) { return i; } } return..原创 2021-03-25 16:55:56 · 121 阅读 · 0 评论 -
剑指offer-3.25-47
dp 数组:class Solution { public int maxValue(int[][] grid) { int m = grid.length, n = grid[0].length; int[][] dp= new int[m][n]; dp[0][0] = grid[0][0]; for (int i = 0; i < m; i++) { for (int j = 0; j < .原创 2021-03-25 15:37:41 · 148 阅读 · 0 评论 -
剑指offer-3.24-42
class Solution { public int maxSubArray(int[] nums) { int pre = nums[0], max = nums[0]; for (int i = 1; i < nums.length; i++) { pre = Math.max(pre + nums[i], nums[i]); max = Math.max(pre, max); } .原创 2021-03-24 13:06:12 · 146 阅读 · 0 评论 -
剑指offer-3.24-10.4-牛客
public class Solution { public int jumpFloorII(int target) { int[] dp = new int[target + 1]; dp[0] = 1; dp[1] = 1; for (int i = 2; i <= target; i++) { for (int j = 0; j < i; j++) { dp[i.原创 2021-03-24 11:07:42 · 154 阅读 · 0 评论 -
剑指offer-3.24-10.2-牛客
public class Solution { public int rectCover(int target) { if (target == 0 || target == 1 || target == 2) { return target; } int a = 1, b = 2, c = 0; for (int i = 3; i <= target; i++) { c = a..原创 2021-03-24 10:58:23 · 186 阅读 · 0 评论 -
剑指offer-3.24-10.2
class Solution { public int numWays(int n) { if (n == 0 || n == 1) { return 1; } int[] dp = new int[n + 1]; dp[1] = 1; dp[2] = 2; for (int i = 3; i <= n; i++) { dp[i] = (dp[i ..原创 2021-03-24 10:09:44 · 142 阅读 · 0 评论 -
剑指offer-3.24-10.1
class Solution { public int fib(int n) { int a = 0, b = 1, sum = 0; for (int i = 0; i < n; i++) { sum = (a + b) % 1000000007; a = b; b = sum; } return a; }}原创 2021-03-24 09:57:51 · 146 阅读 · 0 评论 -
剑指offer-3.22-51
暴力法:(超时)class Solution { public int reversePairs(int[] nums) { int sum = 0; for (int i = 0; i < nums.length - 1; i++) { for (int j = i + 1; j < nums.length; j++) { if (nums[i] > nums[j]) { .原创 2021-03-22 14:25:32 · 130 阅读 · 0 评论 -
剑指offer-3.20-45
选择排序:class Solution { public void swap(String[] str, int i, int j) { String temp = str[i]; str[i] = str[j]; str[j] = temp; } public String minNumber(int[] nums) { int len = nums.length; String[] str = n.原创 2021-03-20 17:54:57 · 130 阅读 · 0 评论 -
剑指offer-3.20-21
class Solution { public void swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } public int[] exchange(int[] nums) { int i = 0, j = nums.length - 1; while (i < j).原创 2021-03-20 16:50:42 · 141 阅读 · 0 评论 -
剑指offer-3.19-38
容易想的方法:class Solution { public void dfs (char[] c_str, int[] used, int depth, int len, StringBuffer sb, List<String> res) { if (depth == len) { String temp = new String(sb); if (!res.contains(temp)) { .原创 2021-03-19 15:25:16 · 266 阅读 · 0 评论 -
剑指offer-3.18-13
深度优先DFS:class Solution { private int cnt = 0; public int byteSum (int i, int j) { int sum = 0; while (i != 0) { sum += i %10; i /= 10; } while (j != 0) { sum += j % 10; .原创 2021-03-18 10:38:10 · 133 阅读 · 0 评论 -
剑指offer-3.17-12
class Solution { public boolean dfs(char[][] board, int i, int j, char[] words, int k) { if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || board[i][j] != words[k]) { return false; } if..原创 2021-03-17 11:04:52 · 104 阅读 · 0 评论 -
剑指offer-3.16-16
class Solution { public double myPow(double x, int n) { double res = 1.0; long b = n; if (x == 0) { return 0; } if (b < 0) { x = 1 / x; b = -b; } while (b ..原创 2021-03-16 15:02:20 · 111 阅读 · 0 评论 -
剑指offer-3.16-53.2
class Solution { public int missingNumber(int[] nums) { int i = 0, j = nums.length - 1; while (i <= j) { int m = (i + j) / 2; if (nums[m] == m) { i = m + 1; } else{ .原创 2021-03-16 14:22:42 · 89 阅读 · 0 评论 -
剑指offer-3.15-53.1
二分:class Solution { public int search(int[] nums, int target) { int i = 0, j = nums.length - 1; while (i <= j) { int m = (i + j) / 2; if (nums[m] <= target) { i = m + 1; } else.原创 2021-03-15 09:58:10 · 109 阅读 · 0 评论 -
剑指offer-2.12-11
class Solution { public int minArray(int[] numbers) { for (int i = 0; i < numbers.length - 1; i++) { if (numbers[i] > numbers[i + 1]) { return numbers[i + 1]; } } return numbers[0];.原创 2021-03-12 09:29:38 · 106 阅读 · 0 评论 -
剑指offer-3.11-63
class Solution { public int maxProfit(int[] prices) { int maxProfit = 0; for (int i = 0; i < prices.length - 1; i++) { for (int j = i + 1; j < prices.length; j++) { if (prices[j] - prices[i] > maxP..原创 2021-03-11 14:49:55 · 106 阅读 · 0 评论 -
剑指offer-3.10-14.1
class Solution { public int cuttingRope(int n) { if (n <= 3) { return n - 1; } int a = n / 3, b = n % 3; if (b == 0) { return (int)Math.pow(3, a); } if (b == 1) { .原创 2021-03-10 10:00:33 · 98 阅读 · 0 评论 -
剑指offer-3.9-68.2
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNo..原创 2021-03-09 15:33:30 · 99 阅读 · 0 评论 -
剑指offer-3.9-68.1
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNo..原创 2021-03-09 11:26:03 · 114 阅读 · 0 评论 -
剑指offer-3.9-55.2
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public int recur(TreeNode root) { if (root == null..原创 2021-03-09 10:32:54 · 126 阅读 · 0 评论 -
剑指offer-3.8-55.1
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { private int depth = 0, maxDepth = 0; public void dfs(Tr.原创 2021-03-08 13:23:19 · 108 阅读 · 0 评论 -
剑指offer-3.8-54
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { private int res, k; public void dfs(TreeNode root) { ..原创 2021-03-08 11:08:08 · 92 阅读 · 0 评论 -
剑指offer-3.8-37
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Codec { // Encodes a tree to a single string. public Strin.原创 2021-03-08 10:22:14 · 105 阅读 · 0 评论 -
剑指offer-3.5-36
/*// Definition for a Node.class Node { public int val; public Node left; public Node right; public Node() {} public Node(int _val) { val = _val; } public Node(int _val,Node _left,Node _right) { val = _val..原创 2021-03-05 10:09:01 · 203 阅读 · 1 评论 -
剑指offer-3.4-34
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public void backtracking(TreeNode node, int target, ArrayL..原创 2021-03-04 10:26:59 · 205 阅读 · 1 评论 -
剑指offer-3.4-33
class Solution { public boolean verify(int[] postorder, int start, int end) { if (start >= end) { return true; } int i = start; while (postorder[i] < postorder[end]) { i++; } ..原创 2021-03-04 10:05:38 · 173 阅读 · 1 评论 -
剑指offer-3.3-32.3
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public List<List<Integer>> levelOrder(TreeNode..原创 2021-03-03 14:54:24 · 248 阅读 · 2 评论 -
剑指offer-3.3-32.2
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public List<List<Integer>> levelOrder(TreeNode..原创 2021-03-03 11:36:13 · 130 阅读 · 0 评论 -
剑指offer-3.3-32.1
// return res.stream().mapToInt(Integer::valueOf).toArray();/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Sol...原创 2021-03-03 10:24:41 · 105 阅读 · 0 评论 -
剑指offer-3.2-28
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public boolean isSymmetrical(TreeNode left, TreeNode righ..原创 2021-03-02 10:55:07 · 97 阅读 · 0 评论 -
剑指offer-3.2-27
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode mirrorTree(TreeNode root) { if (ro..原创 2021-03-02 10:39:21 · 105 阅读 · 0 评论 -
剑指offer-3.2-41.2
public class Solution { private int[] cnts = new int[128]; private Queue<Character> queue = new LinkedList<Character>(); //Insert one char from stringstream public void Insert(char ch) { cnts[ch]++; queue.ad.原创 2021-03-02 10:10:14 · 97 阅读 · 0 评论 -
剑指offer-3.2-18.2
删除链表中重复的结点/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; }}*/public class Solution { public ListNode deleteDuplication(ListNode pHead) { ListNode vHead = new ListNode原创 2021-03-02 09:50:19 · 99 阅读 · 0 评论 -
剑指offer-3.1-23
(也是 leetcode 的142)/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode detec.原创 2021-03-01 11:57:26 · 114 阅读 · 0 评论 -
剑指offer-3.1-58.2
class Solution { public void reverse(char[] str, int start, int end) { while (start < end) { char temp = str[start]; str[start] = str[end]; str[end] = temp; start++; end--; } } public String reverseLeftWords(String s, int .原创 2021-03-01 11:03:55 · 121 阅读 · 0 评论 -
剑指offer-3.1-26
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public boolean isHaveSubTree(TreeNode A, TreeNode B) { ..原创 2021-03-01 10:56:44 · 141 阅读 · 0 评论 -
剑指offer-2.28-8
/*public class TreeLinkNode { int val; TreeLinkNode left = null; TreeLinkNode right = null; TreeLinkNode next = null; TreeLinkNode(int val) { this.val = val; }}*/public class Solution { public TreeLinkNode GetNext(.原创 2021-02-28 14:57:29 · 96 阅读 · 0 评论