博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Swift]LeetCode409. 最长回文串 | Longest Palindrome
阅读量:5360 次
发布时间:2019-06-15

本文共 4989 字,大约阅读时间需要 16 分钟。

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.

This is case sensitive, for example "Aa" is not considered a palindrome here.

Note:

Assume the length of given string will not exceed 1,010.

Example:

Input:"abccccdd"Output:7 Explanation:One longest palindrome that can be built is "dccaccd", whose length is 7.

给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。

在构造过程中,请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。

注意:

假设字符串的长度不会超过 1010。

示例 1:

输入:"abccccdd"输出:7解释:我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。

20ms
1 class Solution { 2     func longestPalindrome(_ s: String) -> Int { 3         // 4         var arr:[Int] = Array(repeating: 0,count: 128) 5         //遍历字符串 6         for char in s.characters 7         { 8             arr[char.toInt()] += 1 9         }10         var ans:Int = 011         //遍历数组12         for i in 0..<12813         {14             ans += arr[i] / 2 * 215             if ans % 2 == 0 && arr[i] % 2 == 116             {17                 ans += 118             }19         }20         return ans21     }22 }23     //Character扩展代码  24     extension Character  25     {  26         func toInt() -> Int  27         {  28             var num:Int = Int()29             for scalar in String(self).unicodeScalars  30             {  31                 num = Int(scalar.value)  32             }  33             return num  34         }  35     }

28ms

1 class Solution { 2     func longestPalindrome(_ s: String) -> Int { 3         var map: [Bool] = Array(repeating: false, count: 128) 4         var length = 0 5         for char in s.unicodeScalars { 6             let value = Int(char.value) 7             map[value] = !map[value] 8             if !map[value] { 9                 length += 210             }11         }12 13         if length < s.count {14             length += 115         }16 17         return length18     }19 }

28ms

1 class Solution { 2     struct CharTracker { 3             let c: Character 4             let n: Int 5         } 6     func longestPalindrome(_ s: String) -> Int { 7         var charTracker: [CharTracker] = [CharTracker]() 8         var pSum = 0 9         var firstSwitch: Bool = true10         var cSet = [Character : Int]()11         s.forEach {12             cSet[$0, default: 0] += 113         }14         for (_, n) in cSet {15             if (n & 1) == 0 {16                 pSum += n17             } else {18                 pSum += n - 119                 if (firstSwitch) {20                     pSum += 121                     firstSwitch = false22                 }23             }24         }25         return pSum26     }27 }

32ms

1 class Solution { 2     func longestPalindrome(_ s: String) -> Int { 3         var frequencyTable = [Character:Int]() 4         for ch in s { 5             frequencyTable[ch] = (frequencyTable[ch] ?? 0) + 1 6         } 7          8         var count = 0 9         for (key,value) in frequencyTable {10             if value%2 == 1 {11                 count += (value-1)12                 frequencyTable[key] = 113             } else {14                 count += value15                 frequencyTable[key] = 016             }17         }18         19         for (_,value) in frequencyTable {20             if value%2 == 1 {21                 count += 122                 break23             }24         }25         26         return count27     }28 }

36ms

1 class Solution { 2     func longestPalindrome(_ s: String) -> Int { 3         var longestPalindrome = 0 4         var singleChar = 0 5      6         // check that the string is not empty 7         guard s.count > 0 else { return 0 } 8          9         // if there is only 1 character, return 110         if s.count == 1 {11             return 112         }13     14         // there can be 1 odd character included15         // the rest can only be included in multiples of 216         // create a hashmap to keep track of the number of occurances17         var characters = [Character: Int]()18         for char in s {19             if let count = characters[char] {20                 characters[char] = count + 121             } else {22                 characters[char] = 123             }24         }25     26         // if there's a 1 or modulo > 0, set a variable to 127         // loop through the characters and divide by 228         for key in characters.keys {29             if characters[key]! % 2 != 0 {30                 singleChar = 131             }32         33             longestPalindrome += characters[key]! / 2 * 234             // divide by 2 to eliminate remainders35         }36     37         longestPalindrome += singleChar38     39         return longestPalindrome40     }41 }

 

转载于:https://www.cnblogs.com/strengthen/p/9782912.html

你可能感兴趣的文章
关于标签之间因为换行等问题造成的空白间距问题处理
查看>>
hdu 2767(tarjan)
查看>>
sklearn之分类模型混淆矩阵和分类报告
查看>>
MySQL各存储引擎
查看>>
项目--简单导出CSV文件
查看>>
Oracle session相关数据字典(一)
查看>>
织梦文章内容提取第一张或者多张图片输出
查看>>
C#用正则表达式 获取网页源代码标签的属性或值
查看>>
BZOJ 3399 [Usaco2009 Mar]Sand Castle城堡(贪心)
查看>>
WCF(一) 简单的认知
查看>>
[MFC][DShow]简单例子
查看>>
Luogu P1141 01迷宫【搜索/dfs】By cellur925
查看>>
js onclick事件传参
查看>>
WiCloud 商业Wi-Fi管理平台
查看>>
团队项目--未完待续
查看>>
双重标准,我该怎么解决
查看>>
python中的网页标签等字符处理
查看>>
Mybatis输入类型和结果类型
查看>>
Linux常用命令(五)
查看>>
Linux常用命令(四)
查看>>