【leetcode】valid-palindrome
来源:互联网 发布:什么软件买东西便宜 编辑:程序博客网 时间:2024/06/03 23:48
题目:
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.
题目解析:
给定一个字符串,确定它是否是一个回文(从前往后何从后到前读到的是一样的),只考虑字母数字字符和忽略情况。
这句话的意思有以下几点:
1、只需要对比的是 数字还有字母,其他的字符不需要考虑;
2、对于大写字母与小写字母默认是 相同的;
例如,
"A man, a plan, a canal: Panama"就是一个回文.
"race a car"不是一个回文
注意:
您是否考虑到字符串可能是空的?这在面试中是一个很好的问题。
为了解决这个问题,我们将空字符串定义为有效的回文。
另外,此题尽量使用递归来实现。
代码实现
class Solution {public://判断一个字符是不是一个数字,和字母bool isSuitAble(char c){if(( c >= '0'&&c<='9')|| (c>= 'A'&&c<='Z')||(c>= 'a'&&c<='z')){return true;}return false;}bool _isPalindrome(char * start,char* end){int size = end-start+1;//得到字符串的长度 if(size == 0||size==1)return true;//得到第一个字符还有最后的一个字符while(1){//只有一个字符 ,或者为空 if(end - start +1 < 2){return true;}if(isSuitAble(*start)&&isSuitAble(*end)){//考虑到大小写的问题if(*start == *end ||(*start < *end &&(*start + 32) == *end)||(*start > *end &&(*start - 32) == *end))return _isPalindrome(start+1,end-1);elsereturn false;}else if(!isSuitAble(*start)&&isSuitAble(*end)){++start;}else if(isSuitAble(*start)&&!isSuitAble(*end)){--end;}else{++start;--end;}}return false;}//使用递归来实现 bool isPalindrome(string s) {//判断一个字符串是不是一个回文(从左向右读和从右向左读到的字符串是一样的)int size = s.size();if(size == 0||size ==1)return true;char * str = (char *)s.c_str();char * start= str;char * end = start+size-1;return _isPalindrome(start,end);}};
阅读全文
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
- 【Objective-C Runtime动态加载】---动态创建类Class 动态创建类Class,动态添加Class成员变量与成员函数,动态变量赋值与取值,动态函数调用等方法 a.使用objc_
- 安卓活动的生命周期
- app自动化测试4---desired capabilities
- centos 6.5 编译 安装 folly指南
- 谈谈对Java中Unicode、编码的理解
- 【leetcode】valid-palindrome
- Java学习笔记-《Java程序员面试宝典》-第四章基础知识-4.5字符串与数组(4.5.1-4.5.3)
- BroadcastReceiver 详解
- Android7.0中文文档(API)-- PopupMenu
- 组件的使用
- 安卓活动的启动模式
- selenium web 自动化环境搭建(Windows7 环境, Chrome, Python)
- java:14-类型转换与接口
- 注解(Annotation)那点事