leetcode Palindrome Partitioning II
来源:互联网 发布:新浪模拟炒股软件 编辑:程序博客网 时间:2024/06/04 01:35
leetcode Palindrome Partitioning II 原题地址:
https://oj.leetcode.com/problems/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.
从后往前数,用cuts[i]来表示i...n的最小划分,在i...n中如果存在j使得i...j为回文,那么可以更新cuts[i] = min(cuts[i], cuts[j+1] + 1) /*i...j为回文,cuts[j+1]为j+1...n的最小划分*/ 而i...j是否为回文可以用动态规划来得到。
public class Solution { public int minCut(String s) { if (s == null) return 0; int len = s.length(); boolean[][] cut = new boolean[len][len]; int cuts[] = new int[len+1]; for (int i = 0; i < len; i++) { cuts[i] = len - i; } for (int i=len-1; i>=0; i--) for (int j=i; j<len; j++){ if (s.charAt(i) == s.charAt(j) && (j-i<2) || (s.charAt(i) == s.charAt(j) && cut[i+1][j-1])) { cut[i][j] = true; cuts[i] = Math.min(cuts[i], cuts[j+1]+1); } } return cuts[0] - 1; }}
0 0
- 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
- [Leetcode]Palindrome Partitioning II
- 【leetcode】Palindrome Partitioning II
- LeetCode Palindrome Partitioning II
- [leetcode]Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning II
- 记我的秋招
- 让Android模拟器飞一会
- 核心动画
- 聚类算法概述
- linux: 几个常用makefile模板
- leetcode Palindrome Partitioning II
- 第10周项目4-大奖赛得分
- 判断单链表中是否有环,如果有环则找到环的入口地址
- C语言程序设计案例精编----UDP程序设计
- C语言多线程的相关函数和实例
- 数据结构与算法-树2-二叉树(javascript描述)
- 十月学习感想
- C语言程序设计案例精编----万年历
- 懒惰之心