leetcode ---- 125. palindromic sequence verification (+ double pointer string processing)

tags: Power button  Double pointer  String

Given a string, verify that it is a palindrome string, consider only the alphabetic and numeric characters, can ignore letter case.
Description: In this problem, we define the empty string as a valid palindromic sequence.
Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:
Input: "race a car"
Output: false
Source: stay button (LeetCode)
Link: https: //leetcode-cn.com/problems/valid-palindrome
network all the copyright collar button. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
ideas:
(1) is determined only numbers and letters, and the letters case-insensitive
(2) arranged to take two pointers to the string head and tail
(3) traversing the head and tail pointers of the string is determined whether the character is equal to

/*
 * @lc app=leetcode.cn id=125 lang=cpp
 *
 * [125] palindromic sequence verification
 */

// @lc code=start
#include<string>
using namespace std;
class Solution {
public:
    bool ischarornum(char c){
        if (c >= 'a' && c <= 'z')
            return true;
        if (c >= 'A' && c <= 'Z')
            return true;
        if (c >= '0' && c <= '9')
            return true;
        return false;
    } 
    bool isPalindrome(string s) {
        if (s.length()<2)
            return true;
        int left =0,right = s.length() -1;
        while (left < right){
            if(!ischarornum(s[left]))
            {
                left++;
            }
            else if (!ischarornum(s[right]))
            {
                right--;
            }
            else if((s[left]+32-'a') % 32 != (s[right]+32-'a') % 32)
            {
                return false;
            }
            else{
                left++;right--;
            }
        }
        return true;
    }
};
// @lc code=end


Intelligent Recommendation

Palindromic sequence verification Leetcode 125. By Python

Thinking Apparently more than a string of characters, including letters and numbers, so we canFirst extractWe want to compare the character Another problem is that the letters are case-sensitive, we m...

leetcode question: 125 palindromic sequence verification (simple)

First, the subject description:125. Verify palindromic sequence(simple) Given a string, verify that it is a palindrome string, consider only the alphabetic and numeric characters, can ignore letter ca...

LeetCode:. 125 palindromic sequence verification (Java)

topic: Given a string, verify that it is a palindrome string, consider only the alphabetic and numeric characters, can ignore letter case. Description:In this problem, we will empty string is defined ...

LeetCode 125. palindromic sequence verification (C ++)

topic: Given a string, verify that it is a palindrome string, consider only the alphabetic and numeric characters, can ignore letter case. Description: In this problem, we define the empty string as a...

More Recommendation

LeetCode.125 palindromic sequence verification (Python solution)

table of Contents topic solution_1 Reference material topic Given a string, verify that it is a palindrome string, consider only the alphabetic and numeric characters, can ignore letter case. Descript...

125 palindromic sequence verification

Algorithm [125] leetcode palindromic sequence verification https://leetcode.com/problems/valid-palindrome/ 1)problem 2)answer 1, ordinary ideas The numbers and characters extracted, and then if it is ...

125, verification palindromic sequence

Given a string, verify that it is a palindrome string, consider only the alphabetic and numeric characters, can ignore letter case. Description: In this problem, we define the empty string as a valid ...

leetcode125. palindromic sequence verification (the double pointer traversal)

Given a string, verify that it is a palindrome string, consider only the alphabetic and numeric characters, can ignore letter case. Description: In this problem, we define the empty string as a valid ...

leetcode-125- palindromic sequence verification (valid palindrome) -java

Title and use cases Solution of a (successful, 4ms, soon) start looking from both sides of the string decimal letters, numbers, letters to upper case letters to lower case, testing for equality. if i ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top