![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
剑指offer
Anrys
这个作者很懒,什么都没留下…
展开
-
剑指offer--69 II 二叉树的最近公共祖先
剑指offer--69 II 二叉树的最近公共祖先题目代码结果题目代码class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null) return null; if (root == p || root == q) return root; //两个递归返回的是root!原创 2021-08-02 00:20:08 · 160 阅读 · 0 评论 -
java刷题--264丑数II
java刷题--264丑数II题目代码结果题目代码class Solution { public int nthUglyNumber(int n) { int n2 = 0,n3 = 0,n5 = 0; int[] dp = new int[n]; dp[0] = 1; for(int i = 1;i<n;i++){ dp[i] = Math.min(2*dp[n2],Math.min(3*dp[n原创 2021-07-27 14:51:26 · 79 阅读 · 0 评论 -
剑指offer--38字符串的排列
剑指offer--38字符串的排列题目代码结果题目代码class Solution { public String[] permutation(String s) { List<String> list = new ArrayList(); char[] arr = s.toCharArray(); StringBuilder sb = new StringBuilder(); boolean[] visited =原创 2021-07-26 23:33:49 · 106 阅读 · 0 评论 -
剑指offer--31栈的压入、弹出序列
剑指offer--31栈的压入、弹出序列题目代码结果题目代码class Solution { public boolean validateStackSequences(int[] pushed, int[] popped) { Deque<Integer> stack = new ArrayDeque<>(); int j = 0; //索引popped for (int i = 0; i < pushed.le原创 2021-07-26 22:36:20 · 90 阅读 · 0 评论 -
剑指offer--40最小的k个数
剑指offer--40最小的k个数题目代码结果题目代码class Solution { public int[] getLeastNumbers(int[] arr, int k) { if (k == 0 || arr.length == 0) return new int[0]; //实现升序排列即头节点是最大的--需要重写一下比较器。 Queue<Integer> pq = new PriorityQueue<>(原创 2021-07-26 20:40:43 · 72 阅读 · 0 评论 -
剑指offer--35复杂链表的复制
剑指offer--35复杂链表的复制题目代码结果题目代码class Solution { public Node copyRandomList(Node head){ if(head == null) return null; Map<Node,Node> map = new HashMap<>(); for(Node cur = head; cur != null; cur = cur.next){原创 2021-07-26 19:32:12 · 95 阅读 · 0 评论 -
剑指offer--30 最小栈
剑指offer--30 最小栈题目代码结果题目代码这代码舒服class MinStack { Stack<Integer> A, B; public MinStack() { A = new Stack<>(); B = new Stack<>(); } public void push(int x) { A.add(x); if(B.empty() || B.pee原创 2021-07-25 00:32:38 · 54 阅读 · 0 评论 -
剑指offer--26树的子结构
剑指offer--26树的子结构题目代码结果题目代码class Solution { public boolean isSubStructure(TreeNode A, TreeNode B) { if (B == null || A == null) return false; if (A.val == B.val && (helper(A.left, B.left) && helper(A.right, B.right)))原创 2021-07-24 09:15:33 · 99 阅读 · 0 评论 -
剑指offer--22链表中倒数第k个节点
剑指offer--22链表中倒数第k个节点题目代码结果题目代码class Solution { public ListNode getKthFromEnd(ListNode head, int k) { ListNode fast = head,slow = head; for(int i=0;i<k;i++) { fast = fast.next; } while(fast!=null) {原创 2021-07-24 08:30:44 · 64 阅读 · 0 评论 -
剑指offer--29顺时针打印矩阵
剑指offer--29顺时针打印矩阵题目代码结果题目代码public class Solution { public int[] spiralOrder(int[][] matrix) { int row = matrix.length; if (row == 0) return new int[0]; int col = matrix[0].length; int[] res = new int[row * col];原创 2021-07-22 23:50:00 · 128 阅读 · 1 评论 -
剑指offer--28对称的二叉树
剑指offer--28对称的二叉树题目代码结果题目代码class Solution { public boolean isSymmetric(TreeNode root) { if (root == null) return true; return helper(root.left, root.right); } public boolean helper(TreeNode root1, TreeNode root2) {原创 2021-07-22 22:38:33 · 64 阅读 · 0 评论 -
剑指offer--27二叉树的镜像
剑指offer--27二叉树的镜像题目代码结果题目代码class Solution { public TreeNode mirrorTree(TreeNode root) { if(root == null) return null; LinkedList<TreeNode> queue = new LinkedList<>(); queue.offer(root); //其实就是假前序遍历原创 2021-07-22 19:01:59 · 75 阅读 · 0 评论 -
剑指offer--21调整数组顺序使奇数位于偶数前
剑指offer--21调整数组顺序使奇数位于偶数前题目代码结果题目代码class Solution { public int[] exchange(int[] nums) { int i=0,j=0; while(j<nums.length){ //奇数与1是1,偶数与1是0 if((nums[j]&1)!=0){ int tmp=nums[i];原创 2021-07-19 23:29:12 · 107 阅读 · 2 评论 -
剑指offer--20表示数值的字符串
剑指offer--20表示数值的字符串题目代码结果题目代码class Solution { public boolean isNumber(String s) { if (s == null || s.length() == 0) return false; s = s.trim();//去掉首位空格 boolean numFlag = false; boolean dotFlag = false; boolea原创 2021-07-19 22:45:24 · 80 阅读 · 0 评论 -
剑指offer--18删除链表的节点
剑指offer--18删除链表的节点题目代码结果题目代码class Solution { public ListNode deleteNode(ListNode head, int val) { if(head.val == val) return head.next; ListNode pre = head, cur = head.next; while(cur != null && cur.val != val) {原创 2021-07-19 22:07:44 · 68 阅读 · 0 评论 -
java刷题--1014最佳观光组合
java刷题--1014最佳观光组合题目代码结果题目代码稍微给这个公式变形成A[i]+i+A[j]-j,这样就可以看成是左A[i]+i和右A[j]-j两部分和的最大值。随着遍历数组,我们对两部分和取最大值,并且若当前的值—下标对之和比之前更大,我们就更新left部分的值即可。class Solution { public int maxScoreSightseeingPair(int[] values) { int res = 0, left = values[0]+0;原创 2021-07-18 23:10:50 · 51 阅读 · 0 评论 -
剑指offer--14-II 剪绳子
剑指offer--14-II 剪绳子题目代码结果题目代码和剪绳子I不同的是,此题涉及到大数运算,要么用BigInteger类要么如下代码所示根据数学原理尽可能接近3输出。class Solution { public int cuttingRope(int n) { if(n < 4) return n - 1; long res = 1; while(n > 4){ res = res * 3 % 10原创 2021-07-18 22:38:16 · 98 阅读 · 1 评论 -
剑指offer--14-I 剪绳子
剑指offer--14-I 剪绳子题目![在这里插入图片描述](https://img-blog.csdnimg.cn/2021071818185891.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0Fucnlz,size_16,color_FFFFFF,t_70)代码结果题目代码class Solution { public int原创 2021-07-18 18:19:58 · 71 阅读 · 0 评论 -
java刷题-剑指offer 12 矩阵中的路径
java刷题-剑指offer 12 矩阵中的路径题目代码结果题目代码class Solution { public boolean exist(char[][] board, String word) { if (board == null || board.length == 0 || board[0].length == 0) return false; char[] chars = word.toCharArray(); //新建一个默认原创 2021-06-24 22:53:06 · 68 阅读 · 0 评论 -
java刷题-剑指offer 11 旋转数组的最小数字
java刷题-剑指offer 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-06-24 22:50:27 · 165 阅读 · 0 评论 -
java刷题-剑指offer 07 重建二叉树
java刷题-剑指offer 07 重建二叉树题目代码结果题目代码class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { int n = preorder.length; if (n == 0) return null; int rootVal = preorder[0], rootIndex = 0; for (int i = 0;原创 2021-06-18 02:00:42 · 89 阅读 · 0 评论 -
java刷题--剑指offer 10 斐波那契数列
java刷题--剑指offer 10 青蛙跳台阶问题题目代码结果题目代码class Solution { public int numWays(int n) { if(n==0) return 1; int f1 = 1, f2 = 1; for(int i = 2; i < n+1; i++) { int temp = (f1 + f2)%1000000007; f1 = f2 ;原创 2021-06-16 01:45:08 · 80 阅读 · 0 评论 -
java刷题--剑指offer 10 斐波那契数列
java刷题--剑指offer 10 斐波那契数列题目代码结果题目代码class Solution { public int fib(int n) { if(n==0) return 0; int f1 = 0, f2 = 1 ; for(int i=2;i<=n;i++) { int temp = (f1+f2) % 1000000007; f1 = f2; f2原创 2021-06-16 01:30:38 · 121 阅读 · 0 评论 -
java刷题--剑指offer09 用两个栈实现队列
java刷题--剑指offer09 用两个栈实现队列题目代码结果题目代码class CQueue { Stack<Integer> stack1 = new Stack<>(); Stack<Integer> stack2 = new Stack<>(); public CQueue() { //无参构造方法 } //入队,我们往第一个栈压入值 public void appendTail(int val原创 2021-06-16 00:43:16 · 74 阅读 · 0 评论 -
java刷题--剑指offer06 从头到尾打印链表
java刷题--剑指offer06 从头到尾打印链表题目代码结果题目代码class Solution { public int[] reversePrint(ListNode head) { Stack<ListNode> stack = new Stack(); ListNode pre = head; while(pre!=null) { stack.push(pre); pre原创 2021-06-15 23:55:13 · 100 阅读 · 0 评论 -
java刷题--剑指offer05 替换空格
java刷题--剑指offer05 替换空格题目代码结果题目代码class Solution { public String replaceSpace(String s) { StringBuilder sb = new StringBuilder(); for(char ss: s.toCharArray()) { if(ss == ' ') sb.append("%20"); else sb.append(s原创 2021-06-15 23:31:10 · 77 阅读 · 0 评论 -
剑指offer04二维数组中的查找
剑指offer04二维数组中的查找题目代码结果题目代码class Solution { public boolean findNumberIn2DArray(int[][] matrix, int target) { int i = matrix.length-1,j=0; while(i >= 0 && j < matrix[0].length) { if(matrix[i][j] > target)原创 2021-06-14 15:34:01 · 49 阅读 · 0 评论