【LeetCode】125 - 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.

Tags :Two Pointers String

Solution 1 : my weak suolution, create a new string to input only alphanumeric

bool isPalindrome(string s) {
    if(s.empty())return true;
    string::iterator iter1=s.begin(),iter2=s.end();
    string news;
    for(;iter1!=iter2;iter1++){
        if(isalnum(*iter1))
            news.push_back(tolower(*iter1));
    }
    if(news.size()==1)return true;
    for(int i=0,j=news.size()-1;i<j;i++,j--){
        if(news[i]!=news[j])return false;
    }
    return true;
}

Solution 2: clean code

    bool isPalindrome(string s) {
        for (int i = 0, j = s.size() - 1; i < j; i++, j--) { // Move 2 pointers from each end until they collide
            while (isalnum(s[i]) == false && i < j) i++; // Increment left pointer if not alphanumeric
            while (isalnum(s[j]) == false && i < j) j--; // Decrement right pointer if no alphanumeric
            if (toupper(s[i]) != toupper(s[j])) return false; // Exit and return error if not match
        }

        return true;
    }

 

时间: 2024-10-13 06:11:15

【LeetCode】125 - Valid Palindrome的相关文章

【LeetCode】Longest Valid Parentheses 解题报告

[题目] Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example

【Leetcode_easy】680. Valid Palindrome II

problem 680. Valid Palindrome II 参考 1. Leetcode_easy_680. Valid Palindrome II; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/11091358.html

【LeetCode】36 - Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.(http://sudoku.com.au/TheRules.aspx) The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. N

【LeetCode】242. Valid Anagram (2 solutions)

Valid Anagram Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false. Note:You may assume the strin

【leetcode】Longest Valid Parentheses

Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length

【Leetcode】036. Valid Sudoku

题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note:A valid Sudoku board (partia

【LeetCode】20. Valid Parentheses

题目: 思路:用Stack基本操作完成. 要检测输入字符是否满足这个条件,一个非常合适的数据结构是stack,后进先出的特征正好满足检测的需求.在检测的时候,每次检查一个字符,如果是左括号,就入栈,如果是右括号,并且右括号和当前栈顶符号是左右配对的,那么就弹出栈顶并且进行下一次检测,如果不满足上面两种情况,就说明检查到了一个非法字符,返回false. public class Solution { public boolean isValid(String s) { if(s.length()=

【leetcode】367. Valid Perfect Square]

题目描述: Given a positive integer num, write a function which returns True if num is a perfect square else False. 解题分析: 这种找数字的题一般都用类似与二分查找的算法.需要注意的是比较平方和时考虑到integer溢出的情况.所以这个结果是要用Long类型保存.由此到来的改变是判断相等时要用“equals()”方法,而不是“==”. 实现代码: 1 public class Solutio

【leetcode 】20. Valid Parentheses

这题需要注意: 栈的空指针 全局变量stack需要clear 输入结束时栈长度不为空 public class Solution { private static final Stack<Character> stack = new Stack<Character>(); public boolean isValid(String s) { stack.clear(); for (int i = 0; i < s.length(); ++i) { char c = s.cha