当前位置:网站首页>A series of problems of dp+ backtracking segmentation palindrome string
A series of problems of dp+ backtracking segmentation palindrome string
2022-07-24 03:18:00 【Curry won't throw three points】
class Solution { public int minCut(String s) { int n=s.length(); // For length is n String , use [1,n] Express int []dp=new int[n+1];//dp[i] From s[0...i] Minimum number of divisions for (int i = 1; i <=n; i++) { if(isHuiwen(s.substring(0,i))){ // If it's a palindrome string , Then this paragraph does not need to be divided dp[i]=0; }else { dp[i]=i-1;// First set a maximum number of divisions (i Maximum segmentation of characters i-1 Time ) for (int j = 1; j <=i ; j++) { if(isHuiwen(s.substring(j-1,i))){ // String segmentation is left closed and right open [j,i-1) dp[i]=Math.min(dp[i],dp[j-1]+1); } } } } return dp[n]; } private boolean isHuiwen(String substring) { int start=0; int end=substring.length()-1; while (start<end){ if (substring.charAt(start)!=substring.charAt(end)){ return false; } end--; start++; } return true; } }Optimize : In fact, dynamic programming can also be used to judge palindrome strings
- base case The length is 1 It must be a string
- String is 2, The same two characters are also palindromes
class Solution { public int minCut(String s) { int n=s.length(); int dp[]=new int[n+1]; boolean[][]g=new boolean[n+1][n+1];//g[l][r] Express s[l...r] Is it a palindrome string It also stipulates that the subscript of the first character is 1 for (int r =1; r <=n; r++) { for (int l =r ; l>=1 ; l--) { if(l==r){ g[l][r]=true; // One character is palindrome string }else { if (s.charAt(l-1)==s.charAt(r-1)){ if(r-l==1||g[l+1][r-1]){ g[l][r]=true; // If there are only two characters , The description is palindrome string // perhaps a b b a a==a when ,,bb It's also a palindrome string that abba Palindrome string } } } } } for (int i = 1; i <=n; i++) { if(g[1][i]){ dp[i]=0; // If [1,i] It's a palindrome string , Then there is no need to split }else { dp[i]=i-1; for (int l = 1; l <=n ; l++) { if (g[l][i]){ dp[i]=Math.min(dp[i],dp[l-1]+1); } } } } return dp[n]; } }
边栏推荐
- New definition of mobile communication: R & scmx500 will improve the IP data throughput of 5g devices
- Cannot resolve symbol 'override' of idea clone‘
- 正则表达式 \b \B 深入浅出理解单词边界的匹配
- Advantages, disadvantages and summary of sequence list and linked list
- Regular expression \b \b understand word boundary matching in simple terms
- Daily gossip (I)
- Insist on accompanying study
- String.split()最详细源码解读及注意事项
- Leetcode stack and queue questions
- Test interview questions
猜你喜欢
![Hospital PACS source code PACS ultrasonic Department source code DICOM image workstation source code [source code free sharing]](/img/7d/c309b7b0b919df44b5c1461d79e7e7.png)
Hospital PACS source code PACS ultrasonic Department source code DICOM image workstation source code [source code free sharing]

Lagrange polynomial

JS 数组 isAarray() typeof

Hcip day 9 notes (OSPF routing feedback, routing policy, and Configuration Guide)

Error code 0x80004005

C动态内存管理详解

Lumberyard game engine of o3de

什么是IMU?

实现两个页面之前的通信(使用localStorage)

Bingbing learning notes: basic operation of vim tool
随机推荐
数据湖:开源数据湖方案DeltaLake、Hudi、Iceberg对比分析
[MySQL learning] install and use multiple versions of MySQL, MySQL 8 and MySQL 5.7 at the same time, compressed version
Minimum exchange times
Go log package
OSPF routing control
C动态内存管理详解
正则表达式 \b \B 深入浅出理解单词边界的匹配
FTP服務與配置
CMT 注册——Google Scholar Id,Semantic Scholar Id,和 DBLP Id
AcWing 4499. 画圆 (相似三角形)
Customize the default width and height of kindeditor rich text
Cannot resolve symbol 'override' of idea clone‘
Basic knowledge of trigger (Part 2)
Lumberyard game engine of o3de
Lagrange polynomial
LCD1602 - binge 51
A simple and perfect WPF management system framework source code
Ugui source code analysis - maskutilities
Detailed explanation of wechat official account online customer service access methods and functions
Bingbing learning notes: basic operation of vim tool


