【leetcode】Valid Palindrome
来源:互联网 发布:网络架构师工资 编辑:程序博客网 时间:2024/05/25 16:39
链接:https://oj.leetcode.com/problems/valid-palindrome/
描述:
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,"A man, a plan, a canal: Panama"
is a palindrome."race a car"
is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
解法:
直接求解。O(n)。Accept
代码如下:
bool isPalindrome(string s) { int len = s.length(); if(len <= 1) return true; int start = 0; int end = len -1; while( start < end) { while(start < end && !isalnum(s[start])) start++; while(start < end && !isalnum<span style="font-family: Arial, Helvetica, sans-serif;">(s[end]) )</span> end--; char ch1 = s[start]; char ch2 = s[end]; if( start < end && tolower(ch1) != tolower(ch2)) return false; start++,end--; } return true; }
代码二:
修改了原输入,需慎重。
bool isPalindrome(string s) { transform(s.begin(), s.end(), s.begin(),::tolower); auto left = s.begin(), right = prev(s.end()); while(left < right){ if(!::isalnum(*left)) ++left; else if(!::isalnum(*right)) --right; else if(*left != *right) return false; else { ++left, --right; } } return true; }
0 0
- [Leetcode] Valid Palindrome
- Leetcode: Valid Palindrome
- leetcode 55: Valid Palindrome
- [LeetCode] Valid Palindrome
- 【leetcode】Valid Palindrome
- LeetCode - Valid Palindrome
- [leetcode]Valid Palindrome
- [LeetCode]Valid Palindrome
- [Leetcode]Valid Palindrome
- LeetCode-Valid Palindrome
- [leetcode] Valid Palindrome
- LeetCode 125: Valid Palindrome
- LeetCode - Valid Palindrome
- leetcode之Valid Palindrome
- LeetCode | Valid Palindrome
- [leetcode]Valid Palindrome
- 【leetcode】Valid Palindrome
- LeetCode题解:Valid Palindrome
- FFmpeg接口 编写视频转码测试程序结果
- hdoj 1425 sort
- 黑球白球
- Android之Monkey全参数(包含隐藏参数)
- ZOJ2110 Tempter of the Bone
- 【leetcode】Valid Palindrome
- 求解一个简单的创建单链表的问题为什么用二级指针 ?
- MFC多线程编程之三——线程间通讯
- [JQuery]学习——过滤选择器
- ubuntu 修改 ls 下的目录颜色
- C#操作注册表,设置系统环境变量
- MFC不能多线程操作控件的原因
- SP2-0667: Message file sp1<lang>.msb not found SP2-0750: You may need to set ORACLE_HOME to your Ora
- ubuntu下SHELL编程基础