[LeetCode]3.Longest Substring Without Repeating Characters,leetcoderepeating

來源:互聯網
上載者:User

[LeetCode]3.Longest Substring Without Repeating Characters,leetcoderepeating
【題目】

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.

【分析】

從左至右掃描,當遇到重複字母時,以上一個重複字母的index+1,作為新的搜尋起始位置,直到最後一個字母。

【代碼】

/**********************************   日期:2015-01-20*   作者:SJF0115*   題目: 3.Longest Substring Without Repeating Characters*   網址:https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/*   結果:AC*   來源:LeetCode*   時間複雜度:O(n)*   空間複雜度:O(1)*   部落格:**********************************/#include <iostream>#include <cstring>using namespace std;class Solution {public:    int lengthOfLongestSubstring(string s) {        int len = s.length();        if(len <= 1){            return len;        }//if        bool visited[256];        // 初始化        memset(visited,false,sizeof(visited));        // 計算        int max = 0,cur = 0,start = 0;        for(int i = 0;i < len;){            // 沒有元素和該元素重複            if(!visited[s[i]]){                visited[s[i]] = true;                ++cur;                ++i;            }//if            else{                // 更新最大長度                if(cur >= max){                    max = cur;                }//if                cur = 0;                memset(visited,false,sizeof(visited));                int index = 0;                // 尋找之前重複元素的下一個元素的下標                for(int j = start;j < i;++j){                    if(s[i] == s[j]){                        index = j+1;                        break;                    }//if                }//for                start = index;                i = index;            }        }//for        if(cur > max){            max = cur;        }//if        return max;    }};int main(){    Solution solution;    string str("abbacdafg");    int result = solution.lengthOfLongestSubstring(str);    // 輸出    cout<<result<<endl;    return 0;}

【代碼二】

class Solution {public:    int lengthOfLongestSubstring(string s) {        int len = s.length();        if(len <= 1){            return len;        }//if        // 記錄上次出現的位置        int last[256];        // 初始化        memset(last,-1,sizeof(last));        // 計算        int max = 0,cur = 0,start = 0;        for(int i = 0;i < len;++i){            // 有元素和該元素重複            if(last[s[i]] >= 0){                if(cur > max){                    max =cur;                }//if                // 當遇到重複字母時,以上一個重複字母的index+1,                // 作為新的搜尋起始位置                i = last[s[i]] + 1;                cur = 0;                memset(last,-1,sizeof(last));            }//if            ++cur;            last[s[i]] = i;        }//for        if(cur > max){            max = cur;        }//if        return max;    }};



【分析三】

對上一個思路繼續進行最佳化。

在上一個思路中當遇到重複字母時,以上一個重複字母的index+1,作為新的搜尋起始位置。上一個重複字母的下一個字母到當前字母可以不用重複遍曆。

在本思路中搜尋起點不是上一個重複字母的index+1而是當前字母。

通過記錄上一個Lonest Substring Without Repeating Characters的起點,來計算出在當前字母時的長度。

【代碼三】

class Solution {public:    int lengthOfLongestSubstring(string s) {        int len = s.length();        if(len <= 1){            return len;        }//if        // 記錄上次出現的位置        int last[256];        // 初始化        memset(last,-1,sizeof(last));        // 計算        int max = 0,cur = 0,start = 0;        for(int i = 0;i < len;++i){            int pos = (int)s[i];            // 有元素和該元素重複            if(last[pos] >= start){// not last[pos] >= 0                // 更新長度                cur = cur - (last[pos] - start);                // 更新起點                start = last[pos] + 1;            }//if            else{                ++cur;            }            // 更新字元位置            last[pos] = i;            if(cur > max){                max =cur;            }//if        }//for        return max;    }};


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.