前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 125 Valid Palindrome

LeetCode 125 Valid Palindrome

作者头像
ShenduCC
发布2018-07-24 16:08:37
2940
发布2018-07-24 16:08:37
举报
文章被收录于专栏:算法修养

题目:https://leetcode.com/problems/valid-palindrome/description/

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:

代码语言:javascript
复制
Input: "A man, a plan, a canal: Panama"
Output: true

Example 2:

代码语言:javascript
复制
Input: "race a car"
Output: false

Seen this question in a real interview before?  

Yes

No

c++:

代码语言:javascript
复制
class Solution {
public:
    bool isPalindrome(string s) {
        if(s.length()==0) return true;
        int l = s.length();
        for(int i=0,j=l-1;i<=j;i++,j--)
        {
            if(judge(s[i])&&judge(s[j]))
            {
                if(change(s[i])!=change(s[j]))
                    return false;
            }
            else if(!judge(s[i])&&judge(s[j]))
            {
                j++;
            }
            else if(judge(s[i])&&!judge(s[j]))
            {
                i--;
            }
                
        }
        
        return true;
        
    }
    int judge(char x)
    {
         if((x>='a'&&x<='z')||(x>='A'&&x<='Z')||(x>='0'&&x<='9'))
             return 1;
        else
            return 0;
            
    }
    char change(char x)
    {
        if(x>='A'&&x<='Z')
            return x+32;
        else
            
            return x;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-07-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档