classSolution{public:
vector<int>platesBetweenCandles(string s, vector<vector<int>>& queries){int len = s.size();
vector<int>psum(len);//前缀和数组
vector<int>lp(len);//离该点左侧最近蜡烛所在位置for(int i =0, sum =0,l =-1; i < len; i ++){if(s[i]=='*') sum ++;if(s[i]=='|') l = i;
lp[i]= l;
psum[i]= sum;}
vector<int>rp(len);//离该点右侧最近蜡烛所在位置for(int i = len -1,r =-1; i >=0; i --){if(s[i]=='|') r = i;
rp[i]= r;}
vector<int> ans;for(auto& query : queries)//遍历每个区间{int x = rp[query[0]];//距离左区间右侧最近的蜡烛int y = lp[query[1]];//距离右区间左侧最近的蜡烛
ans.push_back(x ==-1|| y ==-1|| x >= y ?0: psum[y]- psum[x]);//x == -1 || y == -1 || x >= y 区间内没有蜡烛或者只有1支蜡烛}return ans;}};