[LeetCode]125. Valid Palindrome
来源:互联网 发布:全民k歌软件 编辑:程序博客网 时间:2024/06/06 12:56
[LeetCode]125. Valid Palindrome
题目描述
思路
1.筛选去掉无用字符
2.判断当前字符串是否回文串
代码
#include <iostream>#include <string>using namespace std;class Solution {public: bool isPalindrome(string s) { /* int src = 0, cur = 0; while (src < s.size()) { if ((s[src] >= '0' && s[src] <= '9') || (s[src] >= 'a' && s[src] <= 'z') || (s[src] >= 'A' && s[src] <= 'Z')) { if (s[src] >= 'A' && s[src] <= 'Z') s[cur++] = s[src++] - ('A' - 'a'); else s[cur++] = s[src++]; } else ++src; } s.resize(cur); int start = 0, end = s.size(); while (start < end) { if (s[start] == s[end - 1]) { start++; end--; } else return false; } return true; */ for (int i = 0, j = s.size() - 1; i < j; i++, j--) { // Move 2 pointers from each end until they collide while (isalnum(s[i]) == false && i < j) i++; // Increment left pointer if not alphanumeric while (isalnum(s[j]) == false && i < j) j--; // Decrement right pointer if no alphanumeric if (toupper(s[i]) != toupper(s[j])) return false; // Exit and return error if not match } return true; }};int main() { Solution s; cout << s.isPalindrome("race a car") << endl; system("pause"); return 0;}
阅读全文
0 0
- [LeetCode]125.Valid Palindrome
- LeetCode 125. Valid Palindrome
- [Leetcode] 125. Valid Palindrome
- 【LeetCode】125.Valid Palindrome
- [leetcode] 125.Valid Palindrome
- [leetcode] 125.Valid Palindrome
- Leetcode 125. Valid Palindrome
- 125. Valid Palindrome LeetCode
- 【LeetCode】125. Valid Palindrome
- Leetcode 125. Valid Palindrome
- LeetCode 125. Valid Palindrome
- LeetCode 125. Valid Palindrome
- LeetCode *** 125. Valid Palindrome
- 【leetcode】125. Valid Palindrome
- [leetcode] 125. Valid Palindrome
- leetcode-125. Valid Palindrome
- LeetCode-125.Valid Palindrome
- leetcode 125. Valid Palindrome
- Mysql各种关于函数的操作语句
- 51Nod 1202 子序列个数
- 20170607-ARM-2-虚拟机配置
- 第三本书firewalld
- py-faster-rcnn详解(1)——train_faster_rcnn_alt_opt.py接口说明
- [LeetCode]125. Valid Palindrome
- 阿里云(centOS 7)安装apache
- libgo windows编译
- OpenCL与CNN篇一:搭建在VS的OpenCL运行环境
- 最佳选手
- ios GCD
- 虚拟机中Centos7 部署nginx
- qwb去面试
- CSDN博客上,超过百万访问量的Android牛人都是谁?