Palindrome

866 Prime Palindrome

Find the smallest prime palindrome greater than or equal to N.
Recall that a number is prime if it's only divisors are 1 and itself, and it is greater than 1.
For example, 2,3,5,7,11 and 13 are primes.
Recall that a number is a palindrome if it reads the same from left to right as it does from right to left.
For example, 12321 is a palindrome.

  • 对偶数位的palindrome,一定能够被11整除,除了11本身以外,都不是prime。所以只考虑奇数位的palindrome。满足条件的两位数且是prime的palindrome只有11,接下去考虑三位数,五位数,etc
  • 生成所有奇数位的palindrome,如果它比N大且是prime,返回
class Solution {
    public int primePalindrome(int N) {
        if (N >= 8 && N <= 11) return 11;
        for (int i=1; i<100000; i++) {
            String x = Integer.toString(i);
            String y = new StringBuilder(x).reverse().toString().substring(1);
            int out = Integer.parseInt(x + y);
            if (out >= N && isPrime(out)) return out;
        }
        return -1;
    }
    public boolean isPrime(int x) {
        if (x < 2 || x % 2 == 0) return x == 2;
        for (int i = 3; i * i <= x; i += 2)
            if (x % i == 0) return false;
        return true;
    }
}

276 Palindrome Permutation II

  • Sort the array "int[] nums" to make sure we can skip the same value.
  • When a number has the same value with its previous, we can use this number only if his previous is used
private void backtracking(List res, String path, String base, boolean[] used, String mid) {
    if (path.length() == base.length()) {
        String toAdd = path + mid + new StringBuilder(path).reverse().toString();
        res.add(toAdd);
    }
    for (int i=0; i 0 && base.charAt(i) == base.charAt(i-1) && !used[i-1]) continue;
        used[i] = true;
        path += Character.toString(base.charAt(i));
        backtracking(res, path, base, used, mid);
        used[i] = false;
        path = path.substring(0, path.length() - 1);
    }
}

131 Palindrome Partitioning

Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.

Example:
Input: "aab"
Output:
[
["aa","b"],
["a","a","b"]
]

  • DP + DFS
  • Use a 2d array to keep track of any string we have scanned so far, with an addition pair, we can determine whether it's palindrome or not by justing looking at that pair, which is this line if(s.charAt(i) == s.charAt(j) && (i - j <= 2 || dp[j+1][i-1]))
    . This way, the 2d array dp contains the possible palindrome partition among all.
  • O(n^2)把整个string扫一遍,用简单的backtracking找到满足条件的解
class Solution {
    public List> partition(String s) {
        List> res = new ArrayList<>();
        boolean[][] dp = new boolean[s.length()][s.length()];
        /* dp[i][j] records whether s[j:i+1] is a palindrome*/
        for (int i=0; i(), s, 0);
        return res;
    }
    private void backtracking(List> res, boolean[][] dp, List path, String s, int index) {
        if (index == s.length()) {
            res.add(new ArrayList(path));
            return;
        }
        for (int i=index; i

你可能感兴趣的:(Palindrome)