随笔分类 -  算法

摘要:662. 二叉树最大宽度(优化解法,防止正常标记的id数超过int范围(全是右子树)) List<Integer> ls = new ArrayList<>(); // 记录每一层的最左节点初始id public int widthOfBinaryTree(TreeNode root) { retu 阅读全文
posted @ 2022-03-09 13:31 现在开始努力 阅读(26) 评论(0) 推荐(0)
摘要:题目描述 给你一个食物网,你要求出这个食物网中最大食物链的数量。 (这里的“最大食物链”,指的是生物学意义上的食物链,即最左端是不会捕食其他生物的生产者,最右端是不会被其他生物捕食的消费者。) Delia 非常急,所以你只有 1 秒的时间。 由于这个结果可能过大,你只需要输出总数模上 8011200 阅读全文
posted @ 2022-03-09 10:41 现在开始努力 阅读(126) 评论(0) 推荐(0)
摘要:1.JZ33 二叉搜索树的后序遍历序列 public boolean VerifySquenceOfBST(int [] sequence) { if(sequence == null || sequence.length == 0) { return false; } return process 阅读全文
posted @ 2022-02-28 15:09 现在开始努力 阅读(26) 评论(0) 推荐(0)
摘要:1.JZ23 链表中环的入口结点 public ListNode EntryNodeOfLoop(ListNode pHead) { if(pHead == null || pHead.next == null || pHead.next.next == null) { return null; } 阅读全文
posted @ 2022-02-25 11:56 现在开始努力 阅读(32) 评论(0) 推荐(0)
摘要:1.JZ13 机器人的运动范围 int ans = 0; public int movingCount(int threshold, int rows, int cols) { boolean dp[][] = new boolean[rows][cols]; process(dp,threshol 阅读全文
posted @ 2022-02-24 11:00 现在开始努力 阅读(60) 评论(0) 推荐(0)
摘要:1.JZ3 数组中重复的数字 public int duplicate (int[] numbers) { int L = 0; while(L < numbers.length) { if(numbers[L] == L) { L ++; }else if(numbers[numbers[L]] 阅读全文
posted @ 2022-02-23 17:07 现在开始努力 阅读(47) 评论(0) 推荐(0)
摘要:148.排序链表 public ListNode sortList(ListNode head) { ListNode cur = head; int length = 0; while(cur != null) { cur = cur.next; length ++; } ListNode h = 阅读全文
posted @ 2022-02-18 15:54 现在开始努力 阅读(45) 评论(0) 推荐(0)
摘要:136.只出现一次的数字 public int singleNumber(int[] nums) { int ans = 0; for(int i : nums) { ans ^= i; } return ans; } 138.复制带随机指针的链表 public Node copyRandomLis 阅读全文
posted @ 2022-02-17 15:36 现在开始努力 阅读(34) 评论(0) 推荐(0)
摘要:128.最长连续序列 public int longestConsecutive(int[] nums) { int len = 0; HashMap<Integer,Integer> hm = new HashMap<>(); for(int i : nums) { if(!hm.contains 阅读全文
posted @ 2022-02-16 15:29 现在开始努力 阅读(31) 评论(0) 推荐(0)
摘要:124.二叉树的最大路径和 public int maxPathSum(TreeNode root) { if(root == null) { return 0; } return process(root).max; } public Info process(TreeNode root) { i 阅读全文
posted @ 2022-02-15 16:34 现在开始努力 阅读(34) 评论(0) 推荐(0)
摘要:188.买卖股票的最佳时机4 public int maxProfit(int k, int[] prices) { if (prices == null || prices.length == 0) { return 0; } int N = prices.length; if (k >= N / 阅读全文
posted @ 2022-02-14 17:07 现在开始努力 阅读(53) 评论(0) 推荐(0)
摘要:116.填充每个节点的下一个右侧节点 public Node connect(Node root) { if(root == null) { return null; } MyQueue myQueue = new MyQueue(); myQueue.add(root); while(!myQue 阅读全文
posted @ 2022-02-11 16:54 现在开始努力 阅读(43) 评论(0) 推荐(0)
摘要:剑指Offer 47 public int maxValue(int[][] grid) { int M = grid.length; int N = grid[0].length; int dp[][] = new int[M][N]; dp[0][0] = grid[0][0]; for(int 阅读全文
posted @ 2022-01-26 16:34 现在开始努力 阅读(31) 评论(0) 推荐(0)
摘要:103.二叉树的锯齿形层序遍历 public List<List<Integer>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); if(root == null) { return re 阅读全文
posted @ 2022-01-25 15:38 现在开始努力 阅读(69) 评论(0) 推荐(0)
摘要:91.解码方法 public int numDecodings(String s) { char s1[] = s.toCharArray(); int N = s1.length; int dp[] = new int[N + 1]; dp[N] = 1; for(int index = N -  阅读全文
posted @ 2022-01-24 15:26 现在开始努力 阅读(37) 评论(0) 推荐(0)
摘要:76.覆盖最小子串 public String minWindow(String s, String t) { if(s.length() < t.length()) { return ""; } char s1[] = s.toCharArray(); char t1[] = t.toCharAr 阅读全文
posted @ 2022-01-24 10:34 现在开始努力 阅读(47) 评论(0) 推荐(0)
摘要:66.加一 public int[] plusOne(int[] digits) { int N = digits.length; for(int i = N - 1;i >= 0;i --) { if(digits[i] < 9) { digits[i] ++; return digits; } 阅读全文
posted @ 2022-01-19 15:59 现在开始努力 阅读(39) 评论(0) 推荐(0)
摘要:53.最大子数组和 public int maxSubArray(int[] nums) { int res = Integer.MIN_VALUE; int curRes = 0; if(nums == null || nums.length == 0) { return 0; } for(int 阅读全文
posted @ 2022-01-17 17:00 现在开始努力 阅读(31) 评论(0) 推荐(0)
摘要:687.最长同值路径 public int longestUnivaluePath(TreeNode root) { if(root == null) { return 0; } return process(root).notBeginX - 1; } public Info process(Tr 阅读全文
posted @ 2022-01-11 16:41 现在开始努力 阅读(28) 评论(0) 推荐(0)
摘要:45.跳跃游戏2 public int jump(int[] nums) { if(nums == null || nums.length < 1) { return 0; } int step = 0; int cur = 0; int next = nums[0]; for(int i = 1; 阅读全文
posted @ 2022-01-10 15:59 现在开始努力 阅读(34) 评论(0) 推荐(0)