zoukankan      html  css  js  c++  java
  • 23-Longest Substring Without Repeating Characters

    Given a string, find the length of the 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.

    本题:中等难度
    思路:动态规划

    f[j]j
    f[j]={f[j1]+1f[j1]num+1A[j]notinvs[j1]A[j]invs[j1]

    其中vs[j-1]保存连续子串,num表示去掉重复字符(包含)之前的字符$$

    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            int n = s.size();
            if(n<=1)return n;
            vector<vector<char> > vm(n);
            vector<int> vi(n);
            vi[0]=1;
            vm[0].push_back(s[0]);
            for(int i=1;i<n;++i)
            {
                vector<char>::iterator  it= find(vm[i-1].begin(),vm[i-1].end(),s[i]);
                if(it!=vm[i-1].end())
                {
                    vector<char> vtmp(it+1,vm[i-1].end());
                    vm[i]=vtmp;
                    vm[i].push_back(s[i]);
                    vi[i]=vm[i-1].end()-it;
                }
                else{
                    vi[i]=vi[i-1]+1;
                    vm[i]=vm[i-1];
                    vm[i].push_back(s[i]);
                }
            }
            return *max_element(vi.begin(),vi.end());
        }
    };
  • 相关阅读:
    寒假学习进度七
    寒假学习进度六
    寒假学习进度五
    mysql 数据库第一天
    HTML 标签&总结
    事件对象
    js的事件流的概念
    jquery 的位置信息
    小米导航 案例
    jquery 的文档操作
  • 原文地址:https://www.cnblogs.com/freeopen/p/5482963.html
Copyright © 2011-2022 走看看