督促自己——编程题大杂烩(某扣、某客)——链表、数组、N叉树、MySQL

督促自己——编程题大杂烩——链表、数组、N叉树、MySQL

第一题:——链表组件
给定链表头结点 head,该链表上的每个结点都有一个 唯一的整型值 。
同时给定列表 G,该列表是上述链表中整型值的一个子集。
返回列表 G 中组件的个数,这里对组件的定义为:链表中一段最长连续结点的值(该值必须在列表 G 中)构成的集合。
示例 1:
输入:
head: 0->1->2->3
G = [0, 1, 3]
输出: 2
解释:
链表中,0 和 1 是相连接的,且 G 中不包含 2,所以 [0, 1] 是 G 的一个组件,同理 [3] 也是一个组件,故返回 2。
示例 2:
输入:
head: 0->1->2->3->4
G = [0, 3, 1, 4]
输出: 2
解释:
链表中,0 和 1 是相连接的,3 和 4 是相连接的,所以 [0, 1] 和 [3, 4] 是两个组件,故返回 2。

class Solution {
    
    
    public int numComponents(ListNode head, int[] G) {
    
    
        if(head==null||G.length==0){
    
    
            return 0;
        }
        ListNode cur=head;
        int maxlen=0;
        int len=0;
        while(cur!=null){
    
    
           if(search(cur.val,G)){
    
    
               len++;
           }else{
    
    
              if(len!=0){
    
    
                  maxlen++;
                  len=0;
              }
           }
           cur=cur.next;
        }
        maxlen=len==0?maxlen:maxlen+1;
        return maxlen;
    }
    public boolean search(int key,int[]num){
    
    
        for(int i=0;i<num.length;++i){
    
    
            if(num[i]==key){
    
    
                return true;
            }
        }
        return false;
    }
}

基本思路:遍历链表,找最长子链,记录个数。
第二题:——两数相加 II
给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。
你可以假设除了数字 0 之外,这两个数字都不会以零开头。
进阶:
如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转。
示例:
输入:(7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 8 -> 0 -> 7

class Solution {
    
    
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    
    
        if(l1==null&&l2==null){
    
    
            return null;
        }
       Stack<ListNode>stack1=new Stack<>();
       Stack<ListNode>stack2=new Stack<>();
       ListNode cur1=l1;
       ListNode cur2=l2;
       ListNode head=null;
       while(cur1!=null){
    
    
           stack1.push(cur1);
           cur1=cur1.next;
       }
        while(cur2!=null){
    
    
           stack2.push(cur2);
           cur2=cur2.next;
       }
       int c=0;
       while(!stack1.isEmpty()&&!stack2.isEmpty()){
    
    
           ListNode x=stack1.pop();
           ListNode y=stack2.pop();
             int sum=c+x.val+y.val;
             c=0;
             ListNode node=new ListNode(sum>=10?sum%10:sum);
             c=sum>=10?1:0;
             node.next=head;
             head=node;
       }
      while(!stack1.isEmpty()){
    
    
          int x=stack1.pop().val;
          ListNode node=new ListNode(x+c>=10?(x+c)%10:(x+c));
          node.next=head;
          c=x+c>=10?1:0;
          head=node;
      }
       while(!stack2.isEmpty()){
    
    
           int x=stack2.pop().val;
          ListNode node=new ListNode(x+c>=10?(x+c)%10:(x+c));
           c=x+c>=10?1:0;
          node.next=head;
          head=node;
      }
      if(stack2.isEmpty()&&stack1.isEmpty()&&c==1){
    
    
          ListNode node=new ListNode(1);
          node.next=head;
          head=node;
      }
      return head;
    }
}

基本思路:由于要求不能反转链表,且为单向链表,而求和又要从最后一个节点开始,所以我的做法是利用栈保存节点,因为入栈顺序与出栈顺序刚好相反,刚好贴合求和的计算顺序,所以之后就简单了,只要考虑清楚进位就行了。
第三题:——员工的重要性
给定一个保存员工信息的数据结构,它包含了员工 唯一的 id ,重要度 和 直系下属的 id 。
比如,员工 1 是员工 2 的领导,员工 2 是员工 3 的领导。他们相应的重要度为 15 , 10 , 5 。那么员工 1 的数据结构是 [1, 15, [2]] ,员工 2的 数据结构是 [2, 10, [3]] ,员工 3 的数据结构是 [3, 5, []] 。注意虽然员工 3 也是员工 1 的一个下属,但是由于 并不是直系 下属,因此没有体现在员工 1 的数据结构中。
现在输入一个公司的所有员工信息,以及单个员工 id ,返回这个员工和他所有下属的重要度之和。
示例:
输入:[[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
输出:11
解释:
员工 1 自身的重要度是 5 ,他有两个直系下属 2 和 3 ,而且 2 和 3 的重要度均为 3 。因此员工 1 的总重要度是 5 + 3 + 3 = 11 。

class Solution {
    
    
    public int getImportance(List<Employee> employees, int id) {
    
    
     Employee emp=search(employees,id);
     int sum=0;
     if(emp.subordinates.size()==0){
    
    
         return emp.importance;
     }else{
    
    
         sum+=emp.importance;
         for(int x:emp.subordinates){
    
    
           sum+=getImportance(employees,x);
         }
         return sum;
     }
    }
    public Employee search(List<Employee>list,int id){
    
    
        for(Employee emp:list){
    
    
            if(id==emp.id){
    
    
                return emp;
            }
        }
        return null;
    }
}

基本思路:利用递归公式:总重要度=当前员工重要度+员工的员工的重要度。
第四题:——N 叉树的最大深度
给定一个 N 叉树,找到其最大深度。
最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)。
在这里插入图片描述

输入:root = [1,null,3,2,4,null,5,6]
输出:3

class Solution {
    
    
    public int maxDepth(Node root) {
    
    
        if(root==null){
    
    
            return 0;
        }
        if(root.children.size()==0){
    
    
            return 1;
        }
        int maxlen=0;
        int len=root.children.size();
      for(Node node:root.children){
    
    
          maxlen=Math.max(maxlen,1+maxDepth(node));
      }
      return maxlen;
    }
}

基本思路:递归公式:maxlen=Math.max(maxlen,1+maxDepth(node));
第五题:——MySQL
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
你能使用子查询的方式找出属于Action分类的所有电影对应的title,description吗?

select film.title,film.description from film where film.film_id in (select film_id from film_category 
where category_id in (
select category_id from category 
where name='Action'
)
)

第六题:——MySQL
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
使用join查询方式找出没有分类的电影id以及名称。

select film.film_id,film.title from (film left join film_category on film_category.film_id=film.film_id)
left join category on category.category_id=film_category.category_id where
category.name<=>null;

关注我,和我一起写代码一起加油!

猜你喜欢

转载自blog.csdn.net/qq_45841205/article/details/115263831