为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode709. 转换成小写字母 | To Lower Case

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

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. 

Example 1:

Input: "Hello"
Output: "hello"

Example 2:

Input: "here"
Output: "here"

Example 3:

Input: "LOVELY"
Output: "lovely"

实现函数 ToLowerCase(),该函数接收一个字符串参数 str,并将该字符串中的大写字母转换成小写字母,之后返回新的字符串。 

示例 1:

输入: "Hello"
输出: "hello"

示例 2:

输入: "here"
输出: "here"

示例 3:

输入: "LOVELY"
输出: "lovely"

Runtime: 4 ms
Memory Usage: 19.6 MB
 1 class Solution {
 2     func toLowerCase(_ str: String) -> String {
 3         return String(str.unicodeScalars.map { (s) -> Character in
 4             if s.value >= 65 && s.value <= 90 {
 5                 return Character(UnicodeScalar(s.value + 32)!)
 6             }
 7             return Character(UnicodeScalar(s))
 8         })
 9     }
10 }

Runtime: 4 ms
Memory Usage: 19.8 MB
 1 class Solution {
 2     func toLowerCase(_ str: String) -> String {
 3         var arr:[Character] = Array(str)
 4         for i in 0..<arr.count
 5         {
 6             var char:Character = arr[i]
 7             if char >= "A" && char <= "Z"
 8             {
 9                 arr[i] = (char.ascii + 32).ASCII
10             }
11         }
12         return String(arr)
13     }
14 }
15 
16 //Character扩展 
17 extension Character  
18 {  
19   //Character转ASCII整数值(定义小写为整数值)
20    var ascii: Int {
21        get {
22            return Int(self.unicodeScalars.first?.value ?? 0)
23        }       
24     }    
25 }
26 
27 //Int扩展
28 extension Int
29 {
30     //Int转Character,ASCII值(定义大写为字符值)
31     var ASCII:Character 
32     {
33         get {return Character(UnicodeScalar(self)!)}
34     }
35 }

4ms

 1 class Solution {
 2     func ord(ch: String) -> Int? {
 3     for st in ch.utf16 {
 4         return Int(st)
 5     }
 6     return nil
 7 }
 8 
 9 func ord(val: UInt16) -> Character? {
10     if let us  = UnicodeScalar(val){
11         return Character(us)
12     }
13     return nil
14 }
15     
16     func toLowerCase(_ str: String) -> String {
17     guard str.count > 0 else {
18         return ""
19     }
20     var s = Array(str)
21     for ch in s.enumerated() {
22         if var ordVal = ord(ch: String(ch.element)), ordVal >= 65 && ordVal <= 90 {
23             ordVal = ordVal + 32
24             if let modVal = ord(val: UInt16(ordVal)) {
25                 s[ch.offset] = modVal
26             }
27         }
28     }
29     return String(s)
30     }
31 }

19040kb

 1 class Solution {
 2     
 3     func toLowerCase(_ str: String) -> String {
 4         var word = String()
 5         for i in str.utf8{
 6             var i = i
 7             if i >= 65 && i <= 90 {
 8                 word.append(Character(UnicodeScalar(i + 32)))
 9             }
10             else {
11                 word.append(Character(UnicodeScalar(i)))
12             }
13         }
14         return word
15     }
16 }

19112kb

 1 class Solution {
 2     func toLowerCase(_ str: String) -> String {
 3         return String(str.utf8.map({ (ascii) -> Character in
 4             var charCode = Int(ascii)
 5             if 65 <= charCode && charCode <= 90 {
 6                 charCode += 32
 7             }
 8             return Character(UnicodeScalar(charCode)!)
 9         }))
10     }
11 }

 

posted @ 2019-03-10 19:03  为敢技术  阅读(368)  评论(0编辑  收藏  举报