LeetCode Problem No.3

2018-12-23  本文已影响0人  kino831143

Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.

Example 1:

Input: "abcabcbb"
Output: 3 
Explanation: The answer is "abc", with the length of 3. 

Example 2:

Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3. 
             Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

Solution 1:


class Solution {
public:
    //暴力搜索方法
    //计算字符串的每一个字串是否包含重复的字符,如果不包含,计算其长度,将最大值作为结果
    //TLE,超时
    int lengthOfLongestSubstring(string s) {
        int length = s.length();
        int result = 0;
        for(int i=0;i<length;i++)
        {
            for(int j=i+1;j<=length;j++)
            {
                char bag[256] = {0};
                bool flag = true;
                for(int k=i;k<j;k++)
                {
                    char ch = s[k];
                    if(bag[ch]==0)
                    {
                        bag[ch]=1;
                    }
                    else
                    {
                        flag =false;
                        break;
                    }
                }
                
                if(flag)
                {
                    if((j-i)>result)
                    {
                        result = j-i;
                    }
                }
            }
           
        }
            
        return result;
    }
};

Solution 2:


class Solution {
public:
    //滑动窗口的思想或队列的思想
    //从字符串左端开始遍历,如果不重复,将字符串入队
    //如果重复,也将其入队,从队列首开始遍历,一个个出队,直到找到与其重复的字符出队为止,
    //期间不断记录队列的最大值,这就是我们需要的结果
    //下面代码用滑动窗口实现,实际上是一个道理
    int lengthOfLongestSubstring(string s) {
        int left = 0;
        int rght = 0;
        int leng = s.length();
        int window = 0;
        int ans = 0;

        char bag[256] = {0};

        while (rght < leng) {
            char ch = s[rght];
            if (!bag[ch]) {
                bag[ch] = true;
                ++rght;

                ++window;
                if (window > ans) {
                    ans = window;
                }

                continue;
            }

            char prev;
            do {
                prev = s[left];
                bag[prev] = false;
                ++left;
                --window;
            } while (prev != ch);
        }

        return ans;
    }
};

Solution 3:


class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        vector<int> arr(256,-1);
        int ans=0;
        int left=0;
        //对于每个不重复的字串可以直接存储其最右端的位置,这样就不必循环遍历去找新的最左端的位置
        for(int i=0;i<s.length();i++)
        {
            //判断字符是否重复
            if(arr[s[i]] >=left)
            {
                //新的左端
                left= arr[s[i]]+1;                
            }
            
            arr[s[i]]=i;
            ans= max(ans,i-left+1);
                      
        }
        
      
        return ans;
    }
};
上一篇下一篇

猜你喜欢

热点阅读