[leetcode]680. Valid Palindrome II有效回文II(可至多删一原字符)
Posted 程序媛詹妮弗
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode]680. Valid Palindrome II有效回文II(可至多删一原字符)相关的知识,希望对你有一定的参考价值。
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(可至多删一原字符)的主要内容,如果未能解决你的问题,请参考以下文章
[leetcode-680-Valid Palindrome II]
Leetcode 680: Valid Palindrome II
leetcode-680-Valid Palindrome II
Python 解LeetCode:680. Valid Palindrome II