LeetCode Valid Palindrome

网友投稿 701 2022-10-11

LeetCode Valid Palindrome

LeetCode Valid Palindrome

1.题目

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.

2.解答

首先是题目的意思是:判断一个句子,正的读和倒着读是否都一样。比如"abba",就是正读和倒读都一样。这题只是多少忽略大小写,忽略非字母和数字。所以​​"A man, a plan, a canal: Panama"​​正读和倒读是一样的。

class Solution {public: bool isValideChar(char c){ if(c >= 'A' && c <= 'Z'){ return true; } if(c >= 'a' && c <= 'z'){ return true; } if(c >= '0' && c <= '9'){ return true; } return false; } char tolowerCase(char c){ if(c >= 'A' && c <= 'Z'){ return c - 'A' + 'a'; }else{ return c; } } bool isPalindrome(string s) { int left = 0; int right = s.size() - 1; if(s.size() == 0){ return true; } int sSize = s.size(); while((isValideChar(s[left]) == false) && left < s.size()){ ++left; } while((isValideChar(s[right]) == false) && right >= 0){ --right; } while(left < right){ if(tolowerCase(s[left]) == tolowerCase(s[right])){ ++left; while((isValideChar(s[left]) == false) && left < s.size()){ ++left; } --right; while((isValideChar(s[right]) == false) && right >= 0){ --right; } }else{ return false; } } return true; }};

还是比较容易的,一个指向前,一个指向后,进行比较,过滤掉非字母数字。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:手机里的快应用,与原生App相较有什么优劣势?
下一篇:微信小程序二维码插件,以及授权登陆(小程序专属二维码)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~