zoukankan      html  css  js  c++  java
  • leetcode26:valid-palindrome

    题目描述

    判断题目给出的字符串是不是回文,仅考虑字符串中的字母字符和数字字符,并且忽略大小写
    例如:"A man, a plan, a canal: Panama"是回文
    "race a car"不是回文
    注意:
    你有没有考虑过字符串可能为空?这是面试时应该提出的一个好问题。
    针对这个问题,我们定义空字符串是回文


    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

    输入

    复制
    "A man, a plan, a canal: Panama"

    输出

    复制
    true
    
    示例2

    输入

    复制
    "race a car"

    输出

    class Solution {
    public:
        /**
         *
         * @param s string字符串
         * @return bool布尔型
         */

    bool isPalindrome(string s) {
            int i,j;
            for(i=0,j=s.length()-1;i<j;++i,--j){
                while(i<j && !isalnum(s[i])) ++i;
                while(i<j && !isalnum(s[j])) --j;
                if (i<j && tolower(s[i])!=tolower(s[j])) return false;
            }
            return true;
        }

    };

  • 相关阅读:
    《JavaScript 闯关记》之初探
    《JavaScript 闯关记》之简介
    《JavaScript 闯关记》
    JavaScript检测之basevalidate.js
    如何排版 微信公众号「代码块」
    android开发之路03
    android开发之路02(浅谈BroadcastReceiver)
    android开发之路01
    软件工程复习(一)
    软件工程—人件(一)
  • 原文地址:https://www.cnblogs.com/hrnn/p/13417015.html
Copyright © 2011-2022 走看看