力扣-9.23-680

在这里插入图片描述

class Solution {
    
    
    public boolean validPalindrome(String s) {
    
    
        char[] str=s.toCharArray();
        int low=0,high=str.length-1;
        while(low<high){
    
    
            if(str[low]!=str[high]){
    
    
                return isPalindrome(str,low+1,high) || isPalindrome(str,low,high-1);
            }
        }
        return true;
    }

    public boolean isPalindrome(char[] str,int low,int high){
    
    
        while(low<high){
    
    
            if(str[low]!=str[high]){
    
    
                return false;
            }
            low++;
            high--;
        }
        return true;
    }
}
class Solution {
    
    
    public boolean validPalindrome(String s) {
    
    
        int low=0, high=s.length()-1;
        while(low<high) {
    
    
            if (s.charAt(low) != s.charAt(high)) {
    
    
                return isPalindrome(s, low, high - 1) || isPalindrome(s, low + 1, high);
            }
        }
        return true;
    }

    private boolean isPalindrome(String s, int low, int high) {
    
    
        while (low < high) {
    
    
            if (s.charAt(low) != s.charAt(high)) {
    
    
                return false;
            }
            low++;
            high--;
        }
        return true;
    }
}

注意:

1. length和length()的用法
2. String的charAt()方法

猜你喜欢

转载自blog.csdn.net/Desperate_gh/article/details/108759486
今日推荐