leetcode每日打卡 回文子串&&两个数组的交集 II&&回文数&&最长公共前缀

114 阅读1分钟

647. 回文子串

leetcode-cn.com/problems/pa…

  • 中心扩展法
/**
 * @param {string} s
 * @return {number}
 */
var countSubstrings = function (s) {
    let count = 0
    for (let start = 0;start<s.length;start++) {
        //  分类思想 转换为偶数个和奇数个 大问题转子问题 
        // 偶数
        calcExpandCount(start,start+1)
        // 奇数
        calcExpandCount(start,start)
    }
    function calcExpandCount(start,end){
        while(start>=0&&end<s.length&&s[start]===s[end]){
            count++
            start--
            end++
        }
    }
    return count
};
  • 动态规划
/**
 * @param {string} s
 * @return {number}
 */
var countSubstrings = function (s) {
    const len = s.length
    const dp = []
    for (let i = 0; i < len; i++) {
        dp[i] = []
    }
    let count = 0
    for (let start = s.length-1; start >=0; start--) {
        for (let end = start; end <s.length; end++) {
            if (start === end) {
                dp[start][end] = true
                count ++ 
                continue
            }
            if (s[start] === s[end]) {
                if (start + 1 === end) {
                    dp[start][end] = true
                    count++
                } else {
                    dp[start][end] = dp[start + 1][end - 1]
                    if(dp[start][end]){
                        count ++
                    }
                }
            } else {
                dp[start][end] = false
            }

        }
    }
    return count
};

350. 两个数组的交集 II

leetcode-cn.com/problems/in…

  • 排序之后双指针
/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number[]}
 */
var intersect = function(nums1, nums2) {
    nums1.sort((a,b)=>a-b)
    nums2.sort((a,b)=>a-b)
    const res = []
    for(let i=0,j=0;i<nums1.length&&j<nums2.length;){
        if(nums1[i] === nums2[j]){
            res.push(nums1[i])
            i++
            j++
        }else{
            if(nums1[i]<nums2[j]){
                i++
            }else{
                j++
            }
        }
    }
    return res
};

9. 回文数

leetcode-cn.com/problems/pa…

  • 取得一个整数的每一位的数字 这个数对10取余然后再除以10
/**
 * @param {number} x
 * @return {boolean}
 */
var isPalindrome = function(x) {
    if(x<0){
        return false
    }
    if(x<10){
        return true
    }
    const orginTraget = x
    const res = []
    let reverseTarget = null
    while(x>=1){
        res.push(x%10)
        x = Math.floor(x/10)
    }
    for(let i=0,j=res.length-1;i<res.length;i++,j--){
        reverseTarget += res[i]*Math.pow(10,j)
    }
    return orginTraget === reverseTarget
    
};

14. 最长公共前缀

leetcode-cn.com/problems/lo…

/**
 * @param {string[]} strs
 * @return {string}
 */
var longestCommonPrefix = function(strs) {
    if(!strs.length){
        return ""
    }
    let flag = strs[0]
    for(let i=1;i<strs.length;i++){
        while(flag.length &&strs[i].indexOf(flag)!==0){
            flag = flag.slice(0,flag.length-1)
        }
    }
    return flag
};