★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-seurazxy-ch.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given two strings s and t which consist of only lowercase letters.git
String t is generated by random shuffling string s and then add one more letter at a random position.github
Find the letter that was added in t.微信
Example:dom
Input: s = "abcd" t = "abcde" Output: e Explanation: 'e' is the letter that was added.
給定兩個字符串 s 和 t,它們只包含小寫字母。spa
字符串 t 由字符串 s 隨機重排,而後在隨機位置添加一個字母。scala
請找出在 t 中被添加的字母。code
示例:htm
輸入: s = "abcd" t = "abcde" 輸出: e 解釋: 'e' 是那個被添加的字母。
24ms
1 class Solution { 2 func findTheDifference(_ s: String, _ t: String) -> Character { 3 var num_s:UInt32 = getUInt32Value(s) 4 var num_t:UInt32 = getUInt32Value(t) 5 //Int轉Character:Character(UnicodeScalar(number)) 6 return Character(UnicodeScalar(num_t - num_s)!) 7 } 8 //獲取字符串的全部字符的ASCII整形之和 9 func getUInt32Value(_ str:String) -> UInt32 10 { 11 var num:UInt32 = UInt32() 12 //Character轉Int:for asc in String(char).unicodeScalars 13 for asc in str.unicodeScalars 14 { 15 num += asc.value 16 } 17 return num 18 } 19 } 20 //Character擴展代碼 21 extension Character 22 { 23 func toInt() -> Int 24 { 25 var num:Int = Int() 26 for scalar in String(self).unicodeScalars 27 { 28 num = Int(scalar.value) 29 } 30 return num 31 } 32 }
76msblog
1 class Solution { 2 func findTheDifference(_ s: String, _ t: String) -> Character { 3 var num_s:UInt32 = getUInt32Value(s) 4 var num_t:UInt32 = getUInt32Value(t) 5 //Int轉Character:Character(UnicodeScalar(number)) 6 return Character(UnicodeScalar(num_t - num_s)!) 7 } 8 //獲取字符串的全部字符的ASCII整型之和 9 func getUInt32Value(_ str:String) -> UInt32 10 { 11 var num:UInt32 = UInt32() 12 //Character轉Int:for asc in String(char).unicodeScalars 13 for asc in str.unicodeScalars 14 { 15 num += asc.value 16 } 17 return num 18 } 19 } 20 //Character擴展代碼 21 extension Character 22 { 23 func toInt() -> Int 24 { 25 var num:Int = Int() 26 for scalar in String(self).unicodeScalars 27 { 28 num = Int(scalar.value) 29 } 30 return num 31 } 32 }
104ms
1 class Solution { 2 func findTheDifference(_ s: String, _ t: String) -> Character { 3 4 5 var s = s.sorted() 6 var t = t.sorted() 7 for i in 0..<s.count { 8 9 let startIndex = s.index(s.startIndex, offsetBy: i) 10 let endIndex = s.index(s.startIndex, offsetBy: i + 1) 11 let a = String(s[startIndex..<endIndex]) 12 13 14 let startIndexT = t.index(t.startIndex, offsetBy: i) 15 let endIndexT = t.index(t.startIndex, offsetBy: i + 1) 16 let b = String(t[startIndexT..<endIndexT]) 17 18 if a != b { 19 return Character(b) 20 } 21 22 } 23 24 return t.last ?? " " 25 } 26 }