码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode]680. Valid Palindrome II有效回文II(可至多删一原字符)

时间:2018-10-18 10:58:59      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:fun   string   function   UNC   Plan   private   The   cte   cti   

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‘.

思路:

 

代码:

 1 class Solution {
 2     public boolean validPalindrome(String s) {
 3         int l = 0;
 4         int r = s.length()-1;
 5         while(l < r){
 6             if(s.charAt(l) != s.charAt(r)){
 7                 // try to delete a left side char || delete a right side char
 8                 return isPalin(s, l+1,r) || isPalin(s, l,r-1);
 9             }else{
10                 l++;
11                 r--;
12             }       
13         }
14         return true;
15         
16     }
17         
18    // helper function to judge valid palindrome      
19    private boolean isPalin(String s , int l, int r){   
20        while(l < r){
21            if(s.charAt(l++)!=s.charAt(r--)) return false;
22        }
23        return true;
24    }
25 }

 

[leetcode]680. Valid Palindrome II有效回文II(可至多删一原字符)

标签:fun   string   function   UNC   Plan   private   The   cte   cti   

原文地址:https://www.cnblogs.com/liuliu5151/p/9808300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!