• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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

进入博主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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift-搜索条(UISearchBar)的用法发布时间:2022-07-13
下一篇:
[Swift]LeetCode866.回文素数|PrimePalindrome发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap