狗哥の情书
//这个题首先把源字符串做分割,把列表中的每个词也做分割,判断源字符串的分割能否被列表中单词的分割一一对应上。其实重点就是如何按照重复情况进行字符串分割。
int expressiveWords(char* S, char** words, int wordsSize) {
int result = 0;
for(int i = 0; i < wordsSize; i++)//比较words[i] 和 s
{
int a = 0, b = 0, yes = 1;
while(words[i][a] != '\0' && S[b]!= '\0')
{
if(words[i][a] != S[b]) {yes = 0; break;}
else
{
int count_a = 0, count_b = 0;
char ca = words[i][a], cb = S[b];
while(words[i][a] == ca) {count_a++; a++;}
while(S[b] == cb) {count_b++; b++;}
if(count_a == count_b) continue;
else if(count_b < count_a || count_b < 3) {yes = 0; break;}
}
}
if (words[i][a] != '\0' || S[b]!= '\0') yes = 0;
if(yes) result++;
}
return result;
}
提莫进阶攻略
//题目给了我们一个timeSeries, 和一个 duration,让我们找出中毒的total 时间。流程大概就是每次检查相邻的两个段是否有重合,如果有,则加上不重合的部分;否则就加上整个duration。
int findPoisonedDuration(int* timeSeries, int timeSeriesSize, int duration) {
int total = 0;
int i;
if(timeSeriesSize>0){
total = duration;
}
for(i=1; i<timeSeriesSize; i++){
if(timeSeries[i] - timeSeries[i-1]>=duration){
total += duration;
}else{
total += timeSeries[i] - timeSeries[i-1];
}
}
return total;
}
网友评论