日月换新天。为有牺牲多壮志,敢教

[Swift]LeetCode132. 分割回文串 II | Palindrome Partitioning II

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9963223.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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.

Example:

Input: "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.

给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。

返回符合要求的最少分割次数。

示例:

输入: "aab"
输出: 1
解释: 进行一次分割就可将 s 分割成 ["aa","b"] 这样两个回文子串。

148ms
复制代码
 1 class Solution {
 2     func minCut(_ s: String) -> Int {
 3         var isPalindrome = Array(repeating: Array(repeating: false, count: s.count), count: s.count)
 4         var res = Array(repeating: Int.max, count: s.count)
 5         let sArray = Array(s)
 6         
 7         for i in 0..<s.count {
 8             for j in 0..<i + 1 {
 9                 if sArray[j] == sArray[i] && (i - j < 2 || isPalindrome[j + 1][i - 1]) {
10                     res[i] = j == 0 ? 0 : min(res[i], res[j - 1] + 1)
11                     isPalindrome[j][i] = true
12                 }
13             }
14         }
15         
16         return res[res.count - 1]
17     }
18 }
复制代码

148ms

复制代码
 1 class Solution {
 2     func minCut(_ s: String) -> Int {
 3         if s.isEmpty {
 4             return 0
 5         }
 6         
 7         let count = s.count
 8         var nums = Array(repeating: 0, count: count+1)
 9         for i in 0...count {
10             nums[i] = count - i - 1
11         }
12         var p = Array(repeating: Array(repeating: false, count: count), count: count)
13         let sArr = Array(s)
14         
15         for i in stride(from: count-1, to: -1, by: -1) {
16             for j in i..<count {
17                 if sArr[i] == sArr[j] && (j-i<=1 || p[i+1][j-1]) {
18                     p[i][j] = true
19                     nums[i] = min(nums[i], nums[j+1] + 1)
20                 }
21             }
22         }
23         
24         
25         return nums[0]
26     }
27 }
复制代码

156ms

复制代码
 1 class Solution {
 2     func minCut(_ s: String) -> Int {
 3 
 4         var n = s.count
 5         guard n > 1 else { return 0 }
 6         let s = Array(s)
 7         var p = Array(repeating: Array(repeating: false, count: n), count: n )
 8         var dp = [Int]()
 9         for i in 0...n { dp.append(n-i-1) }
10         for i in (0...n-1).reversed() {
11             for j in i...n-1 {
12                 if s[i] == s[j], (j-i <= 1 || p[i+1][j-1]) {
13                     p[i][j] = true
14                     dp[i] = min(dp[i], dp[j+1]+1)
15                 }
16             }
17         }
18         return dp[0]
19     }
20 }
复制代码

172ms

复制代码
 1 class Solution {
 2     func minCut(_ s: String) -> Int {
 3         var res = [Int](repeating: Int.max, count: s.count)
 4         var isPalindrome = Array(repeating: Array(repeating: false, count: s.count), count: s.count)
 5         let sArray = Array(s)
 6         
 7         for i in 0..<s.count {
 8             for j in 0..<i + 1 {
 9                 if sArray[i] == sArray[j] && (i - j < 2 || isPalindrome[j + 1][i - 1]) {
10                     res[i] = j == 0 ? 0 : min(res[i], res[j - 1] + 1)
11                     isPalindrome[j][i] = true
12                 }
13             }
14         }
15         
16         return res[res.count - 1]
17     }
18 }
复制代码

 

posted @   为敢技术  阅读(306)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°