当前位置:网站首页>Leetcode skimming questions_ Verify palindrome string II

Leetcode skimming questions_ Verify palindrome string II

2022-07-06 01:24:00 Figure throne

Title Description

Java resolvent  

class Solution {
    public boolean Palindrome(String s, int start, int end)
    {
        while(start < end)
        {
            if(s.charAt(start) == s.charAt(end))
            {
                start++;
                end--;
            }
            else
            {
                return false;
            }
        }
        return true;
    }

    public boolean validPalindrome(String s) {
        int start = 0;
        int end = s.length() - 1;
        while(start < end)
        {
            if(s.charAt(start) == s.charAt(end))
            {
                start++;
                end--;
            }
            else
            {
                break;
            }
        }
        if(start >= end)
        {
            return true;
        }
        else
        {
            boolean valid1 = Palindrome(s, start + 1, end);
            boolean valid2 = Palindrome(s, start, end - 1);
            if(valid1 || valid2)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

 

C resolvent

bool Palindrome(char * s, int start, int end)
{
    while(start < end)
        {
            if(s[start] == s[end])
            {
                start++;
                end--;
            }
            else
            {
                return false;
            }
        }
        return true;
}

bool validPalindrome(char * s){
    bool valid1;
    bool valid2;
    int start = 0;
    int end = strlen(s) - 1;
    while(start < end)
    {
        if(s[start] == s[end])
        {
            start++;
            end--;
        }
        else
        {
            break;
        }
    }
    if(start >= end)
    {
        return true;
    }
    else
    {
        valid1 = Palindrome(s, start + 1, end);
        valid2 = Palindrome(s, start, end - 1);
        if(valid1 || valid2)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

 

原网站

版权声明
本文为[Figure throne]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060119162505.html