709.转换成小写字母
链接:709.转换成小写字母
难度:Easy
标签:字符串
简介:实现函数 ToLowerCase(),该函数接收一个字符串参数 str,并将该字符串中的大写字母转换成小写字母,之后返回新的字符串。
题解 1 - typescript
- 编辑时间:2021-12-12
- 执行用时:776ms
- 内存消耗:39.4MB
- 编程语言:typescript
- 解法介绍:转换 ascii。
function toLowerCase(s: string): string {
let ans = '';
for (let i = 0, n = s.length; i < n; i++) {
const num = s.codePointAt(i)!;
if (num >= 65 && num <= 90) ans += String.fromCodePoint(num + 32);
else ans += s[i];
}
return ans;
}
题解 2 - typescript
- 编辑时间:2021-12-12
- 执行用时:68ms
- 内存消耗:39.5MB
- 编程语言:typescript
- 解法介绍:调用内置函数。
function toLowerCase(s: string): string {
return s.toLowerCase();
}
题解 3 - java
- 编辑时间:2020-02-17
- 内存消耗:40.8MB
- 编程语言:java
- 解法介绍:根据 asc 码如果是大写字母则改变成小写字母。
class Solution {
public String toLowerCase(String str) {
for (int i = 0, len = str.length(); i < len; i++) {
char c = str.charAt(i);
if (c >= 65 && c <= 90) {
char newCh = (char) (c + 32);
str=str.replace(c, newCh);
}
}
return str;
}
}