• NC_62_IS_BALANCED NC_63_IS_Continuous NC_66_FindFirstCommonNode


    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);
        }
    }
  • 相关阅读:
    PHP中获取当前页面的URL信息
    $_POST和$GLOBALS['HTTP_RAW_POST_DATA'] 的区别
    curl模拟ip和来源进行网站采集的实现方法
    mysql修改root密码的几种方法
    微信小程序实现支付功能
    git获取远程服务器的指定分支
    mysql函数技巧整理
    sql 查询目标数据库中所有的表以其关键信息
    SET NOCOUNT ON
    C# CultureInfo中常用的InvariantCulture
  • 原文地址:https://www.cnblogs.com/juniorMa/p/15882004.html
Copyright © 2020-2023  润新知