# LeetCode------Longest Substring Without Repeating Characters

177人阅读 评论(0)

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.

public static int lengthOfLongestSubstring(String s) {
int maxLen = 0;
int currentLen = 0;
int start = 0;
int hash[] = new int[256];
for(int i=0; i<s.length(); i++){
int lastPost = hash[ s.charAt(i) ];
if(lastPost == 0){
currentLen++;
}else{
for(int j=start; j< lastPost; j++)
hash[ s.charAt(j) ] = 0;
start = lastPost ;
currentLen = i - start + 1;
}
hash[s.charAt(i)] = i+1;
if(maxLen < currentLen) maxLen = currentLen;
}
return maxLen;
}

    public static int lengthOfLongestSubstring(String s) {
Set<String> set=new HashSet<String>();
char[] strChar=s.toCharArray();
int maxlength=0;
int indexFlag=0;
for(int i=0;i<strChar.length;i++){
if(set.size()==0){
indexFlag=i;
}
if(set.contains(String.valueOf(strChar[i]))){
set.clear();
i=indexFlag+1;
indexFlag=i;
}else{
}
int tempSize=set.size();
if(tempSize>maxlength){
maxlength=tempSize;
}
}
return maxlength;
}

    public static int lengthOfLongestSubstring(String s) {
if(""==s||null==s){
return 0;
}
char[] strArr=s.toCharArray();
Hashtable<String, Integer> table=new Hashtable<String, Integer>();
int maxSize=0;
int tempintSize=0;
int tempindex=0;
for(int i=0;i<strArr.length;i++){
Integer value= table.get(String.valueOf(strArr[i]));
if(value==null&&table.size()==0){
table.put(String.valueOf(strArr[i]),i);
tempindex=i;
}else if(value==null&&table.size()!=0){
table.put(String.valueOf(strArr[i]),i);
}
else{
table.clear();
i=tempindex+1;
//              System.out.println(i);
table.put(String.valueOf(strArr[i]),i);
tempindex=i;
}
tempintSize=table.size();
if(tempintSize>maxSize){
maxSize=tempintSize;
}
}
return maxSize;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：13993次
• 积分：668
• 等级：
• 排名：千里之外
• 原创：54篇
• 转载：5篇
• 译文：0篇
• 评论：2条
阅读排行
评论排行