Palindrome Partitioning II Leetcode
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.
给定一个字符串s,将s分割成一些子串,使每个子串都是回文。
比如,给出字符串s = "aab",
返回 1, 因为进行一次分割可以将字符串s分割成["aa","b"]这样两个回文子串
public class Solution {
public int minCut(String s) {
int n = s.length();
int[] cuts = new int[n];
boolean[][] dp = new boolean[n][n]; for (int i = 0; i < n; i++) {
cuts[i] = i;
for (int j = 0; j <= i; j++) {
if (s.charAt(i) == s.charAt(j) && (i - j <= 1 || dp[j + 1][i - 1])) {
dp[j][i] = true; if (j > 0) {
cuts[i] = Math.min(cuts[i], cuts[j - 1] + 1);
} else {
cuts[i] = 0;
}
} }
}
return cuts[n -1];
}
}
Palindrome Partitioning II Leetcode的更多相关文章
- Palindrome Partitioning II Leetcode java
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- 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】Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 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 ...
- 【LeetCode】132. Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
随机推荐
- 从Paxos到ZooKeeper-二、ZooKeeper和Paxos
ZooKeeper为分布式应用提供了高效且可靠的分布式协调服务,提供了诸如tong'yi统一命名服务.配置管理和分布式锁等分布式的基础服务.在解决分布式数据一致性方面,ZooKeeper并没有直接采用 ...
- (翻译)如何对python dict 类型按键(keys)或值(values)排序
如何对dict类型按键(keys)排序(Python 2.4 或更高版本): mydict = {'carl':40, 'alan':2, 'bob':1, 'danny':3} for key in ...
- python学习笔记-(十)面向对象基础
面向对象相关知识简介 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公用的.类变量定义 ...
- CentOS7搭建hadoop2.6.4+HBase1.1.6
环境: CentOS7 hadoop2.6.4两个节点:master.slave1 HBase1.1.6 过程: hadoop安装目录:/usr/hadoop-2.6.4 master节点,hadoo ...
- C++ 以费波纳茨数列为权重的加权均值计算方法 wMA
#pragma once #include <iostream> using namespace std; template <typename T> double *wMA( ...
- 使用 Object.create 创建对象,super 关键字,class 关键字
ECMAScript 5 中引入了一个新方法:Object.create().可以调用这个方法来创建一个新对象.新对象的原型就是调用 create 方法时传入的第一个参数: var a = {a: 1 ...
- ecshop 订单-》订单状态
/** * 取得状态列表 * @param string $type 类型:all | order | shipping | payment */ function get_status_list($ ...
- python 多线程学习
多线程(multithreaded,MT),是指从软件或者硬件上实现多个线程并发执行的技术 什么是进程? 计算机程序只不过是磁盘中可执行的二进制(或其他类型)的数据.它们只有在被读取到内存中,被操作系 ...
- flask安装及第一个程序
1.flask是一个轻量级的python web框架 ·1.Flask 依赖两个外部库: Jinja2 模板引擎和 Werkzeug WSGI 套件 ·2.安装: # easy_install fla ...
- Django笔记-数据库操作(多对多关系)
1.项目结构 2.关键代码: data6.settings.py INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', ' ...