首页 > 其他分享 >leetcode 647. 回文子串 js实现

leetcode 647. 回文子串 js实现

时间:2022-11-13 02:11:06浏览次数:73  
标签:子串 js length let 647 字符串 leetcode 回文

给你一个字符串 s ,请你统计并返回这个字符串中 回文子串 的数目。

回文字符串 是正着读和倒过来读一样的字符串。

子字符串 是字符串中的由连续字符组成的一个序列。

具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被视作不同的子串。

 

示例 1:

输入:s = "abc"
输出:3
解释:三个回文子串: "a", "b", "c"

示例 2:

输入:s = "aaa"
输出:6
解释:6个回文子串: "a", "a", "a", "aa", "aa", "aaa"

 

提示:

  • 1 <= s.length <= 1000
  • s 由小写英文字母组成

https://leetcode.cn/problems/palindromic-substrings/description/

 

/**
 * @param {string} s
 * @return {number}
 */
//  中心扩展法
var countSubstrings = function(s) {
    let ans = 0;
    let length = s.length;
    // 总的中心点=length*2-1 (即单个字符各一种+两两字符为一种)
    for(let center=0;center<length*2-1;center++){
        let left = center/2;
        // 右侧指针可能跟左侧是同一个,也可能是左右各一个
        let right = left+center%2;
        while(left>=0 && right <s.length && s.charAt(left)===s.charAt(right)){
            ans++;
            left--;
            right++;
        } 
    }
    return ans;
};
// 动态规划
var countSubstrings = function(s) {
    let ans = 0;
    let length = s.length;
    let dp = new Array(length).fill(0).map((item)=>new Array(length));
    // 外层循环相当于右侧指针,i 
    // 内层循环相当于左侧指针,j
    // j<=i
    for(let i=0;i<length;i++){
        for(let j=0;j<=i;j++){
            if(s.charAt(i)===s.charAt(j)&&(i-j<2||dp[j+1][i-1])){
                dp[j][i]=true;
                ans++;
            }
        }
    }
    return ans;
};

参考答案

标签:子串,js,length,let,647,字符串,leetcode,回文
From: https://www.cnblogs.com/beileixinqing/p/16885303.html

相关文章

  • LeetCode 665. 非递减数列
    classSolution{public:boolcheckPossibility(vector<int>&nums){intn=nums.size();for(inti=0;i<n-1;i++){intx=nums[i......
  • windows安装nodejs
    安装nodejs1.官网地址:http://nodejs.cn/download![image](https://img2022.cnblogs.com/blog/2961302/202211/2961302-20221112233115575-1530845639.jpg)2.选择......
  • LeetCode刷题记录.Day13
    四数之和18.四数之和-力扣(LeetCode)classSolution{public:vector<vector<int>>fourSum(vector<int>&nums,inttarget){vector<vector<int>>res......
  • Vue.js -- 动态组件&异步组件
    动态组件根据数据的变化,动态切换组件的显示。点击切换组件首先定义两个子组件//子组件app.component('myInput',{template:`......
  • #yyds干货盘点# LeetCode 腾讯精选练习 50 题:最小栈
    题目:设计一个支持push,pop,top操作,并能在常数时间内检索到最小元素的栈。实现MinStack类:MinStack()初始化堆栈对象。voidpush(intval)将元素val推入堆栈。voidpop......
  • leetcode-888-easy
    FairCandySwapAliceandBobhaveadifferenttotalnumberofcandies.YouaregiventwointegerarraysaliceSizesandbobSizeswherealiceSizes[i]isthenum......
  • JSP—El表达式,java脚本,java表达式,jstl标签库
    jsp简介JSP(全称JavaServerPages)是由SunMicrosystems公司倡导和许多公司参与共同创建的一种使软件开发者可以响应客户端请求,而动态生成HTML、XML或其他格式文档的We......
  • js中+的使用
    1.+someTypeId,把string的id转换为number类型2. 3.+startTime(startTime为ThuMar05202008:00:00GMT+0800(中国标准时间)这样的形式)得到的也是毫秒 ......
  • leetcode-1486-easy
    XOROperationinanArrayYouaregivenanintegernandanintegerstart.Defineanarraynumswherenums[i]=start+2*i(0-indexed)andn==nums.length......
  • error in anyjson setup command: use_2to3 is invalid.问题解决
    报错errorinanyjsonsetupcommand:use_2to3isinvalid.解决因为在setuptools58之后的版本已经废弃了use_2to3pipinstallsetuptools==57.5.0......