美文网首页
LeetCode #214 Shortest Palindrom

LeetCode #214 Shortest Palindrom

作者: 刘煌旭 | 来源:发表于2021-04-11 18:01 被阅读0次
Shortest_Palindrome.png
/*
*Abstract: to be added latter...
*/
char * shortestPalindrome(char * s){
    int n = strlen(s), hi = n - 1;
    while (hi >= 0) {
        int i = 0, j = hi;
        while (s[i] == s[j]) {
            I++;
            j--;
            if (i >= j) { break; }
        }
        if (i >= j) {
            break;
        } else {
            hi--;
        }
    }
    int len = n + (n - hi - 1);
    char *ret = (char*)malloc((len + 1) * sizeof(*ret));
    ret[len] = '\0';
    for (int i = n - 1; i > hi; i--) { ret[n - 1 - i] = s[i]; }
    for (int i = n - 1 - hi; i < len; i++) { ret[i] = s[i - (n - 1 - hi)]; }
    return ret;
}

相关文章

网友评论

      本文标题:LeetCode #214 Shortest Palindrom

      本文链接:https://www.haomeiwen.com/subject/hutzkltx.html