鍍金池/ 教程/ Java/ Longest Substring Without Repeating Characters
ZigZag Conversion(Z型轉(zhuǎn)換)
String to Integer (atoi)(轉(zhuǎn)換到整型)
Generate Parentheses(生成括號(hào))
Longest Common Prefix(最長(zhǎng)公共前綴)
Remove Duplicates from Sorted Array(從已排序數(shù)組中移除重復(fù)元素)
Swap Nodes in Pairs(交換序列中的結(jié)點(diǎn))
Valid Parentheses(有效的括號(hào))
4 Sum(4 個(gè)數(shù)的和)
3 Sum(3 個(gè)數(shù)的和)
3 Sum Closest(最接近的 3 個(gè)數(shù)的和)
Reverse Nodes in k-Group(在K組鏈表中反轉(zhuǎn)結(jié)點(diǎn))
Regular Expression Matching (正則表達(dá)式匹配)
Two Sum
Container With Most Water(最大水容器)
Merge Two Sorted Lists(合并兩個(gè)已排序的數(shù)組)
Remove Nth Node From End of List(從列表尾部刪除第 N 個(gè)結(jié)點(diǎn))
String to Integer (atoi)(轉(zhuǎn)換到整型)
Palindrome Number(回文數(shù))
Longest Substring Without Repeating Characters
Roman to Integer(羅馬數(shù)到整型數(shù))
Integer to Roman(整型數(shù)到羅馬數(shù))
Reverse Integer(翻轉(zhuǎn)整數(shù))
Merge k Sorted Lists(合并 K 個(gè)已排序鏈表)
Implement strStr()(實(shí)現(xiàn) strStr() 函數(shù))
Longest Palindromic Substring(最大回文子字符串)
Add Two Numbers
Letter Combinations of a Phone Number(電話號(hào)碼的字母組合)

Longest Substring Without Repeating Characters

翻譯

給定一個(gè)字符串,找出其沒(méi)有重復(fù)字符的最大子序列的長(zhǎng)度?! ?例如,“abcabcbb”的無(wú)重復(fù)字符的最大子序列是“abc”,它的長(zhǎng)度是 3。   “bbbbb”的最大子序列是“b”,它的長(zhǎng)度是 1。

原文

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.    

Code 1(C++)

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int len = s.Length;
        int head = 0, index = 0, maxLen = 0;
        bool[] exist = new bool[256];
        for (int i = 0; i < exist.Length; i++)  
            exist[i] = false;  
        while (index < len){
            if (exist[s[index]]){
                 maxLen = Math.Max(maxLen, index - head);
                 while (s[head] != s[index]){
                     exist[s[head]] = false;
                     head++;
                 }
                 head++; index++;
             }
             else{
                 exist[s[index]] = true;
                 index++;
             }
         }
         maxLen = Math.Max(maxLen, len - head);
         return maxLen; 
    }
};

Code 1 C

public class Solution
{
    public int LengthOfLongestSubstring(string s)
    {
        int len = s.Length;
        int head = 0, index = 0, maxLen = 0;
        bool[] exist = new bool[256];
        for (int i = 0; i < exist.Length; i++)
            exist[i] = false;
        while (index < len)
        {
            if (exist[s[index]])
            {
                maxLen = Math.Max(maxLen, index - head);
                while (s[head] != s[index])
                {
                    exist[s[head]] = false;
                    head++;
                }
                head++; index++;
            }
            else
            {
                exist[s[index]] = true;
                index++;
            }
        }
        maxLen = Math.Max(maxLen, len - head);
        return maxLen;
    }
}

Code 1 Java

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int len=s.length();
        int head=0,index=0,maxLen=0;
        boolean[] exist=new boolean[256];
        for(boolean e : exist)
            e=false;
        while(index<len){
            if(exist[s.charAt(index)]){
                maxLen=Math.max(maxLen, index-head);
                while(s.charAt(head)!=s.charAt(index)){
                    exist[s.charAt(head)]=false;
                    head++;
                }
                head++; index++;
            }
            else{
                exist[s.charAt(index)]=true;
                index++;
            }
        }
        return maxLen= Math.max(maxLen,len-head);
    }
}

Code 2 C++


static  int lengthOfLongestSubstring(string s)
{
    int ascii[256];
    for(int i=0; i<256; i++) ascii[i] = -1;
    int start = 0, ans = 0;
    int i;
    for(i=0; i<s.size(); i++){
       if( -1 != ascii[ s[i] ] ){
            if(ans < i-start) ans = i-start;
            for(int j=start; j<ascii[ s[i] ]; j++)
                 ascii[j] = -1;
            if(ascii[s[i]] + 1 > start )
                 start = ascii[s[i]] +1;
       }
       ascii[s[i]] = i;
    }
    if(ans < i-start) ans = i-start;
    return ans;
}
```