这道题本身是非常简单的,但是由于审题不仔细,忽略一个细节,导致提交了 6 次才成功,要检讨!
来源:力扣 (LeetCode)
链接:https://leetcode-cn.com/problems/valid-palindrome
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
一、题目描述
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
说明:本题中,我们将空字符串定义为有效的回文串。
示例 1:
- 输入: "A man, a plan, a canal: Panama"
- 输出: true
示例 2:
- 输入: "race a car"
- 输出: false
二、题解
思路:使用双指针,一个从前往后遍历,一个后从往前遍历。遇到非数字和字符就跳过,如果发现两个指针的字符不一样就返回 false,直到两个指针汇合,返回 true 。
要特别注意到的是回文串也包含了数字,我就是因为没看到还包含了数字,所以提交了 6 次才过。一直卡在测试案例"0P"上,注意是」 零+P「,不是」 欧+P「。因为 0 和 O 太像了,我看成了"OP",本地测没问题,提交就有问题,最后失败了 5 次才发现,看评论区才知道大家都被这个输入坑了!!
![[leetcode]125-验证回文串-图片1](https://www.dyxmq.cn/wp-content/uploads/2020/03/5bfa5-image.png)
三、代码
|
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 |
// 判断是否是字符或数字 bool isCharOrNum(char ch) { return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9'); } class Solution { public: bool isPalindrome(string s) { int i = 0, j = s.size() - 1; while (i < j) { // 跳过非字符 if (!isCharOrNum(s[j])) { j--; continue; } if (!isCharOrNum(s[i])) { i++; continue; } // 忽略大小写 if (tolower(s[i]) != tolower(s[j])) return false; i++, j--; } return true; } }; |
单元测试:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <gtest/gtest.h> TEST(leetcode, demo) { string s1("A man, a plan, a canal: Panama"), s2("race a car"), s3("OP"); Solution s; EXPECT_TRUE(s.isPalindrome(s1)); EXPECT_FALSE(s.isPalindrome(s2)); EXPECT_FALSE(s.isPalindrome(s3)); } TEST(leetcode, OP) { string s1("PO"), s2("0P"); Solution s; EXPECT_FALSE(s.isPalindrome(s1)); EXPECT_FALSE(s.isPalindrome(s2)); } int main() { ::testing::InitGoogleTest(); return RUN_ALL_TESTS(); } |
![[leetcode]125-验证回文串-图片2](https://www.dyxmq.cn/wp-content/uploads/2020/03/c0f1c-imagee6e09d9042ce3ecd.png)
![[leetcode]226-翻转二叉树](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/02/cff55-image8d13f59af4ceb4d0.png&w=280&h=210&a=&zc=1)
![[leetcode]410-分割数组的最大值](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/02/a851e-image00f04be2c08aea1c.png&w=280&h=210&a=&zc=1)
![[leetcode]276-栅栏涂色](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/02/532ce-image57287b3fd0fa77e2.md.png&w=280&h=210&a=&zc=1)
![[leetcode-shell]192-统计词频](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/02/b3b4a-image6c552cb516ad2b7c.png&w=280&h=210&a=&zc=1)

![[leetcode]199-二叉树的右视图](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/f24a8-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]72-编辑距离](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/88c60-imageff84a6c5047db6ed.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]460-LFU缓存](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/5b3f2-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[程序员面试宝典]17.16-按摩师](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/9ecc9-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]876-链表的中间节点](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/64d0d-imagece778ab033c9d90d.png&w=280&h=210&a=&zc=1)
![【每日打卡】[剑指offer]面试题40-最小的k个数](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/c83c9-imagec745c425c670e18c.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]-409最长回文子串](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/e5589-image788d511d7c1e839c.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]面试题1.6-字符串压缩](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/776bc-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]695-岛屿的最大面积](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/aa889-image036941cd6bbf2296.png&w=280&h=210&a=&zc=1)
评论