「算法」709. 转换成小写字母

<p/><div class="image-package"><img src="https://upload-images.jianshu.io/upload_images/1648392-a9d4ecf30e2572e4.jpg" contenteditable="false" img-data="{"format":"jpeg","size":40866,"height":900,"width":1600}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><p>

</p><blockquote><p>给你一个字符串 s ,将该字符串中的大写字母转换成相同的小写字母,返回新的字符串。

示例 1:

输入:s = "Hello"
输出:"hello"
示例 2:

输入:s = "here"
输出:"here"
示例 3:

输入:s = "LOVELY"
输出:"lovely"


提示:

1 <= s.length <= 100
s 由 ASCII 字符集中的可打印字符组成

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/to-lower-case
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。</p></blockquote><p>
</p><h1 id="n4c5f">题解</h1><div class="image-package"><img src="https://upload-images.jianshu.io/upload_images/1648392-58e4e922907880c1.jpg" contenteditable="false" img-data="{"format":"jpeg","size":70133,"height":824,"width":1372}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><h2 id="x7nb1">Swift</h2><p>方法一</p><blockquote><p>class Solution {
func toLowerCase(_ s: String) -> String {
return s.lowercased()
}
}
</p></blockquote><p>
</p><p>方法二</p><blockquote><p>class Solution {
func toLowerCase(_ s: String) -> String {
var result = ""

for c in s {
if c.asciiValue! > 64, c.asciiValue! < 91 {
result.append(Character(Unicode.Scalar(c.asciiValue! + 32)))

} else {
result.append(c)
}
}

return result
}
}

print(Solution().toLowerCase("Hello"))
</p></blockquote><p>
</p><h2 id="69mil">Dart</h2><blockquote><p>class Solution {
String toLowerCase(String s) {
var result = <int>[];

List<int> ascs = s.codeUnits;

for (var c in ascs) {
if (c > 64 && c < 91) {
result.add(c + 32);
} else {
result.add(c);
}
}

return String.fromCharCodes(result);
}
}

void main() {
print(Solution().toLowerCase("Hello"));
}
</p></blockquote><p>
</p><p>
</p><p>
</p><p>
</p>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容