何文字leetcode 3.最長の部分文字列Cを繰り返さない++

思考

使用より複雑性を確保するために、最初の質問と、unordered_mapを使用して、のようなものがO(n)は、
各文字を見つけるためにチェック、文字がunordered_map堆積物中に見つからない場合、検索が検出された場合最大の最大の数は少なく、現在のカウントとカウント数の変化よりも、unordered_mapの新しい外観は、両手で始まる、クリア。

しかし、私は使用しませんでした、なぜ彼らの入力「amqpcsrumjjufpu」を提出文句を言うだろうかわかりません。
その理由を知っている兄がある場合は、それを指摘してください、どうもありがとうございました!

コード(個人的な使用とleetcodeための2つの種類の提出)

#include <iostream>
#include <unordered_map>
using namespace std;
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        unordered_map<char, int> hash;
        char* p;
        char check;
        p = &s[0];
        check = *(p + 1);
        char* p_max = p;
        int count = 0;
        int max = 0;
        if ((*p) != '\0') {
            count = 1;
            max = 1;
            hash[*p] = 1;
        }
        while ((*p) != '\0') {
            if (hash.find(check) == hash.end()&&check != '\0') {
            //判断unordered_map中是否有该字母以及check是否到头
                hash[check] = 1;
                count++;
                check = *(p + count);
            }
            else {
                if (count > max) {
                    max = count;
                    p_max = p;
                }
                count = 1;
                hash.clear();
                p = p + 1;
                hash[*p] = 1;
                check = *(p + 1);
            }
        }
        cout << max << endl;
        return 0;
    }
};
int main()
{
    Solution S;
    string s;
    cin >> s;
    S.lengthOfLongestSubstring(s);
    return 0;
`}

```cpp
//leetcode
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        unordered_map<char, int> hash;
        char* p;
        char check;
        p = &s[0];
        check = *(p+1);
        char* p_max = p;
        int count = 0;
        int max = 0;
        if ((*p)!='\0') {
            count = 1;
            max = 1;
            hash[*p] = 1;
        }
        while ((*p)!='\0') {
            if (hash.find(check) == hash.end()&&check!='\0') {
                hash[check] = 1;
                count++;
                check = *(p + count);
                }
                else {
                    if (count > max) {
                        max = count;
                        p_max = p;
                        }
                        count = 1;
                        hash.clear();
                        p = p + 1;
                        hash[*p] = 1;
                        check = *(p + 1);
                    }
            }
            return max;
            }
};
リリース9件のオリジナルの記事 ウォンの賞賛1 ビュー157

おすすめ

転載: blog.csdn.net/qq_37782336/article/details/104449405