#include <iostream>
#include <string>
#include <string.h>
using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
string tmp;
int nums[10000] = {0};
int i = 0;
int j = 0;
int tmplength = 0;
int length = 0;
for(int k = 0;k < s.length();k++){
j = k;
while(j < s.length()){
if(s[i] >= 97 && s[i] <= 122){
nums[s[i] - 97]++;
if(nums[s[i] - 97] > 1){
memset(nums,0,sizeof(nums));
tmplength = -1;
break;}
else{
i++;
tmplength++;}
}
else{
i++;
tmplength++;}
length = (length < tmplength) ? tmplength : length;
j++;
i = j;
}
tmplength = 0;
}
return length;
}
};
这个样例是三个空格,你的代码遇到三个空格得出的结果是3
我用c语言写的通过了:
int lengthOfLongestSubstring(char * s){
char*p,*q;
int l=0;
char n[256]={0};
for(p=s,q=s;*p&&*q;){
if(n[128+*q]){
n[128+*p++]--;
}else{
n[128+*q++]++;
if(l<q-p){
l=q-p;
}
}
}
return l;
}
错误答案反馈的截图能给下吗