Who can help me find the wrong with my code? Thank you very much!


  • 0
    L
     class Solution {
       public:
            string shortestPalindrome(string s) {	
                      string tmp;	
                      int size = s.size(),pos=0;	
                      for (int i =size; i >0; i--){		
                               if (isPalindrome(s.substr(0, i))){			
                                       pos = i;
    			                       break;	
    		                   }	
    	              } 
    	             tmp = s.substr(pos, size - pos);
    	             reverse(tmp.begin(), tmp.end());
    	             return tmp+s;
            }
          bool isPalindrome(const string& str){
    	     int size = str.size();
    	     for (int i= 0; i <= size/2; i++){
    		    if (str[i] != str[size - i - 1])
    			  return 0;
    	      }
    	      return 1;
          }
    };

Log in to reply
 

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