zoukankan      html  css  js  c++  java
  • [Swift]LeetCode389. 找不同 | Find the Difference

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

    Given two strings s and t which consist of only lowercase letters.

    String t is generated by random shuffling string s and then add one more letter at a random position.

    Find the letter that was added in t.

    Example:

    Input:
    s = "abcd"
    t = "abcde"
    
    Output:
    e
    
    Explanation:
    'e' is the letter that was added.

    给定两个字符串 s 和 t,它们只包含小写字母。

    字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。

    请找出在 t 中被添加的字母。

    示例:

    输入:
    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     } 

    76ms

     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 }
  • 相关阅读:
    一款非常好用的范围滑动插件
    设置滚动条样式
    Qml 定义 constant
    qml 中 使用 shader
    Qt ImageProvider 的使用
    qt 汉化 国际化
    qt rcc 使用
    CentOS7/RHEL7 pacemaker+corosync高可用集群搭建
    Ubunt平台Qt出现:-1: error: cannot find -lgl
    排序-堆排序
  • 原文地址:https://www.cnblogs.com/strengthen/p/9778723.html
Copyright © 2011-2022 走看看