LeetCode Palindrome Partitioning, Palindrome Partitioning II
来源:互联网 发布:ios9关闭蜂窝移动数据 编辑:程序博客网 时间:2024/05/28 05:14
Palindrome Partitioning
Description:Given a string, an we need to find out all the palindrome partition, and store them in a List.
Solution:
I think we can split this problem into two steps.
1. find all the palindrome
2. since we need to store all the possible solutions, so simply a searching is enough and necessary
a better way to solve this problem is use the recorded arrays or lists like string[i], records all the possible solutions beginning with character i
import java.util.*;public class Solution {int length;boolean match[][];public ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();public ArrayList<ArrayList<String>> partition(String s) {length = s.length();match = new boolean[length + 2][length + 2];for (int i = 0; i < length; i++)match[i][i] = true;for (int i = 1; i < length; i++)if (s.charAt(i - 1) == s.charAt(i))match[i - 1][i] = match[i][i - 1] = true;int j;for (int l = 1; l <= length; l++) {for (int i = 0; (j = i + l - 1) < length; i++) {if (match[i][j] && i - 1 >= 0 && j + 1 < length&& s.charAt(i - 1) == s.charAt(j + 1)) {match[i - 1][j + 1] = true;}}}dfs(s, 0, "");return list;}void dfs(String s, int index, String next) {// this is where we can add our better solution, like below// if (record[index]!=null)//next * record[index] is what we want => list.add(...) if (index == length) {list.add(new ArrayList<String>(Arrays.asList(next.split(" "))));} else {for (int i = index; i < length; i++) {if (match[index][i]) {if (index != 0)dfs(s, i + 1, next + " " + s.substring(index, i + 1));elsedfs(s, i + 1, s.substring(index, i + 1));}}// to implement the better solution, we need to record[index] here}}public static void main(String[] args) {Solution s = new Solution();String str = "cbbbcc";System.out.println(s.partition(str));}}
Palindrome Partitioning II
Description:Given a string, and we have to figure out the minimum cut that needed to cut it into palindrome partition.
Solution:
Also in two steps.
1. find all the possible palindrome, also store it in the boolean[][] match
2. use a int[] count to represent that least cut number that needed to cut the substring [i,l). And also be aware that any palindrome partition from i can be written in [i,j] + count[j+1,l), where [i,j] is a palindrome
so count[i] = min( 1 + count[j+1] ), match[i][j] = true
public class Solution {int l;boolean match[][];int count[];int min;public int minCut(String s) {l = s.length();match = new boolean[l][l];count = new int[l + 1];for (int i = 0; i < l; i++)match[i][i] = true;for (int i = 1; i < l; i++)if (s.charAt(i - 1) == s.charAt(i))match[i - 1][i] = true;int j;for (int len = 1; len <= l; len++)for (int i = 0; (j = i + len - 1) < l; i++) {if (match[i][j] && i - 1 >= 0 && j + 1 < l&& s.charAt(i - 1) == s.charAt(j + 1))match[i - 1][j + 1] = true;}for (int i = l - 1; i >= 0; i--) {count[i] = 1 + count[i + 1];for (j = i; j < l; j++) {if (match[i][j])count[i] = Math.min(count[i], count[j + 1] + 1);}}return count[0] - 1;}public static void main(String[] args) {Solution s = new Solution();String st = "efe";System.out.println(s.minCut(st));}}
0 0
- 【leetcode】Palindrome Partitioning && Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning && Palindrome Partitioning II
- LeetCode Palindrome Partitioning I&&Palindrome Partitioning II
- LeetCode Palindrome Partitioning, Palindrome Partitioning II
- [Leetcode][python]Palindrome Partitioning/Palindrome Partitioning II
- Leetcode:Palindrome Partitioning & Palindrome Partitioning II
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode:Palindrome Partitioning II
- 【leetcode】Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- [LeetCode]Palindrome Partitioning II
- [Leetcode]Palindrome Partitioning II
- [leetcode]Palindrome Partitioning II
- leetcode Palindrome Partitioning II
- LeetCode-Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- LeetCode - Palindrome Partitioning II
- 约瑟夫环 数学解法
- 《统计学习方法》学习笔记(1)perceptron
- XX银行国库横联系统Websphere MQ中间件安全配置
- 初看linux内核启动过程
- 最长递增子序列详解(longest increasing subsequence)
- LeetCode Palindrome Partitioning, Palindrome Partitioning II
- http://codego.net/18365/
- Android笔记三十一.Service综合实例(一)
- C#探秘系列(二)
- linux ssh远程登陆和文件传输
- Windows环境下的NodeJS+NPM+Bower安装配置步骤
- Java_语法基础_浮点数 最近舍入模式
- 指针与数组
- android listview异步加载图片(详细)