★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-uanyipqh-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. git
Example 1:github
Input: "Hello"
Output: "hello"
Example 2:微信
Input: "here"
Output: "here"
Example 3:app
Input: "LOVELY"
Output: "lovely"
實現函數 ToLowerCase(),該函數接收一個字符串參數 str,並將該字符串中的大寫字母轉換成小寫字母,以後返回新的字符串。 函數
示例 1:spa
輸入: "Hello" 輸出: "hello"
示例 2:code
輸入: "here" 輸出: "here"
示例 3:htm
輸入: "LOVELY" 輸出: "lovely"
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 }
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 }
4msblog
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 }