1、感觉写复杂了,将就看一下吧(有空再优化一下)
首先将数字串给读取下来,读取的时候判断前导0,
然后判断读取到的数字串有多少相等的。
然后输出不想的的数字串数。
class Solution {
public:
int numDifferentIntegers(string word) {
int thesize=word.size();
vector<vector<char>> nums;
vector<char> then;
int label=0;
while(label<thesize)
{
while(label<thesize&&(word[label]<'0'||word[label]>'9')) ++label;
while(label<thesize&&(word[label]<='9'&&word[label]>='0'))
{
if(!then.empty()||word[label]!='0')then.push_back(word[label]);
else if(label+1<thesize&&(word[label+1]<'0'||word[label+1]>'9'))then.push_back(word[label]);
else if(label+1==thesize) then.push_back(word[label]);
++label;
}
then.push_back('\0');
if(then[0]!='\0')nums.push_back(then);
then.clear();
}
thesize=label=nums.size();
if(label<=1) return label;
for(int i=0;i<thesize-1;++i)
for(int j=i+1;j<thesize;++j)
{
if(nums[i].size()!=nums[j].size()) continue;
if(jug_strsame(nums[i],nums[j]))
{
--label;
break;
}
}
return label;
}
bool jug_strsame(vector<char>& s1,vector<char>& s2)
{
for(int i=0;s1[i];++i) if((s1[i]^s2[i])!=0) return false;
return true;
}
};