zoukankan      html  css  js  c++  java
  • 【LeetCode】125. Valid Palindrome

    Valid Palindrome

    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.

    左右分别往中间扫描,如果不同则返回false,如果两端碰面了则返回true

    代码里写了很多注释,我认为这类题难点在于循环的判断条件以及跳出条件

    因此对于每一个循环都需要明确它的进入条件以及跳出条件。

    class Solution {
    public:
        bool alphanumeric(char c)
        {
            if((c >= 'a' && c <= 'z') ||
               (c >= 'A' && c <= 'Z') ||
               (c >= '0' && c <= '9'))
                return true;
            else
                return false;
        }
        bool isPalindrome(string s) {
            if(s == "")
                return true;
            int i = 0;
            int j = s.size()-1;
            while(i < j)
            {
                while(!alphanumeric(s[i]))
                    i ++;
                while(!alphanumeric(s[j]))
                    j --;
                if(i < j && tolower(s[i]) != tolower(s[j]))
                    return false;
                i ++;
                j --;
            }
            return true;
        }
    };

  • 相关阅读:
    PHP入门
    bootstrap框架
    jsp5 include forward param
    jsp4 Cookie
    网页定位导航
    jsp3 javabean
    Obsidian md安装闪退
    Excel 2016 Mac VBA 的变化 窗体消失
    Excel Mac 2016 调用 Applescript
    如何自学R
  • 原文地址:https://www.cnblogs.com/ganganloveu/p/4077246.html
Copyright © 2011-2022 走看看