I do a dp but it can be tested passed in location but in leetcode not


  • 0
    S

    this is my DP code

    int dp[1000][1000] = {0};
    char* longestPalindrome(char* s) {
    int length = strlen(s);
    int i = 0, j = 0;
    char lstr[1000] = "";
    int left = 0, maxLen = 1;

    if (!length) return "";
    if (length == 1)  return s;
    
    for (j = 0; j < length; j++) dp[j][j] = 1;
    
    for (j = 0; j < length - 1; j++) {
        if (s[j] == s[j+1]) {
           dp[j][j+1] = 1;
           left = j;
           maxLen = 2;
        }
    }
            
    for (int len = 3; len <= length; len++) {  
        for (int i = 0; i < length - len + 1; i++) {  
          int j = i + len - 1;  
          if (s[i] == s[j] && dp[i+1][j-1]) {  
            dp[i][j] = 1;  
            left = i;  
            maxLen = len;  
          }  
        }  
     }  
     
     for ( i = left, j = 0; i < left + maxLen; i++,j++) {
         lstr[j] = s[i];
     }
    //strncpy(lstr, s + left, maxLen);
    
    return lstr;
    

    }

    input is "aaabaaaa" but output is "aaabaaaa", i don't know why ,can anyone tell me?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.