LeetCode 3. Longest substring without repeating characters

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

posis the position s[i]of the last .

leftis the starting index (leftmost index) of the longest distinct substring s[i-1]ending .

(1) If it leftis to posthe right of , then leftdoes not need to updated.

insert image description here

(2) If leftis to posthe left of , then leftupdate to pos + 1.

insert image description here

(3) If leftand posare equal, then leftupdate to pos + 1.

insert image description here

The C++ code is as follows:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int len = s.length();
        if (len == 0)   return 0;

        unordered_map<char, int> mp; // 用来保存每个字符上一次出现的位置

        mp[s[0]] = 0; // 预处理:字符s[0]上一次出现的位置是0
        int left = 0; // 预处理:以字符s[0]结尾的最长不重复字符串的开始索引是0
        int res = 1;

        for (int i = 1; i < len; i++) {
            if (mp.count(s[i])) {
                int pos = mp[s[i]];
                if (left <= pos) {
                    left = pos + 1;
                }
            }
            mp[s[i]] = i;
            res = max(res, i - left + 1);
        }
        
        return res;
    }
};

Because it sonly consists of English letters, numbers, symbols and spaces, an integer array can be used instead of a hash table.

The C++ code is as follows:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int len = s.length();
        if (len == 0)   return 0;

        int mp[128]; // 用来保存每个字符上一次出现的位置
        memset(mp, -1, sizeof mp);

        mp[s[0]] = 0; // 预处理:字符s[0]上一次出现的位置是0
        int left = 0; // 预处理:以字符s[0]结尾的最长不重复字符串的开始索引是0
        int res = 1;

        for (int i = 1; i < len; i++) {
            int pos = mp[s[i]];
            if (left <= pos) {
                left = pos + 1;
            }
            mp[s[i]] = i;
            res = max(res, i - left + 1);
        }
        return res;
    }
};

Unordered_map supplementary knowledge:

unordered_map is overloaded to []use subscripts to search for values.

Note: When using []to search for a value, if the corresponding key does not exist, one will be inserted automatically, and the corresponding value will call its default constructor.

Therefore, if you are not sure whether a key exists, you must first use unordered_map::find() to find it, and then search for the corresponding value, otherwise the entire unordered_map will be changed inadvertently.

#include<bits/stdc++.h>

using namespace std;

int main() {

    unordered_map<char, int> mp;

    cout << mp['A'] << endl; // 0

    cout << mp.size() << endl; // 1

    return 0;
}

Summarize

This is the end of writing, and I put a small benefit at the end of the article. The following is a learning idea and direction about java development that I have sorted out during the learning process. In Internet development, the most important thing is to learn technology well, and learning technology is a slow, long and arduous road. You can’t rely on passion for a while, and you can’t learn it well by staying up for a few days and nights. You must develop the habit of studying hard. More need for accurate learning direction to achieve effective learning effect.

Since there is a lot of content, only a general outline is put on it. If you need a more detailed learning mind map, click on my Gitee to get it .
There is also a full set of advanced java video tutorials java advanced architect video + information + code + interview questions!

All aspects of java advanced practice technical materials, and there are also technical experts to discuss and exchange problems to solve.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324085597&siteId=291194637