简单小模拟
class Solution {
public:
std::string::size_type sz = 0;
bool isAdditiveNumber(string num) {
for (int i = 0; i < num.length(); i++)
{
for (int j = i + 1; j < num.length(); j++)
{
if (check(num, i, j))
return true;
}
}
return false;
}
bool check(string num,int first,int second)
{
if(num[0]=='0'&&first!=0)
return false;
if(num[first+1]=='0'&&second!=first+1)
return false;
long long int number1 = std::stoll (num.substr(0, first - 0 + 1).c_str(),&sz,0);
long long int number2 = std::stoll (num.substr(first + 1, second - first).c_str(),&sz,0);
long long int number3 = number1 + number2;
int i = second+1,j = 0;
do
{
string s = to_string(number3);
if(num[i]=='0'&&i!=num.length()-1)
return false;
for (j = 0; i < num.length() && j < s.length(); i++, j++)
{
if (s[j] != num[i])
{
return false;
}
}
if (j != s.length())
{
return false;
}
number1 = number2;
number2 = number3;
number3 = number1 + number2;
first = second;
second = i - 1;
}
while (i < num.length());
return true;
}
};