简介
我们自己观察题目发现了什么这是一道数学题,哈哈哈。
个人的思路是分成两类去判断,
第一种:
全是0
使用
[ (n-1) * (n - 2) / 2
]
第二种:
有1
然后观察10101
发现10101
其中0的个数两个之间
1个和1个
[ (前一堆1和中间一堆1之间的0的个数+1) * (后一堆1和中间一堆1之间的0的个数+1) = 结果
]
Tips
注意防止溢出
AC code
class Solution1573 {
public:
int numWays(string s) {
// step 1. count the number of the 1
long int numberOne = 0;
for(long int i=0; i<s.length(); i++){
if(s[i] == '1'){
numberOne++;
}
}
// step 2. check the numberOne can be subdivided 3?
if(numberOne % 3 != 0){
return 0;
}
if(numberOne == 0){
double n = s.length() - 1;
return (long int)((n / 2) * (n-1)) % (1000000000 + 7) ;
}
long int onePiece = numberOne / 3;
// step 3. get the numberZero between first piece and third piece
long int numberZero1 = 0; long int numberZero2 = 0;
long int index = 0;
long int status = -1;
for(long int i=0; i<s.length(); i++){
if(index == onePiece){
status = 0;
}
if(status == -1){
if(s[i] == '1'){
index++;
}
}
if(status == 0){
if(s[i] == '1'){
status = 1;
break;
}else if(s[i] == '0'){
numberZero1++;
}
}
}
string tmp;
tmp.resize(s.length());
long int j=0;
for(long int i=s.length() -1; i>=0; i--,j++){
tmp[j] = s[i];
}
index = 0;
status = -1;
for(long int i=0; i<tmp.length(); i++){
if(index == onePiece){
status = 0;
}
if(status == -1){
if(tmp[i] == '1'){
index++;
}
}
if(status == 0){
if(tmp[i] == '1'){
status = 1;
break;
}else if(tmp[i] == '0'){
numberZero2++;
}
}
}
// step 4. get the number
return ((numberZero1+1) % (1000000000 + 7) * (numberZero2+1) % (1000000000 + 7))% (1000000000 + 7);
}
long long factorial(long long n){
long long num = 1;
for(long long i=1; i <= n; i++){
num = num * i % (1000000000 + 7);
}
return num;
}
};
link
https://github.com/lishaohsuai/leetCode
https://github.com/haoel/leetcode