思路
用到的东西和第一题比较像,使用的是unordered_map,以此保证复杂度为O(n);
用check依次查找每个字符,如果unordered_map中未找到该字符则存入;若找到则检测最大数max是否小于当前计数count并改动,unordered_map清空,并重开头的指针下一位进行新的查找。
但不知为何提交时其输入"amqpcsrumjjufpu"会报错,我使用的时候并没有。。。
如果有大佬知道原因请指出来,非常感谢!
代码(自用与leetcode提交两种)
#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') {