当前位置:网站首页>LeetCode_ Backtracking_ Dynamic programming_ Medium_ 131. split palindrome string
LeetCode_ Backtracking_ Dynamic programming_ Medium_ 131. split palindrome string
2022-06-22 23:16:00 【I've been up and down in the Jianghu】
1. subject
Give you a string s, Would you please s Split into some substrings , So that each substring is Palindrome string . return s All possible segmentation schemes .
Palindrome string It's the same string read forward and backward .
Example 1:
Input :s = “aab”
Output :[[“a”,“a”,“b”],[“aa”,“b”]]
Example 2:
Input :s = “a”
Output :[[“a”]]
Tips :
1 <= s.length <= 16
s It's only made up of lowercase letters
source : Power button (LeetCode)
link :https://leetcode.cn/problems/palindrome-partitioning
2. Ideas
(1) to flash back _ Dynamic programming
Train of thought reference Official solution to this problem .
3. Code implementation (Java)
// Ideas 1———— to flash back _ Dynamic programming
class Solution {
//dp[i][j] Express s[i...j] Whether it is palindrome string
boolean[][] dp;
//res Save the final result
List<List<String>> res = new ArrayList<>();
//path Save qualified results in the backtracking process
List<String> path = new ArrayList<>();
// String length
int length;
public List<List<String>> partition(String s) {
length = s.length();
dp = new boolean[length][length];
for (int i = 0; i < length; i++) {
Arrays.fill(dp[i], true);
}
for (int i = length - 1; i >= 0; i--) {
for (int j = i + 1; j < length; j++) {
dp[i][j] = (s.charAt(i) == s.charAt(j) && dp[i + 1][j - 1]);
}
}
backtrace(s, 0);
return res;
}
public void backtrace(String s, int i) {
if (i == length) {
res.add(new ArrayList<String>(path));
return;
}
for (int j = i; j < length; j++) {
if (dp[i][j]) {
path.add(s.substring(i, j + 1));
backtrace(s, j + 1);
path.remove(path.size() - 1);
}
}
}
}
边栏推荐
- Enabling partners, major guarantee of Spring Festival "non-stop"
- A SQL optimization case using order by and rownum
- The relationship between derivative and differential of function
- MySQL constraints
- SSH method 2 for adding node nodes in Jenkins
- Mysql8 installation and environment configuration
- Lua -- iterator, module, meta table
- Relationship between adau1452 development system interface and code data
- Is it bad for NFT that the market starts to cool down?
- Spark RDD Programming Guide(2.4.3)
猜你喜欢
Mysql8 installation and environment configuration

How to quickly build an enterprise knowledge base at low cost?

2021-03-06

2021-08-26

【Kubernetes 系列】Kubernetes 概述

The relationship between derivative and differential of function

2021-04-14

Introduction and example application of PostgreSQL string separator function (regexp\u split\u to\u table)

ArcGIS application (20) the ArcGIS grid image symbol system prompts "this dataset does not have valid histogram required for classificati..."

安装typescript环境并开启VSCode自动监视编译ts文件为js文件
随机推荐
js判断浏览器是否打开了控制台
China Mobile's mobile phone users grow slowly, but strive for high profit 5g package users
Greedy interval problem (1)
MySQL functions
Plan and change of continuous repair
2021-04-14
Introduction and example application of PostgreSQL string separator function (regexp\u split\u to\u table)
Mysql8 installation and environment configuration
13. 罗马数字转整数
2020-12-20
From 11 hours to 25 seconds -- is there room for optimization?
Spark SQL Start(2.4.3)
Ensure database and cache consistency
The relationship between derivative and differential of function
一个spark app demo
SQL performance optimization method for interval retrieval
程序员接私活兼职选择
c语言---17 函数简介
Mysql database DQL exercise
How to improve work efficiency? Macintosh efficiency tool set