LeetCode 132. Palindrome Partitioning II 时间复杂度(O(n^2))

时间复杂度(O(n^2)),思想:动态规划

class Solution:
    def minCut(self, s: str) -> int:
        def huiwen_pos(s, i, r):
            extent = 1 - r
            while (i + extent < len(s) and i - extent - r >= 0 and s[i - extent - r] == s[i + extent]):
                extent += 1
            return i - extent + 1 - r, i + extent
        record_path = [i for i in range(len(s) + 1)]
        for index in range(len(s)):
            for r in [0, 1]:
                start, end = huiwen_pos(s, index, r)
                while start < end and record_path[start] + 1 < record_path[end]:
                    record_path[end] = record_path[start] + 1
                    start += 1
                    end -= 1
        return record_path[-1] - 1

猜你喜欢

转载自blog.csdn.net/ziyue246/article/details/88844678
今日推荐