`
SunnyYoona
  • 浏览: 361494 次
社区版块
存档分类
最新评论

[LeetCode]125.Valid Palindrome

 
阅读更多

【题目】

Valid Palindrome

Total Accepted:3479Total Submissions:16532My Submissions

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"isnota 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】

/*********************************
*   日期:2013-12-09
*   作者:SJF0115
*   题目: 125.Valid Palindrome
*   来源:http://oj.leetcode.com/problems/valid-palindrome/
*   结果:AC
*   来源:LeetCode
*   总结:
**********************************/
class Solution {
public:
    bool isStr(char &ch){
        //数字
        if(ch >= '0' && ch <= '9'){
            return true;
        }
        //小写字母
        else if(ch >= 'a' && ch <= 'z'){
            return true;
        }
        //大写字母
        else if(ch >= 'A' && ch <= 'Z'){
            //转换为小写
            ch += 32;
            return true;
        }
        return false;
    }

    bool isPalindrome(string s){
        int i,j;
        int len = s.length();
        if(len == 0){
            return true;
        }
        string str = "";
        //去掉非数字字母字符
        for(i = 0; i < len; i++){
            if(isStr(s[i])){
                str += s[i];
            }
        }
        len = str.length();
        for(i = 0,j = len - 1; i < j; i++,j--){
            if(str[i] != str[j]){
                return false;
            }
        }
        return true;
    }
};


【方法2】

class Solution {
public:
    bool isStr(char &ch){
        //数字
        if(ch >= '0' && ch <= '9'){
            return true;
        }
        //小写字母
        else if(ch >= 'a' && ch <= 'z'){
            return true;
        }
        //大写字母
        else if(ch >= 'A' && ch <= 'Z'){
            //转换为小写
            ch += 32;
            return true;
        }
        return false;
    }

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


【测试】

/*********************************
*   日期:2013-12-09
*   作者:SJF0115
*   题号: 题目: Valid Palindrome
*   来源:http://oj.leetcode.com/problems/valid-palindrome/
*   结果:AC
*   来源:LeetCode
*   总结:
**********************************/
#include <iostream>
#include <stdio.h>
using namespace std;

class Solution {
public:
    bool isStr(char &ch){
        //数字
        if(ch >= '0' && ch <= '9'){
            return true;
        }
        //小写字母
        else if(ch >= 'a' && ch <= 'z'){
            return true;
        }
        //大写字母
        else if(ch >= 'A' && ch <= 'Z'){
            //转换为小写
            ch += 32;
            return true;
        }
        return false;
    }

    bool isPalindrome(string s){
        int i,j;
        int len = s.length();
        if(len == 0){
            return true;
        }
        string str = "";
        //去掉非数字字母字符
        for(i = 0; i < len; i++){
            if(isStr(s[i])){
                str += s[i];
            }
        }
        len = str.length();
        for(i = 0,j = len - 1; i < j; i++,j--){
            if(str[i] != str[j]){
                return false;
            }
        }
        return true;
    }
};
int main() {
    bool result;
    string str = "A man, a plan, a canal: Panama";
    //string str = "race a car";
    Solution solution;
    result = solution.isPalindrome(str);
    if(result){
        printf("This is a Palindrome\n");
    }
    else{
        printf("sorry\n");
    }
    return 0;
}







分享到:
评论

相关推荐

    程序员面试宝典LeetCode刷题手册

    第四章 Leetcode 题解 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most ...

    LeetCode最全代码

    # [LeetCode](https://leetcode.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [![License]...

    lrucacheleetcode-leetcode:leetcode

    Palindrome Number 11. Container With Most Water 12. Integer to Roman 13. Roman to Integer 14. Longest Common Prefix 15. 3Sum 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses ...

    leetcode516-Lcode:代码

    leetcode 516 8/13 - 8/18 ...125. Valid Palindrome 28. Implement strStr() 5. Longest Palindromic Substring option: 516. Longest Palindromic Subsequence string replacement strStr II 链接:

    javalruleetcode-LeetCode:LeetCode算法问题

    Palindrome LeetCode 167 Two Sum II - Input array is sorted LeetCode 344 Reverse String LeetCode 345 Reverse Vowels of a String 2 字符串 编号 题目 LeetCode 3 Longest Substring Without Repeating ...

    leetcode不会-ValidPalindrome:这是我在Python中针对LeetCode上的ValidPalindrome问题的解决方

    Valid Palindrome 问题的解决方案。 首先,我将给定字符串中的所有字符都小写,这样就不会有任何违规行为。 然后我创建两个空数组。 在一个数组中,我将给定字符串的所有字母数字值附加到该数组中。 在第二个中,我...

    leetcode环形数组取值-Crack-Interview::ghost:为什么人们一次又一次地这样做?

    Palindrome 给定一个字符串,判断该字符串是否是回文串。回文串是从前后方向阅读都相同的串。 同时从字符串正序、逆序遍历字符串,如果所有字符都相同,则是回文串,否则不是回文串。 8. String to Integer 给定一个...

    erlang入门级练习:LeetCode OJ问题的部分erlang 源码

    我自己在新学erlang,在LeetCode OJ上找了题目练习,题目很适合新手熟悉语言,但是LeetCode OJ里面只有几门主流语言的... "valid_palindrome.erl" 个人认为dungeon_game这个题目解题逻辑很体现erlang的函数式的思维逻辑

    leetcode分类-leetcode-practice:LeetCode题解

    leetcode 分类 ...Palindrome II Easy 数据结构 二叉树 题目 难度 原题 解答 26. 树的子结构 中等 未分类 鸣谢 题目选择、分类参考了 关于 同步自我的博客:, 欢迎关注我的微信公众号「清风迅来」

    lrucacheleetcode-Algorithm:一些常见的算法的解题报告

    lru ...9.Palindrome Number 11.Container With Most Water 14.Longest Common Prefix 15.3Sum 16.3Sum Closest 19.Remove Nth Node From End of List 20.Valid Parentheses 21.Merge Two Sorted L

    丢失的最小正整数leetcode-LeetCodePractice:我的LeetCode练习

    Palindrome Number:我的思路是直接利用取余法求得倒过来的数然后与原数据进行比较,直接但效率不高,从其他人那看到的思路,其实可以只求一半倒数然后与另一半作比较,既方便又快捷。 13. Roman to Integer:这道题...

    leetcode中国-leetcode:leetcode刷题

    Palindrome Implement strStr() String to Integer (atoi) addBinary longestPalindrome maximal rectangle :dp问题,较难 largestRectangleArea 求直方图的最大面积,左右两次扫面+剪枝优化 Valid Parentheses 用栈...

    leetcode2-Algorithms-Practice:创建此repo是为了跟踪我在解决问题方面的进展

    Palindrome Number 运行时间:92 毫秒内存使用:14 MB 13. Roman to Integer 运行时间:52 毫秒内存使用:14.1 MB 14. Longest Common Prefix 运行时间:40 毫秒内存使用:13.9 MB 20. Valid Parentheses 运行时间:...

    LeetCode:LeetCode题解

    LeetCode LeetCode题解 传送门 # 标题 解决方案 困难 笔记 1个 简单的 ...125 Valid_Palindrome Java 简单的 136 单号 Java 简单的 137 Single_NumberII Java 中等的 167 Two_Sum_II_Input_

    leetcodepython001-LeetCode:力码

    leetcode Python 001 力码 简单的 # 问题 完成日期 语 001 001_Two_Sum ...125_Valid_Palindrome 2021 年 6 月 12 日 C# 009 412_FizzBu​​zz 2021 年 6 月 22 日 C# 中等的 # 问题 完成日期 语

    leetcode题库-LeetCode:力码

    Palindrome Number.cpp 12 整数转罗马数字 Integer to Roman.cpp 13 罗马数字转整数 Roman to Integer.cpp 15 三数之和 3Sum.cpp 最接近的三数之和 3Sum Closest .cpp 20 有效的括号 Valid Parentheses.cpp 22 括号...

    leetcode卡-LeetCode:LeetCode题解

    Palindrome :star: 有效回文,小写字母转换 0005 Longest Palindromic Substring :star: :star: :star: 最长回文子串,Manacher算法 0010 RegularExpressionMatching :star: :star: :star: 正则表达式匹配,dp 0012 ...

    leetcode浇花-LCSolutions:我的力扣解决方案

    leetcode 浇花力扣解决方案 简单的 #0001 - Two Sum #0007 - Reverse Integer #0009 - Palindrome Number #0035 - Search Insert Position #0058 - Length of Last Word #0066 - Plus One #0083 - Remove Duplicates...

    颜色分类leetcode-leetcode-[removed]我对Leetcode问题的解决方案

    Palindrome Number 回文数 11 Container With Most Water 盛最多水的容器 13 Roman to Integer 罗马数字转整数 14 Longest Common Prefix 最长公共前缀 20 Valid Parentheses 有效的括号 26 Remove Duplicates from ...

    gasstationleetcode-leetcode-in-niuke:在牛客网上的在线编程中的leetcode在线编程题解

    gas station leetcode 在牛客网上的在线编程中的leetcode在线编程题解 代码中有详细题解 完成: 树 Minimum Depth of ...evaluate-reverse-polish-notation ...valid-palindrome 模拟 pascals-triangle 模拟 pasca

Global site tag (gtag.js) - Google Analytics