B站左程云算法视频07

题目一:前缀树+深度优先遍历

public static class Node {
    public String name;
    public TreeMap<String, Node> nextMap;

    public Node(String name){
        this.name = name;
        nextMap = new TreeMap<>();
    }
}

public static void print(String[] folderPaths){
    if (folderPaths == null || folderPaths.lenght == 0){
        return;
    }
    //根据字符串,把前缀树建立好,头节点为head
    Node head = generateFolderTree(folderPaths);
    
    //打印
    printProcess(head, 0);
}

public static Node generateFolderTree(String[] folderPaths){
    Node head = new Node("");//系统根目录,前缀树头节点
    for(String foldPath : folderPaths){
        String[] paths = foldpath.split("\\\\");//java特性,用一个"\"做分割的意思
        Node cur = head;
        for (int i = 0; i < paths.length; i++){
            if (!cur.nextMap.containsKey(paths[i])){
                cur.nextMap.put(paths[i], new Node(paths[i]));
            }
            cur = cur.nextMap.get(paths[i]);
        }
    }
    return head;
}

public static void printProcess(Node node, int level){
    if (level != 0){
        //2 * (level - 1)
        System.out.println(get2nSpace(level) + node.name);
    }
    for(Node next : node.nextMap.values()){
        printprocess(next, level + 1);
    }
}


public static String get2nSpace(int n){
    String res = "";
    for (int i = 1; i < n; i++){
        res += "  ";
    }
    return res;
}

题目二:

双向链表节点结构和二叉树结构是一样的

last 认为是 left next 认为是next

给定一个搜索二叉树的头节点head,转化成一条有序的双向链表,并返回链表的头节点

public static class Info{
    public Node start;
    public Node end;

    public Info(Node start, Node end){
        this.start = start;
        this.end = end;
    }
}

//以x为头的整棵搜索二叉树,请全部以有序双向链表的方式,练好
//并且返回,整个有序双向链表的头结点和尾节点
public static Info process(Node X){
    if (X == null){
        return new Info(null, null);
    }
    Info leftHeadEnd = process(X.left);
    Info rightHeadEnd = process(X.right);
    if (leftHeadEnd.end != null){
        leftHeadEnd.end.right = X;
    }
    X.left = leftHeadEnd.end;
    X.right = rightHeadEnd.start;
    if (rightHeadEnd.start != null){
        rightHeadEnd.start.left = X;
    }
    return new Info(leftHeadEn.start != null ? leftHeadEnd.start : X,rightHeadEnd.end != null ? rightHeadEnd.end : X);
}

题目三:找到一棵二叉树中,最大的搜索二叉子树,返回最大搜索二叉子树的节点个数

对于节点X来说,可能性分为和X有关  和X无关

X无关:左树的maxBSTHead     右树的maxBSTHead

X无关:左isBST   右isBST  左max 右min   maxBST Size   

所以要返回的信息有 

Node maxBSTHead

boolean isBST

int  max

int min

maxBST Size

public static class Info{
    public NOode maxBSTHead;
    public boolean isBST;
    public int min;
    public int max;
    public int maxBSTSize;

    public Info(Node head, boolean is, int mi, int ma, int size){
        maxBSTHead = head;
        isBST = is;
        min = mi;
        max = ma;
        maxBSTSize = size;
    }
}

public static Info f(Node x) {
    if (x == null){
        return null;        
    }
    Info leftInfo = f(x.left);
    Info rightInfo = f(x.right);
    
    int min = x.value;
    int max = x.value;
    
    if(leftInfo != null){
        min = Math.min(min, leftInfo.min);
        max = Math.max(max, leftInfo.max);
    }
    if(rightInfo != null){
        min = Math.min(min, rightInfo.min);
        max = Math.max(max, rightInfo.max);
    }
    //下面探讨可能性1,2,3
    int maxBSTSize = 0;
    Node maxBSTHead = null;

    if(leftInfo != null){//可能性1的更新操作
        maxBstSize = leftInfo.maxBSTSize;
        maxBSTHead = leftInfo.maxBSTHead;
    }
    
    if(leftInfo != null && rightInfo.maxBSTSize > maxBSTSize){
        maxBstSize = rightInfo.maxBSTSize;
        maxBSTHead = rightInfo.maxBSTHead;
    }
    //可能性3的讨论
    boolean isBST = false;
    if( 
            ((leftInfo == null) || leftInfo.isBST) 
            && 
            ((rightInfo == null) || rightInfo.isBST) ) {
        if(  (leftInfo == null) || (leftInfo.max < x.value) 
            &&
             ((rightInfo == null) || rightInfo.min > x.value)){
                //有可能性3    
                isBST = true;
                Node maxBSTHead = x;
                int leftSize = leftInfo == null ? 0 :leftInfo.maxBSTSize;
                int rightSize = rightInfo == null ? 0 :rightInfo.maxBSTSize;
                maxBSTSize = leftSize + 1 + rightSize;
        }
    }
    
    return new Info(maxBSTHead, isBST, min, max, maxBSTSize);
    
    
}

题目四:子数组的最大连续之和

int cur = 0

int max = 系统最小

当前数累加到cur,如果cur变得比之前更大了,max更新

如果cur < 0, cur = 0

public static int maxSum(int[] arr) {
    if (arr == null || arr.length == 0){
        return 0;
    }
    int max = Integer.MIN_VAlUE;
    int cur = 0;
    for(int i = 0; i != arr.length; i++){
        cur += arr[i];
        max = Math.max(max, cur);
        cur = cur < 0 ? 0 : cur;
    }
    return max;
}

1)i ... ends<j   都大于等于0

2)[ ? ..... i - 1] < 0

题目5:给定一个整型矩阵,返回子矩阵的最大累加和(压缩数组)

先求0~0.再求0~1,再求0~2 .....

求1~1,1~2.....

......

压缩数组

public static int maxSum(int[][] m){
    if(m == null || m.length == 0 || m[0].length == 0){
        return 0;
    }
    int max = Integer.MIN_VALUE;
    int cur = 0;
    int[] s = null;
    for(int i = 0; i != m.length; i++){
        s = new int[m[0].length];
        for(int j = i; j != m.length; j++){
            cur = 0;
            for (int k = 0; k != s.length; k++){
                s[k] += m[j][k];
                cur += s[k];
                max = Math.max(max, cur);
                cur = cur < 0 ? 0 : cur;
            }
        }
    }
    return max;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值