2022年12月2日 星期五

[763] Partition Labels

邊看答案邊寫的QQ
感覺自己陷入慌亂~不然應該可以自己寫出來的>< QQ
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
#define MAX(A,B) ((A>B)?(A):(B))
int* partitionLabels(char * s, int* returnSize){
int *hash=calloc(26, sizeof(int));
int *ans=calloc(26, sizeof(int));
for (int i=0;i<strlen(s);i++)
{
hash[s[i]-'a']=i;
}
int start=0, end=0,count=0;
for (int i=0;i<strlen(s);i++)
{
end=MAX(hash[s[i]-'a'],end);
if (i==end)
{
ans[count++]=(end-start)+1;
start=i+1;
}
}
*returnSize = count;
return ans;
}

沒有留言:

張貼留言