🔗 leetcode.com/problems/ma…
题目
- 给一个字符串 s,由 0 和 1 组成,求 split 成两个非空字符串之后,score 的最大值
- socre = 左边字符串 0 的个数 + 右边字符串 1 的个数
思路
- 遍历,先统计所有 1 的个数
- 再遍历,统计这过程中 0 的个数和 1 的个数,score = tmp_count(0) + all_count(1) - tmp_count(1)
- 记录这过程中的最大值
代码
class Solution {
public:
int maxScore(string s) {
vector<int> count(2)
for (int i = 0
count[s[i] - '0']++
}
int ans = 0
vector<int> tmp(2)
if (s[0] == '0') {
ans = 1 + count[1]
tmp[0]++
} else {
ans = count[1] - 1
tmp[1]++
}
for (int i = 1
if (s[i] == '0') {
tmp[0]++
} else {
tmp[1]++
}
ans = max(ans, tmp[0] + count[1] - tmp[1])
}
return ans
}
}