欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

string,

来源: javaer 分享于  点击 8725 次 点评:70

string,


1、编号3 Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.


#include<iostream>
#include<string.h>
#include<string>
using namespace std;


int f1(string s,int i,int j){
int exist[256]={0};
int res=0;
exist[s[0]]=1;
while(j<s.length()){
    if(exist[s[j]]){
        res=max(res,j-i);
        memset(exist,0,sizeof(exist));
        i++;
        j=i+1;
        exist[s[i]]=1;
    }
    else{
        exist[s[j]]=1;
        j++;
    }


}
res =max(res,j-i);
return res;
}


int main(){
//statement
string s;
int i=0,j=1;
int length;
//init
cin>>s;


 length = f1(s,i,j);
cout<<length;


}




#include<iostream>
#include<string.h>
#include<string>
using namespace std;


int f1(string s,int i,int j){
int exist[256]={0};
int res=0;
exist[s[0]]=1;
while(j<s.length()){
    if(exist[s[j]]){
        res=max(res,j-i);
        while(s[i]!=s[j]){
            exist[s[i]]=0;
            i++;
        }
        i++;
        j++;
    }
    else{
        exist[s[j]]=1;
        j++;
    }


}
res =max(res,j-i);
return res;
}


int main(){
//statement
string s;
int i=0,j=1;
int length;
//init
cin>>s;


 length = f1(s,i,j);
cout<<length;


}



相关文章

    暂无相关文章

用户点评