获取奇数回文

Get Odd Length Palindrome

我试图找到最长的奇数长度回文,但我编写的代码并没有给我完整的回文,只是其中的一部分。任何帮助都会很棒!

def get_odd_palindrome_at(s, index):
    ''' (str, int) -> str

    > get_odd_palindrome_at('tbababt', 3)
    'tbababt'
    > get_odd_palindrome_at('color', 2)
    'olo'
    '''

    palindrome = s[index]
    i = index

    for char in s:
        if s[i - 1] == s[i + 1]:
            palindrome = s[i - 1] + palindrome + s[i + 1]
            i = i + 1

    return palindrome

你每次都移动 i,所以你不会向两个方向扩展索引,而是每次都将你的 3 个字母的圆圈向右移动。您需要保留 original 索引,并且每次从 original 索引中添加或减去等量的索引:

How you want it to be:

c o l o r
  - i -
-   i   -

How it's doing:

c o l o r
  - i -
    - i -

所以实际上只是保存索引,并增加边距。此外,您只想迭代 index 边距,而不是字符串,因此:

def get_odd_palindrome_at (s, index):

    palindrome = s[index]

    for i in range(index):
        if s[index - i] == s[index + i]:
            palindrome = s[index - i] + palindrome + s[index + i]
        else:
            break

    return palindrome

使iindex保持距离,并确保不要越界。最后,只有在找到 i 的最终值时才构建结果字符串。每次迭代都这样做是没有用的:

def get_odd_palindrome_at(s, index):
    for i in range(1, index+1):
        if index + i >= len(s) or s[index - i] != s[index + i]:
            i -= 1
            break

    return s[index-i:index+i+1]

或者,您可以使用两个变量,这样可以稍微简化代码:

def get_odd_palindrome_at(s, index):
    i = index
    j = index
    while i >= 0 and j < len(s) and s[i] == s[j]:
        i -= 1
        j += 1

    return s[i+1:j]

**这是完整的工作代码,涵盖了even/odd和其他因素**

    public class NextSmallestPalindrome {

    static void utilityFunction(int array[], int n)  
    { 
    int mid = n / 2; 
        int i = mid - 1; 
        int j = (n % 2 == 0) ? mid : mid + 1; 
        boolean leftsmaller = false; 
  
        while (i >= 0 && array[i] == array[j])  
        { 
            i--; 
            j++; 
        } 
          
        if (i < 0 || array[i] < array[j])  
        { 
        leftsmaller = true; 
        } 
          
        while (i >= 0)  
        { 
        array[j++] = array[i--];  //code to swap
        } 
    
        if (leftsmaller)  
    { 
            int carry = 1; 
          
            if (n % 2 == 1) { 
                array[mid] += 1; 
                carry = array[mid] / 10; 
                array[mid] %= 10; 
        } 
            i = mid - 1; 
            j = (n % 2 == 0 ? mid : mid + 1); 
              
            while (i >= 0 && carry>0)   
            { 
                array[i] = array[i] + carry; 
            carry = array[i] / 10; 
                array[i] %= 10; 
                array[j] = array[i];
                i--; 
                j++; 
            } 
  
        } 
} 
  
    static void nextPalindromeProblem(int array[], int n)  
    { 
        System.out.println("Next Palindrome is:"); 
          
        if (isAll9(array, n)) { 
            System.out.print("1"); 
        for (int i = 0; i < n - 1; i++) 
            System.out.print("0"); 
            System.out.println("1"); 
  
        } 
      
        else { 
            utilityFunction(array, n); 
            printarray(array); 
        } 
    } 

    static boolean isAll9(int array[], int n) { 
        for (int i = 0; i < n; i++) 
            if (array[i] != 9) 
                return false; 
        return true; 
} 
  
    static void printarray(int array[]) { 
        for (int i = 0; i < array.length; i++) 
        System.out.print(array[i]); 
        System.out.println(); 
    } 
  
    public static void main(String[] args)  
    { 
    int array[] = { 2, 8, 3, 8, 4, 9}; 
        nextPalindromeProblem(array, array.length); 
    } 

}