#Leetcode# 125. Valid Palindrome

https://leetcode.com/problems/valid-palindrome/

 

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Note: For the purpose of this problem, we define empty string as valid palindrome.

Example 1:

Input: "A man, a plan, a canal: Panama"
Output: true

Example 2:

Input: "race a car"
Output: false

代码:

class Solution {
public:
    bool isPalindrome(string s) {
        int len = s.length();
        int L = 0, R = len - 1;
        while(L < R) {
            if(!isalnum(s[L])) L ++;
            else if(!isalnum(s[R])) R --;
            else if((s[L] + 32 - 'a') %32 != (s[R] + 32 - 'a') % 32) return false;
            else {
                L ++;
                R --;
            }
        }
        return true;
    }
};

  学到了一个新的函数 $isalnum$ 判断是不是字母或者数字!恰饭去了

posted @ 2018-11-27 16:13  丧心病狂工科女  阅读(130)  评论(0编辑  收藏  举报