diff --git a/src/string/125-valid-palindrome/index.js b/src/string/125-valid-palindrome/index.js index c3d6195..04a37da 100644 --- a/src/string/125-valid-palindrome/index.js +++ b/src/string/125-valid-palindrome/index.js @@ -22,6 +22,8 @@ * s consists only of printable ASCII characters. * * https://leetcode.com/problems/valid-palindrome/ + * + * @related 680-valid-palindrome-ii */ /** @@ -32,10 +34,8 @@ * 3. 指定空字符串为回文字符串 * * 思路: - * 1. 大小写统一,用 .toLowerCase 处理 - * 2. 正则匹配,筛选出由字母和数字所组成的字符串 - * 3. 字符串反转,.split + .reverse + .join 方法 - * 4. 将反转字符串与原字符串作比较得出结果 + * 1. 统一大小写,正则匹配出字符串中的字母和数字,得到目标字符串 + * 2. 将目标字符串反转,拿反转后的字符串与目标字符串作比较得出是否是回文字符串 * * Time Complexity: O(n) = 遍历次数 * Space Complexity: O(n) = 创建的数组长度 / 创建的字符串长度 diff --git a/src/string/125-valid-palindrome/optimize.js b/src/string/125-valid-palindrome/optimize.js index 15b79ce..6051cd9 100644 --- a/src/string/125-valid-palindrome/optimize.js +++ b/src/string/125-valid-palindrome/optimize.js @@ -22,19 +22,19 @@ * s consists only of printable ASCII characters. * * https://leetcode.com/problems/valid-palindrome/ + * + * @related 680-valid-palindrome-ii */ /** * 输入一个字符串,判断它是否是回文字符串 * 注意: - * 1. 只考虑字幕和数字 - * 2. 忽略字母的大小写 - * 3. 指定空字符串为回文字符串 + * 1. 只考虑数字和字母,忽略字母的大小写 + * 2. 定义空字符串为回文字符串 * * 思路: - * 1. 统一大小写,.toLowerCase - * 2. 正则匹配筛选出字母和数字组成的字符串 - * 3. 遍历筛选后的字符串,利用 s[i] === s[len - i - 1] 来判断是否是回文字符串 + * 1. 统一大小写,正则匹配出字符串中的字母和数字,得到目标字符串 + * 2. 遍历目标字符串,判断 s[i] === s[s.length - i - 1] 来得出是否是回文字符串 * * Time Complexity: O(n) = 遍历次数 * Space Complexity: O(n) = 创建的字符串长度 diff --git a/src/string/125-valid-palindrome/optimize2.js b/src/string/125-valid-palindrome/optimize2.js index 5d5bfbe..036fd2d 100644 --- a/src/string/125-valid-palindrome/optimize2.js +++ b/src/string/125-valid-palindrome/optimize2.js @@ -22,6 +22,8 @@ * s consists only of printable ASCII characters. * * https://leetcode.com/problems/valid-palindrome/ + * + * @related 680-valid-palindrome-ii */ /** @@ -34,8 +36,8 @@ * 思路: * Two Pointers 双指针,原地做比较,不创建新的字符串、数组 * 1. 创建 left right 两个下标,分别代表要比较的前后对称两个值的下标 - * 2. 遍历字符串 s,以此判断两个下标对应的值是否是字母或数字,如果不是则把对应下标向前/向后移动,直到为字母或数字值为止 - * 3. 依次判断 left right 对应的值 + * 2. 遍历字符串 s,依次判断两个下标对应的值是否是字母或数字,如果不是则把对应下标向前/向后移动,直到值为字母或数字为止 + * 3. 依次判断 s[left].toLowerCase === s[right].toLowerCase() * * Time Complexity: O(n) = 遍历次数 * Space Complexity: O(1) diff --git a/src/string/125-valid-palindrome/template-zh.js b/src/string/125-valid-palindrome/template-zh.js index 82e493a..e516131 100644 --- a/src/string/125-valid-palindrome/template-zh.js +++ b/src/string/125-valid-palindrome/template-zh.js @@ -22,6 +22,8 @@ * s consists only of printable ASCII characters. * * https://leetcode.cn/problems/valid-palindrome/ + * + * @related 680-valid-palindrome-ii */ module.exports = isPalindrome; diff --git a/src/string/125-valid-palindrome/template.js b/src/string/125-valid-palindrome/template.js index 39ee3a1..e15409b 100644 --- a/src/string/125-valid-palindrome/template.js +++ b/src/string/125-valid-palindrome/template.js @@ -22,6 +22,8 @@ * s consists only of printable ASCII characters. * * https://leetcode.com/problems/valid-palindrome/ + * + * @related 680-valid-palindrome-ii */ module.exports = isPalindrome; diff --git a/src/greedy/680-valid-palindrome-ii/index.js b/src/string/680-valid-palindrome-ii/index.js similarity index 60% rename from src/greedy/680-valid-palindrome-ii/index.js rename to src/string/680-valid-palindrome-ii/index.js index 17d178b..c0ef7ba 100644 --- a/src/greedy/680-valid-palindrome-ii/index.js +++ b/src/string/680-valid-palindrome-ii/index.js @@ -21,14 +21,24 @@ * s consists of lowercase English letters. * * https://leetcode.com/problems/valid-palindrome-ii/ + * + * @related 125-valid-palindrome */ + /** - * Greedy Algorithm + * 给定一个非空字符串 s,最多删除一个字符,判断能否成为回文字符串 + * + * 思路: + * 贪心算法 Greedy Algorithm + * 定义 left right 两个初始值为头尾的指针,依次判断 s[left] === s[right] + * 如果相同则 left += 1 right -= 1 + * 如果不同则分别判断去除 s[left] 和 s[right] 后的子字符串是否是回文字符串 * - * Time Complexity: O(n) = while 循环次数 / isPalindrome 操作 - * Space complexity: O(n) - * Auxiliary complexity: O(n) + * Time Complexity: O(n) = 遍历次数 + * Space complexity: O(1) + * Auxiliary complexity: O(1) + * 其中 n 是字符串 s 的长度 * * @param {string} s * @returns {boolean} @@ -39,7 +49,7 @@ function validPalindrome(s) { while (left < right) { if (s[left] !== s[right]) { - return isPalindrome(s, left + 1, right) || isPalindrome(s, left, right - 1); + return isPalindrome(left + 1, right, s) || isPalindrome(left, right - 1, s); } left += 1; right -= 1; @@ -49,16 +59,17 @@ function validPalindrome(s) { } /** - * Time Complexity: O(n) = while 循环次数 + * Time Complexity: O(n) = 遍历次数 * Space complexity: O(1) * Auxiliary complexity: O(1) + * 其中 n 是字符串 s 的长度 * * @param {string} s * @param {left} left * @param {right} right * @returns {boolean} */ -function isPalindrome(s, left, right) { +function isPalindrome(left, right, s) { while (left < right) { if (s[left] !== s[right]) { return false; diff --git a/src/greedy/680-valid-palindrome-ii/template-zh.js b/src/string/680-valid-palindrome-ii/template-zh.js similarity index 93% rename from src/greedy/680-valid-palindrome-ii/template-zh.js rename to src/string/680-valid-palindrome-ii/template-zh.js index 06b07a6..2f8ead2 100644 --- a/src/greedy/680-valid-palindrome-ii/template-zh.js +++ b/src/string/680-valid-palindrome-ii/template-zh.js @@ -21,6 +21,8 @@ * s consists of lowercase English letters. * * https://leetcode.cn/problems/valid-palindrome-ii/ + * + * @related 125-valid-palindrome */ module.exports = validPalindrome; diff --git a/src/greedy/680-valid-palindrome-ii/template.js b/src/string/680-valid-palindrome-ii/template.js similarity index 93% rename from src/greedy/680-valid-palindrome-ii/template.js rename to src/string/680-valid-palindrome-ii/template.js index 5ee1a14..50c5646 100644 --- a/src/greedy/680-valid-palindrome-ii/template.js +++ b/src/string/680-valid-palindrome-ii/template.js @@ -21,6 +21,8 @@ * s consists of lowercase English letters. * * https://leetcode.com/problems/valid-palindrome-ii/ + * + * @related 125-valid-palindrome */ module.exports = validPalindrome; diff --git a/src/greedy/680-valid-palindrome-ii/test.js b/src/string/680-valid-palindrome-ii/test.js similarity index 100% rename from src/greedy/680-valid-palindrome-ii/test.js rename to src/string/680-valid-palindrome-ii/test.js