leetcode 131. 分割回文串(dp+回溯)

给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。

回文串 是正着读和反着读都一样的字符串。

示例 1:

输入:s = “aab”
输出:[[“a”,“a”,“b”],[“aa”,“b”]]
示例 2:

输入:s = “a”
输出:[[“a”]]

解题思路

动态规划:
dp[i][j]==true表示字符串s的[i…j]子串为回文字符串
状态转移:if(s.charAt(i)==s.charAt(j))
dp[i][j]=dp[i+1][j-1];
(两头的字符相等,并且中间的字符串是回文的话,就是一个更长的回文子串)

回溯:
根据dp数组,搜索出所有可能的组合

代码

class Solution {List<List<String>> res=new ArrayList<List<String>>();public  void back(int s,boolean[][] dp,List<String> list,String k){if(s==dp.length){res.add(new ArrayList<String>(list));return;}   for(int i=s;i<dp.length;i++){if(dp[s][i]){list.add(k.substring(s,i+1));back(i+1,dp,list,k);  list.remove(list.size()-1);}}}public  List<List<String>> partition(String s) {int n=s.length();boolean[][] dp=new boolean[n][n];for (int i = n-1; i >=0; i--) {for (int i1 = i; i1 < n; i1++) {if(s.charAt(i)==s.charAt(i1)){if(i1-i+1>2)dp[i][i1]=dp[i+1][i1-1];elsedp[i][i1]=true;}}}back(0,dp,new ArrayList<String>(),s);return res;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391653.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[翻译练习] 对视图控制器压入导航栈进行测试

译自&#xff1a;swiftandpainless.com/testing-pus… 上个月我写的关于使用 Swift 进行测试驱动开发的书终于出版了&#xff0c;我会在本文和接下来的一些博文中介绍这本书撰写过程中的一些心得和体会。 在本文中&#xff0c;我将会展示一种很好的用来测试一个视图控制器是否因…

python多人游戏服务器_Python在线多人游戏开发教程

python多人游戏服务器This Python online game tutorial from Tech with Tim will show you how to code a scaleable multiplayer game with python using sockets/networking and pygame. You will learn how to deploy your game so that people anywhere around the world …

版本号控制-GitHub

前面几篇文章。我们介绍了Git的基本使用方法及Gitserver的搭建。本篇文章来学习一下怎样使用GitHub。GitHub是开源的代码库以及版本号控制库&#xff0c;是眼下使用网络上使用最为广泛的服务&#xff0c;GitHub能够托管各种Git库。首先我们须要注冊一个GitHub账号&#xff0c;打…

leetcode132. 分割回文串 II(dp)

给你一个字符串 s&#xff0c;请你将 s 分割成一些子串&#xff0c;使每个子串都是回文。 返回符合要求的 最少分割次数 。 示例 1&#xff1a; 输入&#xff1a;s “aab” 输出&#xff1a;1 解释&#xff1a;只需一次分割就可将 s 分割成 [“aa”,“b”] 这样两个回文子串…

数据标准化和离散化

在某些比较和评价的指标处理中经常需要去除数据的单位限制&#xff0c;将其转化为无量纲的纯数值&#xff0c;便于不同单位或量级的指标能够进行比较和加权。因此需要通过一定的方法进行数据标准化&#xff0c;将数据按比例缩放&#xff0c;使之落入一个小的特定区间。 一、标准…

熊猫tv新功能介绍_熊猫简单介绍

熊猫tv新功能介绍Out of all technologies that is introduced in Data Analysis, Pandas is one of the most popular and widely used library.在Data Analysis引入的所有技术中&#xff0c;P andas是最受欢迎和使用最广泛的库之一。 So what are we going to cover :那么我…

关于sublime-text-2的Package Control组件安装方法,自动和手动

之前在自己的文章《Linux下安装以及破解sublim-text-2编辑器》的文章中提到过关于sublime-text-2的Package Control组件安装方法。 当时使用的是粘贴代码&#xff1a; 1import urllib2,os;pfPackage Control.sublime-package;ippsublime.installed_packages_path();os.makedirs…

上海区块链会议演讲ppt_进行第一次会议演讲的完整指南

上海区块链会议演讲pptConferences can be stressful even if you are not giving a talk. On the other hand, speaking can really boost your career, help you network, allow you to travel for (almost) free, and give back to others at the same time.即使您不讲话…

windows下Call to undefined function curl_init() error问题

本地项目中使用到curl_init()时出现Call to undefined function curl_init()的错误&#xff0c;去掉php.ini中的extensionphp_curl.dll前的分号还是不行&#xff0c;phpinfo()中无curl模块&#xff0c;于是上网搜索并实践了如下方法&#xff0c;成功&#xff1a; 在使用php5的c…

数据转换软件_数据转换

数据转换软件&#x1f4c8;Python金融系列 (&#x1f4c8;Python for finance series) Warning: There is no magical formula or Holy Grail here, though a new world might open the door for you.警告 &#xff1a;这里没有神奇的配方或圣杯&#xff0c;尽管新世界可能为您…

leetcode 1047. 删除字符串中的所有相邻重复项(栈)

给出由小写字母组成的字符串 S&#xff0c;重复项删除操作会选择两个相邻且相同的字母&#xff0c;并删除它们。 在 S 上反复执行重复项删除操作&#xff0c;直到无法继续删除。 在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。 示例&#xff1a; 输入&#x…

spring boot: spring Aware的目的是为了让Bean获得Spring容器的服务

Spring Aware的目的是为了让Bean获得Spring容器的服务 //获取容器中的bean名称import org.springframework.beans.factory.BeanNameAware;//获得资源加载器&#xff0c;可以获得额外的资源import org.springframework.context.ResourceLoaderAware; package ch2.aware; import …

10张图带你深入理解Docker容器和镜像

【编者的话】本文用图文并茂的方式介绍了容器、镜像的区别和Docker每个命令后面的技术细节&#xff0c;能够很好的帮助读者深入理解Docker。这篇文章希望能够帮助读者深入理解Docker的命令&#xff0c;还有容器&#xff08;container&#xff09;和镜像&#xff08;image&#…

matlab界area_Matlab的数据科学界

matlab界area意见 (Opinion) My personal interest in Data Science spans back to 2011. I was learning more about Economies and wanted to experiment with some of the ‘classic’ theories and whilst many of them held ground, at a micro level, many were also pur…

javascript异步_JavaScript异步并在循环中等待

javascript异步Basic async and await is simple. Things get a bit more complicated when you try to use await in loops.基本的async和await很简单。 当您尝试在循环中使用await时&#xff0c;事情会变得更加复杂。 In this article, I want to share some gotchas to wat…

白盒测试目录导航

白盒测试目录导航&#xff08;更新中&#xff09; 2017-12-29 [1] 白盒测试&#xff1a;为什么要做白盒测试 [2] 白盒测试&#xff1a;理论基础 [3] 白盒测试实战&#xff08;上&#xff09; [4] 白盒测试实战&#xff08;中&#xff09; [5] 白盒测试实战&#xff08;下&#…

hdf5文件和csv的区别_使用HDF5文件并创建CSV文件

hdf5文件和csv的区别In my last article, I discussed the steps to download NASA data from GES DISC. The data files downloaded are in the HDF5 format. HDF5 is a file format, a technology, that enables the management of very large data collections. Thus, it is…

CSS仿艺龙首页鼠标移入图片放大

CSS仿艺龙首页鼠标移入图片放大&#xff0c;效果似乎没有js好。。。。。。 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>图片放大</title><style>*{padding:0;margin:0;}body{padding-…

leetcode 224. 基本计算器(栈)

给你一个字符串表达式 s &#xff0c;请你实现一个基本计算器来计算并返回它的值。 示例 1&#xff1a; 输入&#xff1a;s “1 1” 输出&#xff1a;2 示例 2&#xff1a; 输入&#xff1a;s " 2-1 2 " 输出&#xff1a;3 示例 3&#xff1a; 输入&#xff…

机械制图国家标准的绘图模板_如何使用p5js构建绘图应用

机械制图国家标准的绘图模板The theme for week #5 of the Weekly Coding Challenge is:每周编码挑战第5周的主题是&#xff1a; 创建绘图应用程序 (Creating a Drawing Application) This is the first application that we are building in the #weeklyCodingChallenge prog…