zoukankan      html  css  js  c++  java
  • leetcode--Palindrome Partitioning

    1.题目描述

    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.

    2.解题思路

    题目很简单,但是就是要注意考虑到大小写和空字符串的问题(空字符串包括删除无用字符串之后为空的情况)

    class Solution {
    public:
        bool isPalindrome(string s) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            
            if(s.empty())return true;
            
            string s_argument;
            
            s_argument.push_back('#');
            
            for(int i=0;i<s.length();++i)
            {
                if((s[i]<='z'&&s[i]>='a')||(s[i]>='0'&&s[i]<='9')||(s[i]>='A'&&s[i]<='Z'))
                {
                    if(s[i]>='A'&&s[i]<='Z')
                    {
                        s_argument.push_back(s[i]-'A'+'a');
                    }
                    else s_argument.push_back(s[i]);
                    s_argument.push_back('#');
                }
            }
            
            if(s_argument.length()==1)return true;
            for(int i=s_argument.length()/2-1;i>=0;i--)
            {
                if(s_argument[i]!=s_argument[s_argument.length()-1-i])return false;
                
            }
            
            return true;    
            
        }
    };
  • 相关阅读:
    grpc(五)
    go的代码库
    grpc(四)
    grpc(三)
    grpc(二)
    grpc(一)
    java的泛型
    如何学习编程语言
    老男孩K8S集群部署(二)
    VMware虚拟机状态正常,但SecureCRT连接时显示超时的解决方法
  • 原文地址:https://www.cnblogs.com/obama/p/3247173.html
Copyright © 2011-2022 走看看