zoukankan      html  css  js  c++  java
  • leetcode--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.

    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.

    #include<iostream>
    #include<string.h>
    using namespace std;
    bool isPalindrome(string s) 
    {
        string temp;
        if(s.length() == 0 || s.length() == 1)
            return 1;
            for(int i = 0;i<s.length();i++)
            {
                if(s[i]>='a' && s[i]<='z')
                {
                    temp += s[i];
                }
                else if(s[i] >= 'A' && s[i] <= 'Z')
                {
                    temp += (char)((int)s[i]+32);
                }
                else if(s[i]>='0' && s[i]<='9')
                {
                    temp += s[i];
                }
            }
            int low = 0;
            int high = temp.length()-1;
            bool result = true;
            if(temp.length()==0 || temp.length()==1)
                return 1;
            while(low < high)
            {
                if(temp[low] != temp[high])
                {
                    result = false;
                    return result;
                }
                else 
                {
                    low ++;
                    high --;
                }
            }
            return result;
    }


    int main()
    {
        //string s = "a.";
        cout << isPalindrome(s) << endl;
        system("pause");
        
        return 0;

    } 

  • 相关阅读:
    spring bean的生命周期
    02-MySQL主要配置文件
    01-MySQL Linux安装
    spring自动装配
    JVMGC+Spring Boot生产部署和调参优化
    java面试-生产环境出现CPU占用过高,谈谈你的分析思路和定位
    java面试-G1垃圾收集器
    java面试-垃圾回收器谈谈你的理解
    java面试-谈谈你对OOM的理解
    RPC介绍以及编程
  • 原文地址:https://www.cnblogs.com/xiawen/p/3291968.html
Copyright © 2011-2022 走看看