Swift 力扣第五题:给定一个字符串 s,找到 s 中最长的回

2020-04-26  本文已影响0人  码农淏

给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。

示例 1:

输入: "babad"
输出: "bab"
注意: "aba" 也是一个有效答案。
示例 2:

输入: "cbbd"
输出: "bb"

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-palindromic-substring
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

一次暴力解答:

class Solution {
    func longestPalindrome(_ s: String) -> String {

        
        guard s.isEmpty == false && s.count > 1 else { return s }
        let array: [Character] = s.map { return $0 }
        let count = s.count
        var result: String = String(array.first!)
        var tempString = result
        
        func lastC(_ index: Int) -> String {
            if index > 0 {
                return String(array[index - 1])
            }
            return ""
        }
        func nextC(_ index: Int) -> String {
            if index + 1 < count {
                return String(array[index + 1])
            }
            return ""
        }
      
        func fixTempString(_ index: Int, lastIndex: Int, nextIndex: Int) {
            
            let current = String(array[index])
            var lastIndex = lastIndex
            var nextIndex = nextIndex
            var lastS = lastC(lastIndex + 1)
            var nextS = nextC(nextIndex - 1)
            var isSame = current == String(array[index + 1])
            
            while lastS.isEmpty == false && lastS == nextS {
                if isSame {
                    isSame = nextS == current
                }
                tempString = lastS + tempString + nextS
                lastS = lastC(lastIndex)
                nextS = nextC(nextIndex)
                lastIndex -= 1
                nextIndex += 1
            }
            if isSame {
                while nextS == current {
                    tempString += nextS
                    nextS = nextC(nextIndex)
                    nextIndex += 1
                }

                while lastS.isEmpty == false && lastS == nextS {
                    tempString = lastS + tempString + nextS
                    lastS = lastC(lastIndex)
                    nextS = nextC(nextIndex)
                    lastIndex -= 1
                    nextIndex += 1
                }
            }
        }
        
        var i = 0
        func fixResult() {
            if result.count < tempString.count {
                result = tempString
            }
            tempString = String(array[i + 1])
        }
        
        
        while i < count {
            defer { i += 1 }

            let last = lastC(i)
            let next = nextC(i)
            if last == tempString && next == tempString {
                fixTempString(i, lastIndex: i - 1, nextIndex: i + 1)
                fixResult()
            } else if last == next {
                tempString = last + tempString + next
                fixTempString(i, lastIndex: i - 2, nextIndex: i + 2)
                fixResult()
            } else if tempString == next {
                tempString += next
                fixTempString(i, lastIndex: i - 1, nextIndex: i + 2)
                fixResult()
            } else {
                tempString = next
            }

        }

        return result
    }
}
image.png

内存还是很满意的,时间复杂度就 emmmmm

上一篇 下一篇

猜你喜欢

热点阅读