zoukankan      html  css  js  c++  java
  • LeetCode 3 Longest Substring Without Repeating Characters

    题目大意是给一个字符串,需要求出最大的子串(子串中没有任何元素是相同的)

    Examples:

    Given "abcabcbb", the answer is "abc", which the length is 3.

    Given "bbbbb", the answer is "b", with the length of 1.

    Given "pwwkew", 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.

    最初想的比较简单,O(n^2)

    class Solution {
    public:
        map<char,int>mp;
        int lengthOfLongestSubstring(string s) {
            int len=s.length();
            if(len==1)
            {
                return 1;
            }
            int temp=0,max=0,i,j;
            for(i=0;i<len;i++)
            {
                mp[s[i]]=1;
                for(j=i+1;j<len;j++)
                {
                    if(mp[s[j]]==0)
                        mp[s[j]]=1;
                    else
                    {
                        break;
                    }
                }
                temp=j-i;
                mp.clear();
                if(temp>max)
                {
                    max=temp;
                }
            }
            return max;
        }
    };

    其实这个题目有点类似于双指针了。两个指针 i , j表示从i开始到j都是没有相同单元的。这里我们可以用一个set容器来维护。如果到了某一个j,发现出现相同的元素,则将i向前移,同时删除set中对应i节点的内容

    class Solution {
    public:
        set<char>p;
        int lengthOfLongestSubstring(string s) {
            int len=s.length();
            int i=0,j=0,ans=0;
            while(i<len&&j<len)
            {
                if(p.count(s[j])==0)
                {
                    p.insert(s[j++]);
                    ans=max(ans,j-i);
                }
                else
                {
                    p.erase(s[i++]);
                }
            }
            return ans;
        }
    };

    我们发现其实还可以继续降低复杂度,因为当我们发现s[j] 已经出现在前面时,我们是从i一个个寻找的。而我们可以用一个map记录与s[j]相等的j'的位置

    class Solution {
    public:
        map<char,int>mp;
        int lengthOfLongestSubstring(string s) {
            int len=s.length();
            int i=0,j=0,ans=0;
            for(j=0,i=0;i<len&&j<len;j++)
            {
                if(mp.count(s[j])!=0)
                {
                    i=max(i,mp[s[j]]);
                }
                ans=max(ans,j-i+1);
                mp[s[j]]=j+1; //yao从下一个不相同的元素开始,所以+1
            }
            return ans;
        }
    };
  • 相关阅读:
    容器适配器
    告别格式工厂的视频格式转换方法(mac版 命令行)
    配置CentOS7的网络为固定IP
    Java 多线程学习--线程池
    Java多线程学习--[转]
    使用VLC接收RTP流并时时播放(RTP流是pcma包)
    [转载好文]Linux下编写 makefile 详细教程
    [转载]简明 VIM 练级攻略
    linux删除不了文件, 修改文件属性也删除不了的一个解决思路
    SHELL脚本“syntax error: unexpected end of file”解决方案[半原创]
  • 原文地址:https://www.cnblogs.com/flightless/p/8735057.html
Copyright © 2011-2022 走看看