24_3_10最长平衡字符串
详见力扣题解
class Solution
{
public:
int findTheLongestBalancedSubstring(string s)
{
vector<int> count = { 0,0 };
int res = 0;
int i = 0;
for (; i < s.size(); i++)
{
if (s[i] == '0')
{
if ((0 == i && s[i] == '0') || (s[i - 1] == '1' && s[i] == '0'))
{
count[0] = 1;
count[1] = 0;
}
else
{
count[0]++;
}
}
else
{
count[1]++;
res = max(res, 2 * min(count[0], count[1]));
}
}
return res;
}
};
24_3_12在受污染的二叉树中查找元素
1261. 在受污染的二叉树中查找元素 - 力扣(LeetCode)
class FindElements {
public:
FindElements(TreeNode* root)
{
//修正
Correct(root,0);
}
bool find(int target)
{
return (bool)Count[target];
}
private:
int Count[10000000] = {0};
void Correct(TreeNode*root, int rootval)
{
if (root==nullptr)
return;
root->val = rootval;
Count[rootval] = 1;
Correct(root->left, 2 * rootval + 1);
Correct(root->right, 2 * rootval + 2);
}
};
程序提交时总是报越界错误,一直都没找到,后来发现时Count数组的空间不够导致,Count空间开的过大会超出时间限制
24_3_12最大二进制奇数
std::cout——统计[first,last)区间与val相等的元素的个数
class Solution
{
public:
string maximumOddBinaryNumber(string s)
{
//统计
int S = s.size();
int n1= std::count(s.begin(), s.end(), '1');//1的个数
//重置string
for (int i = 0; i <S; i++)
{
if (i < n1-1 || i == S - 1)
{
s[i] = '1';
}
else
s[i] = '0';
}
return s;
}
};