6056. 字符串中最大的 3 位相同数字
class Solution {
public:
string largestGoodInteger(string num) {
string ans = "";
for(int i = 0;i < num.size();++i) {
if(num[i] == num[i +1] && num[i + 1] == num[i + 2]) {
string temp(3,num[i]);
//string temp = {num[i],num[i],num[i] };
ans = max(ans,temp);
}
}
return ans;
}
};
6057. 统计值等于子树平均值的节点数
class Solution {
public:
int ans = 0;
vector<int> getSum(TreeNode* root) {
if (!root) return { 0,0 };
int count = 1, sum = root->val;
// 中
vector<int> left = getSum(root->left);
// 左
vector<int> right = getSum(root->right);
// 右
count += left[0];
count += right[0];
sum += left[1];
sum += right[1];
if (sum / count == root->val) ans += 1;
return { count,sum };
}
int averageOfSubtree(TreeNode* root) {
ans = 0;
getSum(root);
return ans;
}
};
|