首页 > 其他分享 >有效回文 II

有效回文 II

时间:2024-05-17 20:51:46浏览次数:17  
标签:return int ++ II 有效 str -- check 回文

题目链接:

来自罗勇军《算法竞赛》尺取法一节的习题。

思路:反向扫描,设双指针为 \(i\) 和 \(j\)。

if (s[i] == s[j]) i++, j--;

否则的话要么删除 \(s[i]\) 或者删除 \(s[j]\),看剩下的字符串是否是回文串。

class Solution {
public:
    bool check(string str, int l, int r) {
        while (l < r) {
            if (str[l] != str[r]) return false;
            l++, r--;
        }
        return true;
    }
    bool validPalindrome(string &s) {
        int n = s.size();
        for (int i = 0, j = n - 1; i < j; ) {
            if (s[i] == s[j]) i++, j--;
            else {
                return check(s, i, j - 1) || check(s, i + 1, j);
            }
        }
        return true;
    }
};

标签:return,int,++,II,有效,str,--,check,回文
From: https://www.cnblogs.com/pangyou3s/p/18198594

相关文章

  • 51模拟IIC-页读写操作
    51代码页读写IIC--模拟IIC#include<reg52.h>#include<intrins.h>sbitSDA=P0^0;sbitSCL=P0^1;sbitLED=P2^0;unsignedcharcodetable[]={0x1c,0X3B,0X2C,0X2D,0X5A,0X5C,0XC5,0X5b};voiddelayms(unsignedintt){unsignedinti,j;fo......
  • 40. 组合总和 II(leetcode)
    https://leetcode.cn/problems/combination-sum-ii/description/classSolution{List<List<Integer>>res=newArrayList<>();LinkedList<Integer>path=newLinkedList<>();intsum=0;publicList<List&l......
  • 抽象代数课程笔记 III —— 域论、伽罗瓦理论
    持续更新。\(\newcommand{\a}{\alpha}\newcommand{\b}{\beta}\newcommand{\D}{\Delta}\newcommand{\eps}{\varepsilon}\newcommand{\ph}{\varphi}\newcommand{\t}{\theta}\newcommand{\la}{\lambda}\newcommand{\si}{\sigma}\newcommand{\d}{......
  • 抽象代数课程笔记 III —— 域论、伽罗瓦理论
    持续更新。\(\newcommand{\a}{\alpha}\newcommand{\b}{\beta}\newcommand{\D}{\Delta}\newcommand{\eps}{\varepsilon}\newcommand{\ph}{\varphi}\newcommand{\t}{\theta}\newcommand{\la}{\lambda}\newcommand{\si}{\sigma}\newcommand{\d}{......
  • 研发数据在企业内部多重传输场景,怎样才能有效响应?
    研发数据因行业不同包含的种类各异,主要有设计和仿真数据、研发投入、进展和成果数据、生产过程数据、维护和保养数据、质量数据等,企业研发数据对企业而言具有至关重要的意义。特别是以研发为核心业务及定位的企业,如半导体IC设计、生物制药、科研单位等,研发数据就是其最核心的数......
  • 代码随想录算法训练营第第八天 | 344.反转字符串 、541. 反转字符串II、卡码网:54.替
    344.反转字符串建议:本题是字符串基础题目,就是考察reverse函数的实现,同时也明确一下平时刷题什么时候用库函数,什么时候不用库函数题目链接/文章讲解/视频讲解:https://programmercarl.com/0344.反转字符串.html/***@param{character[]}s*@return{void}Donotret......
  • 264 ugly number II 丑数
    问题描述Anuglynumberisapositiveintegerwhoseprimefactorsarelimitedto2,3,and5.Givenanintegern,returnthenth*uglynumber*.解释:一个丑数是一个正整数,其公因子只有2,3,5。给定数字n,求第n个丑数案例:Input:n=10Output:12Explanation:[1,2,......
  • 文章详情URL不使用ID用slug和uuiid代替
    在Laravel中,可以通过使用slug或UUID来展示文章详情和文章列表。这样可以提高URL的可读性和安全性。以下是实现方法和代码示例:方法一:使用Slug数据库字段首先,在posts表中添加slug字段:Schema::table('posts',function(Blueprint$table){$table->string('sl......
  • 2024-05-15:用go语言,考虑一个整数 k 和一个整数 x。 对于一个数字 num, 在其二进制表示
    2024-05-15:用go语言,考虑一个整数k和一个整数x。对于一个数字num,在其二进制表示中,从最低有效位开始,我们计算在x,2x,3x等位置处设定位的数量来确定其价值。举例说明,若对于x=1,num=13,则二进制表示为000001101,对应的价值为3。又如,当x=2,num=13,二进制表示依然为000001101,但对......
  • 本地SSL证书过期 输入命令在IIS自动生成
    C:\Users\win10-zhiyong>dotnetdev-certshttps--trustTrustingtheHTTPSdevelopmentcertificatewasrequested.Aconfirmationpromptwillbedisplayedifthecertificatewasnotpreviouslytrusted.Clickyesontheprompttotrustthecertificate.Suc......