嗨我有一个函数,它连续计算回文数(使用哈希) 然后输入=长字符串我得溢出
class palindrome {
var string: String
var str: [Character]
var prefixHash: [Int64]
var suffixHash: [Int64]
var powArray: [Int64]
var oddCount: [Int]
var evenCount: [Int]
var simpleBase: Int64 { return 21 }
init(string: String) {
self.string = string
self.str = Array(string.characters)
self.prefixHash = [Int64](repeating: 0, count: str.count)
self.suffixHash = [Int64](repeating: 0, count: str.count)
self.powArray = [Int64](repeating: 0, count: str.count)
self.oddCount = [Int](repeating: 0, count: str.count)
self.evenCount = [Int](repeating: 0, count: str.count)
countPow()
}
func countPow() {
powArray[0] = 1
var i = 1
while i < str.count {
powArray[i] = powArray[i-1] &* simpleBase
if powArray[i] < 0 {
powArray[i] *= -1
}
i+=1
}
}
func getHashForSubStr(left: Int, right: Int) -> Int64 {
var result = prefixHash[right]
if left > 0 {
result = result - prefixHash[left-1]
}
return result
}
func getRevHashForSubStr(left: Int, right: Int) -> Int64 {
var result = suffixHash[left]
if right < suffixHash.count - 1 {
result = result - suffixHash[right+1]
}
return result
}
func countPrefixHash() {
prefixHash[0] = str[0].unicodeScalarCodePoint()
var i = 1
while i < str.count {
prefixHash[i] = prefixHash[i-1] &+ (str[i].unicodeScalarCodePoint() &* powArray[i])
if prefixHash[i] < 0 {
prefixHash[i] *= -1
}
i+=1
}
}
func countSuffixHash() {
suffixHash[suffixHash.count-1] = str[str.count-1].unicodeScalarCodePoint()
var i = str.count-2
var x = 1
while i >= 0 {
suffixHash[i] = suffixHash[i+1] &+ (str[i].unicodeScalarCodePoint() &* powArray[x])
if suffixHash[i] < 0 {
suffixHash[i] *= -1
}
x+=1
i-=1
}
}
func isPalindrome(left: Int, right: Int) -> Bool {
var first = getHashForSubStr(left: left, right: right) &* powArray[prefixHash.count - right - 1]
var second = getRevHashForSubStr(left: left, right: right) &* powArray[left]
return first == second
}
func countOdd() {
var i = 0
while i < oddCount.count {
var left = 1
var right = min(i+1, oddCount.count-i)
while left <= right {
let mid = (left+right)/2
if isPalindrome(left: i-mid+1, right: i+mid-1) {
oddCount[i] = mid
left = mid + 1
} else {
right = mid - 1
}
}
i+=1
}
}
func countEven() {
var i = 0
while i < evenCount.count {
var left = 1
var right = min(i, evenCount.count-i)
while left <= right {
let mid = (left+right)/2
if isPalindrome(left: i-mid, right: i+mid-1) {
evenCount[i] = mid
left = mid + 1
} else {
right = mid - 1
}
}
i+=1
}
}
}
func answer(str: String) -> Int {
let test = palindrome(string: str)
let count = str.characters.count
test.powArray
test.countPrefixHash()
test.countSuffixHash()
test.countEven()
test.countOdd()
let evenArray = test.evenCount
let oddArray = test.oddCount
var even = 0
var odd = 0
test.prefixHash
test.suffixHash
for i in evenArray {
even+=i
}
for i in oddArray {
odd+=i
}
return (even+odd)-count
}
answer(str: "abbbadadfdaflpfewlpwflp")
我尝试使用运算符&amp; *&amp; - &amp; +和 我摆脱了问题,但现在它的工作不正确 是否有可能避免其他事情 或者我没有正确使用swift
答案 0 :(得分:0)
我尝试使用Int64(-x到x),但必须是UInt64((x到x)没有负数)