Thinking private school-sliding window

The essence of the sliding window algorithm is to maintain a window so that the value in the window meets the requirements of the title. It is mainly used to solve the problem of the sub-element of the array/string. The nested loop problem can be converted into a single loop problem, thereby reducing time the complexity.

Next, we explain how to use the sliding window to solve the problem and its thoughts through specific topics.

topic

LeetCode3. The longest substring without repeated characters

image

Ideas

The time complexity of the violent solution is relatively high, which can reach O(n^2) , so the sliding window method is adopted to reduce the time complexity

  • Define a map data structure storage (k, v), where the key value is a character, and the value value is the character position + 1, adding 1 means that the character position is not repeated until the next character position, and the map is constantly updated during the traversal process. Make its value value +1 at the position of the last occurrence.

  • We define the start position of the non-repeated substring as start and the end position as end

  • As end continues to traverse backwards, it will encounter the same situation as the characters in the map. At this time, there are two situations, one is that the position of the same character is in our sliding window, and the other is that the position of the same character is not in our sliding window.

    • If the same character is in our sliding window, ie start<map.get (same character), we need to update our start
    • If the same character is not in our sliding window, ie start>map.get (same character), we don’t need to update our stat
  • That is, start=max (start, map.get (same character))

  • At this point, we can ensure that there are no duplicate elements between our start and end

Code

  public int lengthOfLongestSubstring(String s) {
            int n = s.length(), ans = 0;
            Map<Character, Integer> map = new HashMap<>();
            for (int end = 0, start = 0; end < n; end++) {
                char alpha = s.charAt(end);
                if (map.containsKey(alpha)) {
                    start = Math.max(map.get(alpha), start);
                }
                ans = Math.max(ans, end - start + 1);
                map.put(s.charAt(end), end + 1);
            }
            return ans;
        }

to sum up

The key to the sliding window problem lies in figuring out the limits of left and right sliding, and when the window shrinks is the key point.

At last

  • If you feel that you are rewarded after reading it, I hope to give me a thumbs up. This will be the biggest motivation for me to update. Thank you for your support.
  • Welcome everyone to pay attention to my public account [Java Fox], focusing on the basic knowledge of java and computer, I promise to let you get something after reading it, if you don’t believe me, hit me
  • If you have different opinions or suggestions after reading, please comment and share with us. Thank you for your support and love.

——I am Chuhu, and I love programming as much as you.

image

Guess you like

Origin blog.csdn.net/issunmingzhi/article/details/111286112