随笔分类 - 算法
摘要:662. 二叉树最大宽度(优化解法,防止正常标记的id数超过int范围(全是右子树)) List<Integer> ls = new ArrayList<>(); // 记录每一层的最左节点初始id public int widthOfBinaryTree(TreeNode root) { retu
阅读全文
摘要:题目描述 给你一个食物网,你要求出这个食物网中最大食物链的数量。 (这里的“最大食物链”,指的是生物学意义上的食物链,即最左端是不会捕食其他生物的生产者,最右端是不会被其他生物捕食的消费者。) Delia 非常急,所以你只有 1 秒的时间。 由于这个结果可能过大,你只需要输出总数模上 8011200
阅读全文
摘要:1.JZ33 二叉搜索树的后序遍历序列 public boolean VerifySquenceOfBST(int [] sequence) { if(sequence == null || sequence.length == 0) { return false; } return process
阅读全文
摘要:1.JZ23 链表中环的入口结点 public ListNode EntryNodeOfLoop(ListNode pHead) { if(pHead == null || pHead.next == null || pHead.next.next == null) { return null; }
阅读全文
摘要:1.JZ13 机器人的运动范围 int ans = 0; public int movingCount(int threshold, int rows, int cols) { boolean dp[][] = new boolean[rows][cols]; process(dp,threshol
阅读全文
摘要:1.JZ3 数组中重复的数字 public int duplicate (int[] numbers) { int L = 0; while(L < numbers.length) { if(numbers[L] == L) { L ++; }else if(numbers[numbers[L]]
阅读全文
摘要:148.排序链表 public ListNode sortList(ListNode head) { ListNode cur = head; int length = 0; while(cur != null) { cur = cur.next; length ++; } ListNode h =
阅读全文
摘要:136.只出现一次的数字 public int singleNumber(int[] nums) { int ans = 0; for(int i : nums) { ans ^= i; } return ans; } 138.复制带随机指针的链表 public Node copyRandomLis
阅读全文
摘要:128.最长连续序列 public int longestConsecutive(int[] nums) { int len = 0; HashMap<Integer,Integer> hm = new HashMap<>(); for(int i : nums) { if(!hm.contains
阅读全文
摘要:124.二叉树的最大路径和 public int maxPathSum(TreeNode root) { if(root == null) { return 0; } return process(root).max; } public Info process(TreeNode root) { i
阅读全文
摘要:188.买卖股票的最佳时机4 public int maxProfit(int k, int[] prices) { if (prices == null || prices.length == 0) { return 0; } int N = prices.length; if (k >= N /
阅读全文
摘要:116.填充每个节点的下一个右侧节点 public Node connect(Node root) { if(root == null) { return null; } MyQueue myQueue = new MyQueue(); myQueue.add(root); while(!myQue
阅读全文
摘要:剑指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
阅读全文
摘要:103.二叉树的锯齿形层序遍历 public List<List<Integer>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); if(root == null) { return re
阅读全文
摘要: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 -
阅读全文
摘要:76.覆盖最小子串 public String minWindow(String s, String t) { if(s.length() < t.length()) { return ""; } char s1[] = s.toCharArray(); char t1[] = t.toCharAr
阅读全文
摘要: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; }
阅读全文
摘要:53.最大子数组和 public int maxSubArray(int[] nums) { int res = Integer.MIN_VALUE; int curRes = 0; if(nums == null || nums.length == 0) { return 0; } for(int
阅读全文
摘要:687.最长同值路径 public int longestUnivaluePath(TreeNode root) { if(root == null) { return 0; } return process(root).notBeginX - 1; } public Info process(Tr
阅读全文
摘要: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;
阅读全文

浙公网安备 33010602011771号