Palindrome Partitioning II 改进

Given a string s, partition s such that every substring of the partition is a palindrome.

Return the minimum cuts needed for a palindrome partitioning of s.

For example, given s = "aab",
Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.

 维护两张表,一张一维的表是为了从后往前计算递归 以减小开销。

另外一张是 用在计算每一步的大小。(计算的很巧妙)

 1 public class Solution {

 2      boolean[][] store;

 3             int[] results;

 4             int len;

 5             public int minCut(String s) {

 6                 // Start typing your Java solution below

 7                 // DO NOT write main() function

 8                 len = s.length();

 9                 store = new boolean[len][len];

10                 results = new int[len];

11                 getStore(s);

12                 for(int pos = len - 1; pos > -1; pos --){

13                     finmin(s,pos);

14                 }

15                 return results[0];

16             }

17             public void getStore(String s){

18                 int header = 0;

19                 int tail = 0;

20                 for(int i = 0; i < len; i ++){

21                     store[i][i] = true;

22                 }

23                 // for odd length string

24                 for(int i = 1; i < len - 1; i ++){

25                     int k = 1;

26                     while(i + k < len && i - k > -1){

27                         if(s.charAt(i - k) == s.charAt(i + k)){

28                             store[i - k][i + k] = true;

29                         }

30                         else{

31                             break;

32                         }

33                         k ++;

34                     }

35                 }

36                 //for even length string

37                 for(int i = 0; i < len - 1; i ++){

38                     if(s.charAt(i) == s.charAt(i + 1)){

39                         store[i][i + 1] = true;

40                         int k = 1;

41                         while(i + 1 + k < len && i - k > -1){

42                             if(s.charAt(i - k) == s.charAt(i + 1 + k)){

43                                 store[i - k][i + 1 + k] = true;

44                             }

45                             else{

46                                 break;

47                             }

48                             k ++;

49                         }

50                     }

51                 }

52             }

53             public void finmin(String s,int pos){

54                 int cur = 0;

55                 int min = len;

56                 if(pos == len - 1){

57                     results[pos] = 0;

58                 }

59                 else{

60                     for(int i = pos; i < len - 1; i ++){

61                         if(store[pos][i] == true){

62                             cur = 1 + results[i + 1];

63                             if(cur < min){

64                                 min = cur;

65                             }

66                         }

67                     }

68                     if(store[pos][len - 1] == true)

69                         min = 0;

70                     results[pos] = min;

71                 }

72             }

73 }

 

你可能感兴趣的:(partition)