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());
        }
    };
  • 相关阅读:
    读写文件print函数操作
    协程相关
    线程池
    多线程条件
    ibm动态测试
    ubuntu 之 搜狗拼音安装
    Linux 安装 出现Could not get lock /var/lib/dpkg/lock
    Sql server
    maven 搭建
    EOS
  • 原文地址:https://www.cnblogs.com/freeopen/p/5482963.html
Copyright © 2011-2022 走看看