LeetCode OJ - Valid Palindrome
生活随笔
收集整理的這篇文章主要介紹了
LeetCode OJ - Valid Palindrome
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目:
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.
解題思路:
設(shè)置前后兩個指針,依次比較,若不相等則表明不是回文串。注意調(diào)過不是字母的字符。
代碼:
class Solution { public:bool isPalindrome(string s) {if (s.empty()) return true;for (int i = 0, j = s.length() - 1; i <= j; i++, j--) {while (i < s.length() && !((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= '0' && s[i] <= '9'))) i++;while (j >= 0 && !((s[j] >= 'a' && s[j] <= 'z') || (s[j] >= 'A' && s[j] <= 'Z') || (s[j] >= '0' && s[j] <= '9'))) j--;if (i > j) break;if (s[i] != s[j] && s[i] != s[j] + 32 && s[i] + 32 != s[j]) {return false;}}return true;} };?
轉(zhuǎn)載于:https://www.cnblogs.com/dongguangqing/p/3727758.html
總結(jié)
以上是生活随笔為你收集整理的LeetCode OJ - Valid Palindrome的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java入门到精通——基础篇之stati
- 下一篇: 使用软件测试路由器性能报告,小米路由器网