力扣-9.23-680

Desperate_gh發表於2020-09-23

在這裡插入圖片描述

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()方法

相關文章