自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(54)
  • 收藏
  • 关注

转载 剑指 Offer 07. 重建二叉树

class Solution { int[] preorder; HashMap<Integer, Integer> map = new HashMap<>(); // 前序遍历 preorder: 根 -- 左 -- 右 第一个肯定是根节点 // 中序遍历 inorder: 左 -- 根 -- 右 public TreeNode buildTree(int[] preorder, int[] inorder) { t...

2022-03-24 10:32:57 92

原创 六种排序写法

算法

2022-03-23 14:36:32 316

原创 剑指 Offer 34. 二叉树中和为某一值的路径

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.comclass Solution { // res 存放所有满足要求的路径 LinkedList<List<Integer>> res = new LinkedList<>(); // path 为正在遍历的路径 LinkedList<Integer> path = new LinkedList<>(); .

2022-03-12 01:20:05 2078

原创 剑指 Offer 13. 机器人的运动范围

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.comclass Solution { public int movingCount(int m, int n, int k) { // 使用临时变量 visited 记录格子是否被访问过 boolean[][] visited = new boolean[m][n]; // 开始深度优先遍历 return dfs(0, 0, m, n, k, visite.

2022-03-11 19:55:39 225

原创 剑指 Offer 12. 矩阵中的路径 递归

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.com// 作者:程序员吴师兄class Solution { public boolean exist(char[][] board, String word) { // 先将字符串进行拆分,一个一个元素进行匹配 char[] words = word.toCharArray(); // 通过两层嵌套,覆盖所有的情况 for(...

2022-03-11 16:35:00 105

原创 剑指 Offer 58 - I. 翻转单词顺序

class Solution { public String reverseWords(String s) { s = s.trim(); // 删除首尾空格 int j = s.length() - 1, i = j; StringBuilder res = new StringBuilder(); while(i >= 0) { while(i >= 0 && s.charAt(.

2022-03-10 00:22:21 60

原创 剑指 Offer 57. 和为s的两个数字

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.com// 作者:程序员吴师兄class Solution { public int[] twoSum(int[] nums, int target) { // 定义头指针 left int left = 0; // 定义尾指针 right int right = nums.length - 1; // .

2022-03-10 00:02:35 59

原创 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.com// 作者:程序员吴师兄class Solution { public int[] exchange(int[] nums) { // 定义头指针 left int left = 0 ; // 定义尾指针 right int right = nums.length - 1; // 定义临时变量 tmp.

2022-03-09 23:59:24 65

原创 剑指 Offer 52. 两个链表的第一个公共节点

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.compublic class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { // 边界判断 if (headA == null || headB == null) return null; // 从两个链表的头结点开始遍历 ListNode pA .

2022-03-09 23:25:49 257

原创 剑指 Offer 25. 合并两个排序的链表

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.com// 作者:程序员吴师兄class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { // 一开始设置一个虚拟节点,它的值为 -1,它的值可以设置为任何的数,因为我们根本不需要使用它的值 ListNode dummy = new ListNode(-1); .

2022-03-09 23:13:25 141

原创 剑指 Offer 22. 链表中倒数第k个节点

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.com// 作者:程序员吴师兄class Solution { public ListNode getKthFromEnd(ListNode head, int k) { //初始化两个指针 former 和 latter,一开始都指向链表的头节点 // 指针 former 指向链表的头节点 ListNode former = head; .

2022-03-09 23:03:40 214

原创 剑指 Offer 18. 删除链表的节点

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.com// 作者:程序员吴师兄class Solution { public ListNode deleteNode(ListNode head, int val) { //特殊情况处理,删除的节点是头结点时,比较特别 if(head.val == val) return head.next; //设置两个指针,一个指针指向当前的节点 .

2022-03-09 22:56:27 134

原创 剑指 Offer 45. 把数组排成最小的数

class Solution { public String minNumber(int[] nums) { String[] strs = new String[nums.length]; for(int i = 0; i < nums.length; i++) strs[i] = String.valueOf(nums[i]); quickSort(strs, 0, strs.length - 1); ...

2022-03-09 10:35:21 55

原创 剑指 Offer 48. 最长不含重复字符的子字符串

class Solution { public int lengthOfLongestSubstring(String s) { Map<Character, Integer> dic = new HashMap<>(); int res = 0, tmp = 0; for(int j = 0; j < s.length(); j++) { int i = dic.getOrDefault(s...

2022-03-09 10:35:12 117

原创 剑指 Offer 61. 扑克牌中的顺子

class Solution { public boolean isStraight(int[] nums) { Set<Integer> repeat = new HashSet<>(); int max = 0, min = 14; for(int num : nums) { if(num == 0) continue; // 跳过大小王 max = Math.max(ma..

2022-03-09 10:34:07 110

原创 剑指 Offer 46. 把数字翻译成字符串

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.com// 作者:程序员吴师兄class Solution { public int translateNum(int num) { // 将 int 变量 num 转换成字符串 String s = String.valueOf(num); // dp[i] 表示前 i 位可以解码的总数 // dp[0] 表示前 0 位可以解码.

2022-03-08 19:53:33 129

原创 剑指 Offer 47. 礼物的最大价值

class Solution { public int maxValue(int[][] grid) { int m=grid.length; int n=grid[0].length; for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if(i==0&&j==0) continue; if(i=..

2022-03-08 17:56:39 51

原创 剑指 Offer 42. 连续子数组的最大和

https://www.bilibili.com/video/BV1ML4y1h7S8?spm_id_from=333.999.0.0class Solution { public int maxSubArray(int[] nums) { int[] dp=new int[nums.length]; dp[0]=nums[0]; int max=dp[0]; for(int i=1;i<nums.length;i++){

2022-03-08 17:34:10 45

原创 剑指 Offer 63. 股票的最大利润

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.com// 作者:程序员吴师兄class Solution {public int maxProfit(int[] prices) { // 边界判断,如果交易日为 0 天,那么没得交易,返回 0 // 如果交易日为 1 天,只能当天买当天卖,利润为 0 if( prices.length < 2 ) return 0; // 设置 dp 数组,用来存放每天的最.

2022-03-08 17:21:07 101

原创 剑指 Offer 10- II. 青蛙跳台阶问题

class Solution { public int numWays(int n) { if(n==0||n==1) return 1; int[] res=new int[n+1]; res[1]=1; res[2]=2; for(int i=3;i<=n;i++){ res[i]=(res[i-1]+res[i-2])%1000000007; } re.

2022-03-08 16:40:01 58

原创 剑指 Offer 10- I. 斐波那契数列

//访问网站,获取更多题解:https://www.algomooc.comclass Solution { public int fib(int n) { //边界判断 if(n == 0) return 0; //用于存储第 0 到 n 个数对应的值 int[] dp = new int[n + 1]; //先定义好第一个数 dp[0] = 0; //再定义好第二个数 .

2022-03-08 16:13:22 50

原创 剑指 Offer 28. 对称的二叉树

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.com// 作者:程序员吴师兄class Solution { public boolean isSymmetric(TreeNode root) { // 边界情况 if(root == null) return true; // 递归判断左子树和右子树是否对称 return isSymmetriacalCor(root.left.

2022-03-08 14:01:38 48

原创 剑指 Offer 27. 二叉树的镜像

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.comclass Solution { public TreeNode mirrorTree(TreeNode root) { // 当节点为空时,直接返回 if(root == null) return null; // 设置一个临时的节点 tmp 用来存储当前节点的左子树 TreeNode tmp = root.left; .

2022-03-08 13:52:26 51

原创 剑指 Offer 26. 树的子结构

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.comclass Solution { public boolean isSubStructure(TreeNode A, TreeNode B) { // 一开始如果 A 或者 B 为空,直接返回 false // 因为题目约定空树不是任意一个树的子结构 if( A == null || B == null){ return.

2022-03-08 13:45:33 46

原创 剑指 Offer 32 - III. 从上到下打印二叉树 III

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.comclass Solution { public List<List<Integer>> levelOrder(TreeNode root) { // 设置 res 用来保存输出结果 List<List<Integer>> res = new LinkedList<>(); // 边.

2022-03-08 00:09:39 67

原创 剑指 Offer 32 - II. 从上到下打印二叉树 II

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.comclass Solution { public List<List<Integer>> levelOrder(TreeNode root) { // 设置 res 用来保存输出结果 List<List<Integer>> res = new LinkedList<>(); // 边界情.

2022-03-07 22:36:23 110

原创 面试题32 - I. 从上到下打印二叉树

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.comclass Solution { public int[] levelOrder(TreeNode root) { // 根节点为空的情况返回空数组 if (root == null) return new int[0]; // 生成一个队列,用来保存节点 Queue<TreeNode> queue = new Li.

2022-03-07 22:34:41 114

原创 面试题50. 第一个只出现一次的字符

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.com// 作者:程序员吴师兄class Solution { public char firstUniqChar(String s) { // 设置哈希表用来记录字符串中每个字符是否只出现过一次 // key 为字符 // value 为 boolean 类型 // true 代表当前遍历的字符中,该字符出现了一次 .

2022-03-07 20:06:35 64

原创 剑指 Offer 11. 旋转数组的最小数字

class Solution { public int minArray(int[] numbers) { //设置 start, end 指针分别指向 numbers 数组左右两端 int start = 0, end = numbers.length - 1; //循环判断处理,直到找到结果 while (start < end) { // mid 为中点(这里向下取整,比如 ( 2 + 7 )/.

2022-03-07 19:46:33 49

原创 剑指 Offer 04. 二维数组中的查找

class Solution { public boolean findNumberIn2DArray(int[][] matrix, int target) { //初始化 i 和 j 为数组左下角元素 int i = matrix.length - 1, int j = 0; //如果 i 和 j 没有越界继续判断 while(i >= 0 && j < matrix[0].lengt.

2022-03-07 19:24:53 83

原创 剑指 Offer 53 - II. 0~n-1中缺失的数字

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.com// 作者:程序员吴师兄class Solution { public int missingNumber(int[] nums) { // left 指向当前区间的最左边位置,所以初始化为 0 int left = 0 ; // right 指向当前区间的最右边位置,所以初始化为 nums.length - 1 int.

2022-03-07 13:43:12 70

原创 剑指 Offer 53 - I. 在排序数组中查找数字 I

二分查找 找出target和target-1的右边界即可。class Solution { public int search(int[] nums, int target) { return help(nums,target) - help(nums,target - 1); } int help(int [] nums,int target){ int j = nums.length - 1; int i = 0;.

2022-03-07 00:56:08 44

原创 剑指 Offer 03. 数组中重复的数字

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.com// 作者:程序员吴师兄class Solution { public int findRepeatNumber(int[] nums) { // HashSet 的特点是不会存储重复元素 // 所以可以利用 HashSet 来查找出重复的元素 Set<Integer> dic = new HashSet<>();.

2022-03-07 00:42:59 49

原创 剑指 Offer 58 - II. 左旋转字符串

class Solution { public String reverseLeftWords(String s, int n) { // 1、获取字符串的长度 int length = s.length(); // 2、把字符串转换为字符数组的形式 char[] chars = s.toCharArray(); // 3、把字符数组的所有字符反转 // 即 a b c d e f g .

2022-03-07 00:25:53 41

原创 剑指 Offer 05. 替换空格

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.com// 作者:程序员吴师兄class Solution { public String replaceSpace(String s) { // StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象 // 由于 StringBuilder 相较于 StringBuffer 有速度优势,所以多数情况下建议使用 StringBuilder.

2022-03-07 00:08:05 57

原创 剑指 Offer 35. 复杂链表的复制

简化写法:class Solution { public Node copyRandomList(Node head) { if(head == null) return null; Node cur = head; Map<Node, Node> map = new HashMap<>(); // 3. 复制各节点,并建立 “原节点 -> 新节点” 的 Map 映射 while(cu..

2022-03-06 23:34:55 116

原创 剑指 Offer 24. 反转链表

迭代方法public ListNode reverseList(ListNode head) { ListNode prev = null; ListNode curr = head; while (curr != null) { ListNode next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev;.

2022-03-06 23:17:14 53

原创 剑指 Offer 06. 从尾到头打印链表

// 登录 AlgoMooc 官网获取更多算法图解// https://www.algomooc.com// 作者:程序员吴师兄class Solution { public int[] reversePrint(ListNode head) { // 构建一个栈,用来存储链表中每个节点的值 Stack<Integer> stack = new Stack<>(); // 构建一个指针,指向链表的头结点位置,从它开始.

2022-03-06 22:56:05 476

原创 剑指 Offer 30. 包含min函数的栈

博客地址:https://mp.weixin.qq.com/s/hEGKU844FgHTurzXR39VYw视频地址:https://www.bilibili.com/video/BV1Df4y1w7b5?spm_id_from=333.999.0.0class MinStack { LinkedList<Integer> stack1; LinkedList<Integer> stack2; /** initialize your data st

2022-03-03 01:05:08 132

原创 剑指 Offer 09. 用两个栈实现队列

剑指 Offer 09. 用两个栈实现队列讲解博客:https://mp.weixin.qq.com/s/iUW9uGUvs9dS_fqIvevd3Aclass CQueue { LinkedList<Integer> stack1; LinkedList<Integer> stack2; public CQueue() { // 初始化 stack1 与 stack2 stack1 = new LinkedList

2022-03-03 00:33:12 268

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除