Leet Code 第 133 场周赛

1032. 字符流

题目链接

题解:

用字典中所有的元素构建后缀树,标记end节点,然后对于每个询问,都结合之前的询问进行后缀匹配查询,如果匹配到后缀树上的一个节点是end节点的话,最匹配成功。

注意用java 不要在String 后面添加元素,会报错,之间用一个LinkedList 即可。

代码:


class ListNode{
    ListNode[] next = new ListNode[26];
    boolean is_end;
    ListNode(){is_end = false;}
}

class StreamChecker {
 
     ListNode head;
   // public String buf;
    LinkedList<Character> buf = new LinkedList<>();
    public StreamChecker(String[] words) {
       head = new ListNode();  
      for(String word : words)  
      {
         ListNode temp = head;
         for(int j = word.length()-1; j >= 0; j--)
         {
            int k = word.charAt(j) - 'a';
            if(temp.next[k] == null) temp.next[k] = new ListNode();
            temp = temp.next[k];
         }
         temp.is_end = true;
      }
    }
    
    public boolean query(char letter) {
        
        ListNode temp = head;
        buf.addFirst(letter);
        for(char x : buf)
        {
            int k = x - 'a';
            if(temp.next[k] == null) return false;
            if(temp.next[k].is_end)  return true; 
            temp = temp.next[k];
        }
        return false;
    }
}

/**
 * Your StreamChecker object will be instantiated and called as such:
 * StreamChecker obj = new StreamChecker(words);
 * boolean param_1 = obj.query(letter);
 */

1031. 两个非重叠子数组的最大和

题目链接

题解:

先记录一个前缀和,然后两次循环进行选择即可,注意要使两次选择不重叠。

代码:

class Solution {
    public int maxSumTwoNoOverlap(int[] A, int L, int M) {
        int[] S = new int[A.length+1];
        S[0] = 0;
        for(int i = 1; i < S.length; i++)  {
            S[i] = S[i-1] + A[i-1];
        }
        int res = -2000000;
        for(int i = L; i < S.length; i++)  
        {
            int temp = 0;
            for(int j = M; j < S.length; j++)
            {
                if(j < i-L+1 || j-M+1 > i)
                {
                temp =  S[i] - S[i-L] + S[j] - S[j-M];
                res = Math.max(res,temp);
                }
            }
          }
        return res;
    }
}

1030. 距离顺序排列矩阵单元格

题目链接

题解:

将所有节点的曼哈顿距离求出来,然后排序即可。也可以用BFS进行搜索,时间复杂度会更小。

代码:

class Point{
    public int x;
    public int y;
    public int d;
    Point(int x, int y, int d)
    {
        this.x = x;
        this.y = y;
        this.d = d;
    }
}

class Solution {    
    public int[][] allCellsDistOrder(int R, int C, int r0, int c0) {
        int n = R*C;
        int[][] res = new int[R*C][2];
        Point[] p = new Point[n];
        int k = 0;
        for(int i = 0; i < R; i++)
            for(int j = 0; j < C; j++)
            {
                p[k] = new Point(i,j,Math.abs(i-r0) + Math.abs(j-c0));
                //System.out.println(p[k].x+" "+p[k].y);
                k++;
            }
        Arrays.sort(p,new Comparator <Point>(){

            public int compare(Point a, Point b)
            {
                return a.d -b.d;
            }
        });
        
        for(int i = 0; i < n; i++)
        {
            res[i][0] = p[i].x;
            res[i][1] = p[i].y;
        }
            
       return res; 
    }
}

1029. 两地调度

题目链接

题解:

对于任意两个节点前后问题,其实是两种方案花费的大小比较,所以以此为排序条件进行排序即可。

代码:

class Solution {
    public int twoCitySchedCost(int[][] costs) {
        Integer[] a = new Integer[costs.length];
        for(int i = 0; i < costs.length; i++)
            a[i] = i;
        Arrays.sort(a,new Comparator <Integer>(){
           public int compare(Integer x, Integer y)
           {
               return (costs[x][0]+costs[y][1]) - (costs[x][1] + costs[y][0]);
           }
        });
        int res = 0;
        for(int i = 0; i < a.length/2; i++)
        {
            res += costs[a[i]][0] + costs[a[i+a.length/2]][1];
        }
        return res;
        
        }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值