-
Notifications
You must be signed in to change notification settings - Fork 24
/
lee709.js
122 lines (112 loc) · 2.32 KB
/
lee709.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* 实现函数 ToLowerCase(), 该函数接收一个字符串参数 str, 并将该字符串中的大写字母转换成小写字母, 之后返回新的字符串。
*
* 输入: "Hello"
* 输出: "hello"
*/
/**
* @param {string} str
* @return {string}
* 我的提交执行用时
* 已经战胜 81.64 % 的 javascript 提交记录
*/
var toLowerCase = function (str) {
const letterMap = {
A: 'a',
B: 'b',
C: 'c',
D: 'd',
E: 'e',
F: 'f',
G: 'g',
H: 'h',
I: 'i',
J: 'j',
K: 'k',
L: 'l',
M: 'm',
N: 'n',
O: 'o',
P: 'p',
Q: 'q',
R: 'r',
S: 's',
T: 't',
U: 'u',
V: 'v',
W: 'w',
X: 'x',
Y: 'y',
Z: 'z',
};
let lowerCaseLetter = '';
for (let index = 0; index < str.length; index ++) {
const curLetter = str[index];
lowerCaseLetter += letterMap[curLetter] || curLetter;
}
return lowerCaseLetter;
};
const re = toLowerCase('AbCdEfG');
console.log(re);
/**
* leecode 耗时最少解法
* @param {string} str
* @return {string}
*/
let toLowerCase = function (str) {
let letterDictionary = {
A: "a",
B: "b",
C: "c",
D: "d",
E: "e",
F: "f",
G: "g",
H: "h",
I: "i",
J: "j",
K: "k",
L: "l",
M: "m",
N: "n",
O: "o",
P: "p",
Q: "q",
R: "r",
S: "s",
T: "t",
U: "u",
V: "v",
W: "w",
X: "x",
Y: "y",
Z: "z"
};
let result = "";
for (let i = 0, len = str.length; i < len; i += 1) {
result += str[i] in letterDictionary ? letterDictionary[str[i]] : str[i];
}
return result;
};
/**
* @param {string} str
* @return {string}
* other
*/
var toLowerCase = function (str) {
if (str.length === 0) {
return ''
}
let newStr = ''
for (let i = 0; i < str.length; i++) {
let code = str[i].charCodeAt()
// 判断是否ASCII码区间是否为大写字母区间
if (code >= 65 && code <= 90) {
code += 32
newStr += String.fromCharCode(code)
} else {
newStr += str[i]
}
}
return newStr
};