觀前提醒:
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:
Input: "race a car"
Output: false
Constraints:
s
consists only of printable ASCII characters.
/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function (s) {
const n = s.length;
if (n === 0) {
return true;
}
// 轉小寫
s = s.toLowerCase();
// 取代非文字部分
let newS = "";
for (let i = 0; i < s.length; i++) {
if (s.charCodeAt(i) >= 97 && s.charCodeAt(i) <= 122) {
newS += s[i];
} else if (s.charCodeAt(i) >= 48 && s.charCodeAt(i) <= 57) {
newS += s[i];
}
}
// 反轉
let revNewS = newS.split("").reverse().join("");
if (revNewS === newS) {
return true;
} else {
return false;
}
};
這題簡單說,對我來講就是直接一步步拆解,然後看著"OOO / 481 test cases passed.",那個通過數量上升,來判斷自己的CODE有沒有過這樣~
謝謝大家的收看,LeetCode 小學堂我們下次見~