动态规划——Palindrome Partitioning II
Input: "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.
dp[i] = min(dp[j-1]+1),当0<j<=i时,并且s[j..i]是回文时
dp[i] = 0 ,当j=0,并且s[j..i]是回文时
在具体的dp数组递推运算过程中,需要这两个分支,同时会更新pa[j][i]的值便于后面的过程中回文条件的判断。
public int minCut(String s) {
int slen = s.length();
int[][]pa = new int[slen][slen];
int[]dp = new int[slen];
for(int i = 0;i<slen;i++)
dp[i] = i;
for(int i = 0;i<slen;i++)
Arrays.fill(pa[i],0);
for(int i = 1;i<slen;i++) {
for(int j = 0;j<=i;j++) {
if(s.charAt(j)==s.charAt(i)&&((i-j<2)||pa[j+1][i-1]==1)) {
pa[j][i] = 1;
if(j!=0)dp[i] = Math.min(dp[i],dp[j-1]+1);
else dp[i] = 0;
}
}
}
return dp[slen-1];
}
动态规划——Palindrome Partitioning II的更多相关文章
- 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- 【leetcode】Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【LeetCode】132. Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- LeetCode: Palindrome Partitioning II 解题报告
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- leetcode132. Palindrome Partitioning II
leetcode132. Palindrome Partitioning II 题意: 给定一个字符串s,分区使分区的每个子字符串都是回文. 返回对于s的回文分割所需的最小削减. 例如,给定s =&q ...
随机推荐
- 吐血记录微信小程序授权获取Unionid及linux下使用bouncycastle解密用户数据 遇到的坑
背景 公司小程序上线了,发现系统无法拿到一些用户的UniondID.但是上线前的测试一切都是正常的. 坑1 经排查,发现一些用户通过下面的接口无法得到unionid https://api.weixi ...
- VS Less Compiler插件使用
1.打开扩展管理器,下载安装 2.新建一个test.less文件 3.敲入代码 @grayback: #808080; body { background:@grayback; } 4.保存即可自动生 ...
- shell 生成任意大小文件
$ dd if=/dev/zero of=junk.data bs=1M count=1 参数: if (input file) of (output file) bs(block size) co ...
- python selenium 最简单示例
使用 pip 安装 selenium 下载 chromedriver,添加在PATH中 # -*- coding: utf-8 -*- from selenium import webdriver ...
- 【gitlab】gitlab快速部署教程
gitlab快速部署教程 部署环境 Ubuntu 16.04(亲测可用) 开始部署 安装依赖 sudo apt-get install curl openssh-server ca-certifica ...
- 贪吃蛇游戏——C语言双向链表实现
采用了双向链表结点来模拟蛇身结点: 通过C语言光标控制函数来打印地图.蛇身和食物: /************************** *************************** 贪吃 ...
- vue 前端框架 (三)
VUE 生命周期 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- kali中的webshell工具--webacoo
webacoo webshell其实就是放置在服务器上的一段代码 kali中生成webshell的工具 WeBaCoo(Web Backdoor Cookie) 特点及使用方法 类终端的shell 编 ...
- Hibernate-注解
一, 为啥用注解 Hibernate注解使得原本放在xml文件中的信息直接表现在类中 为什么要用注解呢,因为注解可以简洁快速地在编写代码的同时实现映射关系 注解的好处就是语言简洁,即插即用. 坏处就 ...
- YOLO v3
yolo为you only look once. 是一个全卷积神经网络(FCN),它有75层卷积层,包含跳跃式传递和降采样,没有池化层,当stide=2时用做降采样. yolo的输出是一个特征映射(f ...