蜡烛之间的盘子 (Plates Between Candles)

 

思路:

// @Title: 蜡烛之间的盘子 (Plates Between Candles)
// @Author: qisiii
// @Date: 2022-03-08 13:00:19
// @Runtime: 7 ms
// @Memory: 102.2 MB
// @comment: 
// @flag: 
class Solution {
    public int[] platesBetweenCandles(String s, int[][] queries) {
        char[] str=s.toCharArray();
        int[] result=new int[queries.length];
        int n=str.length;
        int[] left=new int[n],right=new int[n];int leftIndex=-1,rightIndex=-1;
        int[] prefix=new int[n];
        for(int i=0;i<n;i++){
            if(str[i]=='|'){
                leftIndex=i;
            }
            left[i]=leftIndex;
            if(str[n-i-1]=='|'){
                rightIndex=n-i-1;
            }
            right[n-i-1]=rightIndex;
            if(i>0){
                prefix[i]=prefix[i-1];
            }
            if(str[i]=='*'){
                prefix[i]++;
            }
        }
        int count=0;
        for(int[] temp:queries){
            int l=right[temp[0]];
            int r=left[temp[1]];
            if(l==-1||r==-1){
                result[count++]=0;
                continue;
            }
            int e=prefix[r]-prefix[l];
            result[count++]=e>0?e:0;
        }
        return result;
    }

}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18