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;
        
        }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章