125. Valid Palindrome
来源:互联网 发布:北京数据所 编辑:程序博客网 时间:2024/06/07 12:12
DescriptionHintsSubmissionsDiscussSolution
DiscussPick One
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.
Seen this question in a real interview before?
Yes
分析:有效的回文
给定一个字符串,判断是否是回文,只考虑字母数字,并且忽略大小写。
这道题唯一难点就是处理大小写和非数字字母的字符。其实c++有现成的函数
isalnum() 判断字符是否为数字或者字母
toupper()将字母变成大写
tolower()将字母变成小写
代码:
class Solution {public: bool isPalindrome(string s) { int first,last;first = 0;last = s.length()-1;while(first<=last){while(!(isalnum(s[first])) && first < last)first++;while(!(isalnum(s[last])) && first < last)last--;if(toupper(s[first])!=toupper(s[last]))return false;last--;first++;}return true; }};
阅读全文
0 0
- [LeetCode]125.Valid Palindrome
- LeetCode 125. Valid Palindrome
- [Leetcode] 125. Valid Palindrome
- 【LeetCode】125.Valid Palindrome
- [leetcode] 125.Valid Palindrome
- 125. Valid Palindrome
- [leetcode] 125.Valid Palindrome
- Leetcode 125. Valid Palindrome
- 125. Valid Palindrome
- 125. Valid Palindrome LeetCode
- 125. Valid Palindrome
- 【LeetCode】125. Valid Palindrome
- 125. Valid Palindrome
- 125. Valid Palindrome
- Leetcode 125. Valid Palindrome
- LeetCode 125. Valid Palindrome
- LeetCode 125. Valid Palindrome
- LeetCode *** 125. Valid Palindrome
- python之__new__方法与实例化
- 2017.11.1
- Http最常见的错误代码
- Android学习之通过SeekBar调节布局背景颜色(RGB)
- P
- 125. Valid Palindrome
- javascript之Object.assign()痛点
- 面试题
- 7-zip 下lzma数据解压缩方式
- java三大部件之Listener
- sublime常用快捷键
- 线程的同步控制---信号量、互斥锁、条件变量
- Q
- 详解C语言内存对齐