package org.example.interview.practice; /** * @author xianzhe.ma * @date 2021/8/15 */ public class NC_62_IS_BALANCED { public boolean IsBalanced_Solution(TreeNode root) { if (root == null) { return true; } if (!IsBalanced_Solution(root.left)) { return false; } if (!IsBalanced_Solution(root.right)) { return false; } return Math.abs(deep(root.left) - deep(root.right)) <= 1; } public int deep(TreeNode node) { if (node == null) { return 0; } int leftDeep = deep(node.left); int rightDeep = deep(node.right); return Math.max(leftDeep, rightDeep) + 1; } public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; } }
package org.example.interview.practice; import java.util.Arrays; /** * @author xianzhe.ma * @date 2021/8/22 */ public class NC_63_IS_Continuous { public boolean IsContinuous(int [] numbers) { Arrays.sort(numbers); int max = numbers[4]; int min = numbers[0]; if (min == 0) { int zeroNum = 0; for (int i=0;i<4;i++) { if (numbers[i] == 0) { zeroNum++; } } for (int j = 3; j >= 0; j--) { if (numbers[j] != max-(4-j) ) { if (zeroNum <= 0) { return false; } else { zeroNum--; numbers[0] = max-(4-j); Arrays.sort(numbers); } } } } else { for (int j = 3; j >= 0; j--) { if (numbers[j] != max-(4-j)) { return false; } } } return true; } }
package org.example.interview.practice; /** * @author xianzhe.ma * @date 2021/11/3 */ public class NC_66_FindFirstCommonNode { public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) { ListNode l1 = pHead1, l2 = pHead2; while (l1 != l2) { l1 = (l1 == null) ? pHead2 : l1.next; l2 = (l2 == null) ? pHead1 : l2.next; } return l1; } public static class ListNode { int val; ListNode next = null; public ListNode(int val) { this.val = val; } } }
public class NC_68_JUMPFLOOR{ public int jumpFloor(int target) { if (target == 0) { return 1; } if (target == 1) { return 1; } return jumpFloor(target - 1) + jumpFloor(target - 2); } }