import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return int整型
*/
public int countValidWays(String s) {
int ans = 0, f0 = 0, f1 = 0, f01 = 0, f10 = 0;
for (char c : s.toCharArray()) {
if (c == '0') {
// 当前可取区域为010,所以加上前两个区域01的个数
ans += f01;
// f0的个数++
f0++;
// 因为当前是0,所以10的个数++f1的个数
f10 += f1;
} else {
// 当前可取区域为101,所以加上前两个区域10的个数
ans += f10;
// f1的个数++
f1++;
// 因为当前是1,所以01的个数++f0的个数
f01 += f0;
}
}
return ans;
}
}
本题知识点分析:
1.字符串遍历
2.数学模拟
本题解题思路分析:
1.如果字符为0,当前可取区域为010,所以加上前两个区域01的个数,f0的个数++, 因为当前是0,所以10的个数++f1的个数
2. 当前可取区域为101,所以加上前两个区域10的个数, f1的个数++,因为当前是1,所以01的个数++f0的个数

京公网安备 11010502036488号