classSolution{public:
int numWaterBottles(int numBottles, int numExchange){
int a =0,b =0;
int sum =0;
sum+=numBottles;while(numBottles >= numExchange){
a =(numBottles)/ numExchange;
sum += a;
numBottles =(numBottles)% numExchange + a;}return sum;}};
class Solution {
String labels;
TreeSet<Integer>[] graph;
int[] ans;
public int[] countSubTrees(int n, int[][] edges, String labels) {
this.labels = labels;
graph = new TreeSet[n];
for (int i = 0; i < n; i++) {
graph[i] = new TreeSet<>();
}
for (int[] edge : edges) {
graph[edge[0]].add(edge[1]);
graph[edge[1]].add(edge[0]);
}
ans = new int[n];
dfs(0, new boolean[n]);
return ans;
}
int[] dfs(int root, boolean[] visited) {
visited[root] = true;
//以root为根节点的子树中每个字母的个数
int[] count = new int[26];
//先统计根节点
count[labels.charAt(root) - 'a']++;
for (int next : graph[root]) {
if (!visited[next]) {
//再统计每个子树
int[] res = dfs(next, visited);
for (int i = 0; i < 26; i++) {
count[i] += res[i];
}
}
}
//更新结果数组
ans[root] = count[labels.charAt(root)-'a'];
return count;
}
}
class Solution {
int [] res;
int [] arr;
public int closestToTarget(int[] arr, int target) {
res = new int [4 * arr.length];
this.arr = arr;
build_tree(0,arr.length-1,0);
int n =arr.length;
int l = 0;
int r = 0;
int t = Math.abs(-1000000000 + target);
while(l < n && r < n){
int tmp = query(0,n-1,l,r++,0);
t = Math.min(t, Math.abs(tmp - target));
if(tmp < target){
while(tmp < target && l < r){
l++;
tmp = query(0,n-1,l,r,0);
t = Math.min(t, Math.abs(tmp - target));
}
}
}return t;
}
void build_tree(int left,int right,int index)
{
if(left>=right)
{
res[index] = arr[left];
return;
}
int mid = (left + right)/2;
int left_index = index *2 + 1;
int right_index = index * 2 + 2;
build_tree(left,mid,left_index);
build_tree(mid+1,right,right_index);
res[index] = res[right_index]&res[left_index];
}
int query(int left,int right,int L,int R,int index)
{
if(L<=left&&R>=right)
return res[index];
if(L>right||left>R)
return Integer.MAX_VALUE;
int sum = query(left,(left+right)/2,L,R,index*2+1);
sum = sum & query((left+right)/2 + 1,right,L,R,index*2+2);
return sum;
}
}
力扣第 198 场周赛部分题解class Solution {public: int numWaterBottles(int numBottles, int numExchange) { int a = 0,b = 0; int sum = 0; sum+=numBottles; while (numBottles >= numExchange) { a = (numBottles)