[C++]LeetCode: 25 Valid Palindrome
来源:互联网 发布:access2007数据库实例 编辑:程序博客网 时间:2024/05/11 01:59
题目:
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.
判断一个字符串是否是回文,只考虑字母和数字,忽略符号。定义空字符为空回文。
ORI_思路
思路:得到翻转的字符串,然后比较是否相同,将符号和空格去掉,都转为小写,得到S,ReversS.比较。
考虑几个问题:1.空回文 2.去掉符号和空格 3.strcmp比较ASCII,所以要统一大小写。ASCII码48~57 数字; 65~90 大写字母; 97~122 小写字母。
分析:1. 需要存储两个字符串,一个栈,效率低,占用太多内存。 2. 判断是否是字符数字,不需要具体ASCII,用对应字符就可以。
3. 无法比较两个non const string.
Submission Result: Compile Error
Error Code:
class Solution {public: bool isPalindrome(string s) { //判断一个字符串是否是回文,只考虑字母和数字,忽略符号。 //定义空字符为空回文。 //思路:得到翻转的字符串,然后比较是否相同,将符号和空格去掉,都转为小写,得到S,ReversS.比较 //考虑几个问题:1.空回文 2.去掉符号和空格 3.strcmp比较ASCII,所以要统一大小写。 //ASCII码48~57 数字; 65~90 大写字母; 97~122 小写字母。 bool ret = false; string lowerS; //小写s string revslowerS; //翻转小写s stack<char> strstack; int strlen = s.length(); if(strlen == 0) return true; for(int i = 0 ; i < strlen; i++) { if((s[i] >= 65 && s[i] <= 90)) { lowerS.push_back(s[i] + 32); strstack.push(s[i] + 32); } else if((s[i] >= 48 && s[i] <= 57)|| (s[i] >=97 && s[i] <= 122)) { lowerS.push_back(s[i]); strstack.push(s[i]); } } //翻转s while(!strstack.empty()) { revslowerS.push_back(strstack.top()); strstack.pop(); } if(strcmp(lowerS, revslowerS) == 0) ret = true; return ret; }};
正解:
思路: 构造函数判断每个字符是否是数字字符,从正反两个方向遍历字符串,比较是否相等(i < j 循环)。不断往字符串中间逼近。
复杂度:O(N)
Attention: 需要把判断函数写在主函数外; 判断函数参数用引用类型, 直接进行大小写转换!!
AC Code:
class Solution {public: //判断是否是数字或字符 bool isStr(char &ch){ if(ch >= '0' && ch <= '9'){ return true; } else if(ch >= 'a' && ch <= 'z'){ return true; } else if(ch >= 'A' && ch <= 'Z'){ ch += 32; return true; } else{ return false; } } bool isPalindrome(string s) { int len = s.length(); if(len == 0) return true; int i = 0, j = len - 1; while(i < j){ if(!isStr(s[i])){ ++i; } else if(!isStr(s[j])){ --j; } else if(s[i++] != s[j--]){ return false; } } return true; }};
- [C++]LeetCode: 25 Valid Palindrome
- [leetcode-125]Valid Palindrome(c++)
- [leetcode]Valid Palindrome C语言
- 【leetcode c++】125 Valid Palindrome
- LeetCode 之 Valid Palindrome — C 实现
- leetcode Valid Palindrome C++&python 题解
- LeetCode 125:Valid Palindrome(c++)
- 【leetcode】125. Valid Palindrome(Python & C++)
- [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
- Ubuntu上搭建Hadoop环境(单机模式+伪分布模式)
- Android系统中查看某个应用当前流量的方法
- 一个服务器上面部署多个tomcat
- Gzip compression/decompression
- spring mvc文件上传下载
- [C++]LeetCode: 25 Valid Palindrome
- 关系/对象映射 多对多关系(@ManyToMany 注释)【重新认识】
- Golang flag包使用详解(一)
- hdu nyoj 220 推桌子
- iOS客户端的gzip解压
- Linux下几种文件传输命令 sz rz sftp scp
- 第十三周 项目一:数组大折腾(1.1)
- Linux下php+mysql+nginx编译搭建(三)
- SQL中EXISTS的用法