当前位置:网站首页>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);
}
}
}
}
边栏推荐
- 2020-12-04
- SqlServer 复制表的自增属性
- ArcGIS application (20) the ArcGIS grid image symbol system prompts "this dataset does not have valid histogram required for classificati..."
- 2021-04-14
- China Mobile's mobile phone users grow slowly, but strive for high profit 5g package users
- How to manage tasks in note taking software such as flowus and notation?
- One case of SQL performance degradation caused by modifying implicit parameters
- A spark app demo
- Use full function simulation design method
- 14. 最长公共前缀
猜你喜欢

c# sqlsugar,hisql,freesql orm框架全方位性能测试对比 sqlserver 性能测试

保证数据库和缓存的一致性

Do domestic mobile phones turn apples? It turned out that it was realized by 100 yuan machine and sharp price reduction

Reasons for the failure of digital transformation and the way to success
Learn redis with you (11) -- redis distributed lock

leetcode. 11 --- container with the most water

安装typescript环境并开启VSCode自动监视编译ts文件为js文件

【STM32技巧】使用STM32 HAL库的硬件I2C驱动RX8025T实时时钟芯片

Is it bad for NFT that the market starts to cool down?

2020-12-04
随机推荐
MySQL multi table operation exercise
Dragon City in Europe | National Geographic the most romantic and safe destination in the world
2020-12-04
2021-07-27
Learn redis with you (11) -- redis distributed lock
Experiment 4 operation comparison between NoSQL and relational database
Greedy distribution problem (1)
Palindromes (simple version)
Relationship between adau1452 development system interface and code data
R language data Table data import practice: data Rename the name of the table data column (rename)
Practice brings true knowledge: the strongest seckill system architecture in the whole network is decrypted. Not all seckills are seckills!!
mysql主从同步及其分库分表基本流程
别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
Core and semiconductor "RF eda/ filter design platform" shines ims2022
2020-12-04
获取鼠标移动的方向
2021-07-27
Valid parentheses
Phantomjs实用代码段(持续更新中……)
2021-05-02