首页 > 其他 > 详细

【LeetCode】680. Valid Palindrome II

时间:2019-09-29 23:59:40      阅读:152      评论:0      收藏:0      [点我收藏+]

Difficulty:easy

 More:【目录】LeetCode Java实现

Description

https://leetcode.com/problems/valid-palindrome-ii/

Given an array of integers, return indices of the two numbers such that they add up to a specific 

Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.

Example 1:

Input: "aba"
Output: True

 

Example 2:

Input: "abca"
Output: True
Explanation: You could delete the character ‘c‘.

Intuition

1.Make use of two pointers.

 

Solution

    public boolean validPalindrome(String s) {
        for(int i = 0, j = s.length()-1; i < j; i++,j--){
            if(s.charAt(i) != s.charAt(j))
                return isPalindrome(s, i, j-1) || isPalindrome(s, i+1, j);
        }
        return true;
    }
    
    private boolean isPalindrome(String s, int i, int j){
        while(i < j){
            if(s.charAt(i++) != s.charAt(j--))
                return false;
        }
        return true;
    }

  

Complexity

Time complexity : O(n)

Space complexity : O(1)

 

What I‘ve learned

1. It‘s important to learn and make use of the method isPalindrome() 

 More:【目录】LeetCode Java实现

 

【LeetCode】680. Valid Palindrome II

原文:https://www.cnblogs.com/yongh/p/11610216.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!