算法珠玑——滑动窗口(1)

算法珠玑——滑动窗口(1)

https://leetcode-cn.com/problems/longest-substring-without-repeating-characters

这题以下代码两个平均都没下过90%
多试几百次,也许能双100%

    int lengthOfLongestSubstring(string s) {
        unsigned int longest = 0;
        unsigned char arr[95];
        fill(arr, arr + 95, 0);

        for (unsigned int left = 0, right = 0, len = s.size(); right < len;++right)
        {
            ++arr[s[right] - ' '];
            while (arr[s[right] - ' '] == 2)
            {
                --arr[s[left] - ' '];
                ++left;
            }
            longest = longest < right - left + 1 ? right - left + 1 : longest;
        }
        return longest;
    }

lon = max(lon, right-left+1);处可以优化吗?答案是很难。
不使用s.size()会比较快。
具体的细节明天有时间再讲。(12月5日 10:31留)

When to Use the Sliding Window Approach?

The following are some of the most important indications that a sliding window approach might be appropriate:

  • Your problem involves data structures such as arrays and strings. An image is basically a multidimensional array. (use index)
  • You want to find a subrange involving the longest, shortest, or goal values in that array or string.
  • Conceptually, it revolves around ideas like the longest or shortest sequence of something that meets a specific requirement.

Steps

  • Determine the required window size.
  • Begin with the data structure’s first window.(leftright0)
  • In a loop, slide the window by 1 and continue calculating the result window by window.

更新!

8月11日更新

    int lengthOfLongestSubstring(string s) {
        unsigned int longest = 0;
        // 根据目标的类型限制,构造一个高性能的map
        unsigned char exists[95];
        // 高性能初始化
        fill(exists, exists + 95, 0);

        // base case: inital window [0,0], len
        
        // induction step: ++right, ++exists in slibing window 
        for (unsigned int left = 0, right = 0, len = s.size(); right < len;++right)
        {
            ++exists[s[right] - ' '];
            // sub base case: exists[s[right] - ' '] == 1(will set on right == left)
            while (exists[s[right] - ' '] == 2)
            {
                // sub induction step: ++left, and --exists in slibing window
                --exists[s[left] - ' '];
                ++left;
            }
            // induction step: get max (longest)
            longest = longest < right - left + 1 ? right - left + 1 : longest;
        }
        // end step: jump out when right index out
        return longest;
    }
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int longest = 0;
        char exists[95];
        fill(exists, exists + 95, 0);

        // base: left==right==0
        // induction(right < s.size()): ++exists[s[right] - ' '], ++right
        for (int left = 0, right = 0; right < s.size(); ++right)
        {
            ++exists[s[right] - ' '];
            // sub base: exists[s[right] - ' '] == 1
            // sub induction(exists[s[right] - ' '] == 2): --exists[s[left] - ' '], ++left
            while(exists[s[right] - ' '] == 2)
            {
                --exists[s[left] - ' '];
                ++left;
            }
            // end: max longest
            longest = max(longest, right - left + 1);
        }
        return longest;
    }
};
posted @ 2021-12-05 22:32  千心  阅读(42)  评论(0)    收藏  举报