【 关关的刷题日记51】 Leetcode 67. Add Binary

时间:2022-05-08
本文章向大家介绍【 关关的刷题日记51】 Leetcode 67. Add Binary,主要内容包括题目、思路、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

关关的刷题日记 52 – 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.

题目的意思是给定一个字符串,只考虑其中的数字和字母,判断该字符串是否是回文。

思路

思路:设置头尾指针同时向中间遍历字符串,遇到非字母数字的字符需要跳过,判断头尾指针所指的字符是否相等,如果不相等的话,就不是回文。这题目也可以直接用一个函数isalnum来判断是否是字母数字。

class Solution {public:
    bool isPalindrome(string s) {
        if(s.empty() || s.size()==1)
            return true;
        for(int i=0, j=s.size()-1;i<j;)
        {
            //while(!(tolower(s[i])>='a' && tolower(s[i])<='z' || s[i]>='0' && s[i]<='9'))
            while(!(isalnum(s[i])))
                i++;
            //while(!(tolower(s[j])>='a' && tolower(s[j])<='z' || s[j]>='0' && s[j]<='9'))
            while(!(isalnum(s[j])))  
                j--;
            if(i<j)
            {
                if(tolower(s[i])==tolower(s[j]))
                {
                    i++;
                    j--;
                }
                else
                    return false;
            }
        }
        return true;
    }};

人生易老,唯有陪伴最长情,加油!

以上就是关关关于这道题的总结经验,希望大家能够理解,有什么问题可以在我们的专知公众号平台上交流或者加我们的QQ专知-人工智能交流群 426491390,也可以加入专知——Leetcode刷题交流群(请先加微信小助手weixinhao: Rancho_Fang)。