zoukankan      html  css  js  c++  java
  • Leetcode(680) ;验证回文字符串 Ⅱ

    给定一个非空字符串 s,最多删除一个字符。判断是否能成为回文字符串。

    字符串只包含从 a-z 的小写字母。字符串的最大长度是50000。


     1 class Solution {
     2 public:
     3     bool validPalindrome(string s) {
     4         int len = s.size();
     5         int low = 0,high = len-1;
     6         while(low<high){
     7             if(s[low]!=s[high]){
     8                 //移除左侧             
     9                 int L=low+1,H=high;
    10                 while(L<=H){
    11                     if(s[L]!=s[H]) break;
    12                     L++;H--;
    13                     if(L>=H) return true;
    14                 }
    15 
    16                 //移除右侧
    17                 L=low,H=high-1;
    18                 while(L<=H){
    19                     if(s[L]!=s[H]) break;
    20                     L++;H--;
    21                     if(L>=H) return true;
    22 
    23                 }
    24                 
    25                 return false;
    26             }
    27                 
    28             else{
    29                 low++;high--;
    30             }
    31         }        
    32         return true;
    33      
    34         
    35     }
    36 };
  • 相关阅读:
    Thrift在微服务中的使用
    MySQL 必知必会
    IDEA 内使用 git
    分布式锁
    LeetCode 图
    LeetCode 位运算
    LeetCode 数组
    LeetCode 字符串
    LeetCode 哈希表
    LeetCode 栈和队列
  • 原文地址:https://www.cnblogs.com/lemon333333/p/10292221.html
Copyright © 2011-2022 走看看