常见题目

image.png
[](https://leetcode-cn.com/probl...

leetcode 239 剑指 Offer 59 - I. 滑动窗口的最大值

        const nums = [1, 3, -1, -3, 5, 3, 6, 7];
        const k = 3;
        function findMax(nums, k) {
            const result = []
            let temp = nums[0]
            for (let i = 0; i < nums.length; i++) {
                for (let j = 0; j < k; j++) {
                    if (nums[i + j] > temp) {
                        temp = nums[i + j]
                    }
                }
                result.push(temp)
                temp = nums[i]
                if (i == nums.length - k) {
                    break;
                }
            }
            return result
        }
        const result = findMax(nums, k)
        console.log('result', result);

3

    var s = "abcabcbb"; //3
    // var s = 'abbcdea' //5
    var lengthOfLongestSubstring = function (s) {
      let l = 0; //左指针起始位置
      let res = 0; //最大窗口长度
      const map = new Map()
      for (let r = 0; r < s.length; r += 1) { //r为右指针
        let n = s[r]
        let hasr = map.has(s[r])
        let getsr = map.get(s[r])
        if (map.has(s[r]) && map.get(s[r]) >= l) {  //map.get(s[r]) >= l左指针在滑动窗口里面
          l = map.get(s[r]) + 1 //如果有重复的值,左指针向前移动一位
        }
        res = Math.max(res, r - l + 1)  //右指针减去右指针+1得到滑动窗口的长度
        map.set(s[r], r)
      }
      return res;
    };
    var result = lengthOfLongestSubstring(s)
    console.log('result', result)

76最小覆盖子串

给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" 。

注意:如果 s 中存在这样的子串,我们保证它是唯一的答案。

输入:s = "ADOBECODEBANC", t = "ABC"
输出:"BANC"
输入:s = "a", t = "a"
输出:"a"
    <script>
        var s = "ADOBECODEBANC", t = "ABC";
    </script>
    <script>
        const minWindow = function (s, t) {
            let l = 0;
            let r = 0;
            const need = new Map()
            for (let c of t) {
                if (need.has(c)) {
                    need.set(c, need.get(c) + 1)
                } else {
                    need.set(c, 1)
                }
            }
            let needType = need.size;
            let res = ''
            while (r < s.length) { //r++来增加滑动窗口,从而找到符合条件的子串
                const c = s[r]
                if (need.has(c)) {
                    need.set(c, need.get(c) - 1)
                    if (need.get(c) === 0) {
                        needType--
                    }
                }
                while (needType == 0) { //needType=== 0 此时的窗口中是包含符合条件的子串,l++来缩小滑动窗口
                    const newRes = s.substring(l, r + 1)
                    if (!res || newRes.length < res.length) {
                        res = newRes
                    }
                    const c2 = s[l]
                    if (need.has(c2)) {
                        need.set(c2, need.get(c2) + 1)
                        if (need.get(c2) === 1) {
                            needType++
                        }
                    }
                    l++
                }
                r++
            }
            return res
        }
    </script>
    <script>
        var result = minWindow(s, t)
        console.log('result', result);
    </script>

渣渣辉
1.3k 声望147 粉丝