diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/README.md" index 1ab9aa2ce53de..ea1e2b8cad1a0 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/README.md" @@ -41,6 +41,12 @@ +**方法一:双指针** + +我们定义两个指针 $i$ 和 $j$,初始时分别指向字符串的首尾位置,每次判断两个指针指向的字符是否为数字或字母,如果两个指针指向的字符都为数字或字母时,判断两个指针指向的字符是否相同(忽略大小写),如果不相同则返回 `false`,否则将两个指针向中间移动一位,直到两个指针相遇时返回 `true`。 + +时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。 + ### **Python3** @@ -58,8 +64,7 @@ class Solution: j -= 1 if s[i].lower() != s[j].lower(): return False - i += 1 - j -= 1 + i, j = i + 1, j - 1 return True ``` @@ -73,22 +78,47 @@ class Solution { int i = 0, j = s.length() - 1; while (i < j) { while (i < j && !Character.isLetterOrDigit(s.charAt(i))) { - i++; + ++i; } while (i < j && !Character.isLetterOrDigit(s.charAt(j))) { - j--; + --j; } if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(j))) { return false; } - i++; - j--; + ++i; + --j; } return true; } } ``` +### **C++** + +```cpp +class Solution { +public: + bool isPalindrome(string s) { + int i = 0, j = s.size() - 1; + while (i < j) { + while (i < j && !isalnum(s[i])) { + ++i; + } + while (i < j && !isalnum(s[j])) { + --j; + } + if (tolower(s[i]) != tolower(s[j])) { + return false; + } + ++i; + --j; + } + return true; + } +}; +``` + ### **Go** ```go @@ -104,8 +134,7 @@ func isPalindrome(s string) bool { if tolower(s[i]) != tolower(s[j]) { return false } - i++ - j-- + i, j = i+1, j-1 } return true } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.cpp" new file mode 100644 index 0000000000000..5bd0441865b63 --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.cpp" @@ -0,0 +1,20 @@ +class Solution { +public: + bool isPalindrome(string s) { + int i = 0, j = s.size() - 1; + while (i < j) { + while (i < j && !isalnum(s[i])) { + ++i; + } + while (i < j && !isalnum(s[j])) { + --j; + } + if (tolower(s[i]) != tolower(s[j])) { + return false; + } + ++i; + --j; + } + return true; + } +}; \ No newline at end of file diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.go" "b/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.go" index 69bd197e96b53..a6d36fa9e4268 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.go" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.go" @@ -10,8 +10,7 @@ func isPalindrome(s string) bool { if tolower(s[i]) != tolower(s[j]) { return false } - i++ - j-- + i, j = i+1, j-1 } return true } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.java" "b/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.java" index 790a7bef11b2c..24246142e53e9 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.java" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.java" @@ -1,19 +1,19 @@ -class Solution { - public boolean isPalindrome(String s) { - int i = 0, j = s.length() - 1; - while (i < j) { - while (i < j && !Character.isLetterOrDigit(s.charAt(i))) { - i++; - } - while (i < j && !Character.isLetterOrDigit(s.charAt(j))) { - j--; - } - if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(j))) { - return false; - } - i++; - j--; - } - return true; - } -} +class Solution { + public boolean isPalindrome(String s) { + int i = 0, j = s.length() - 1; + while (i < j) { + while (i < j && !Character.isLetterOrDigit(s.charAt(i))) { + ++i; + } + while (i < j && !Character.isLetterOrDigit(s.charAt(j))) { + --j; + } + if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(j))) { + return false; + } + ++i; + --j; + } + return true; + } +} \ No newline at end of file diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.py" "b/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.py" index 281ed748b1903..0eea9cf942917 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.py" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.py" @@ -1,13 +1,12 @@ -class Solution: - def isPalindrome(self, s: str) -> bool: - i, j = 0, len(s) - 1 - while i < j: - while i < j and not s[i].isalnum(): - i += 1 - while i < j and not s[j].isalnum(): - j -= 1 - if s[i].lower() != s[j].lower(): - return False - i += 1 - j -= 1 - return True +class Solution: + def isPalindrome(self, s: str) -> bool: + i, j = 0, len(s) - 1 + while i < j: + while i < j and not s[i].isalnum(): + i += 1 + while i < j and not s[j].isalnum(): + j -= 1 + if s[i].lower() != s[j].lower(): + return False + i, j = i + 1, j - 1 + return True