码迷,mamicode.com
首页 > 其他好文 > 详细

Longest Substring Without Repeating Characters

时间:2019-07-16 18:35:16      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:cto   参考   运行   with   tin   NPU   code   back   lse   

3.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.

Code

//
//  main.cpp
//  最长无重复子串
//
//  Created by mac on 2019/7/16.
//  Copyright ? 2019 mac. All rights reserved.
//

#include <iostream>

#include <string.h>

#include <algorithm>

#include <vector>

using namespace std;

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        string tmp="";
        vector<int> Max = {0};
        for (int i=0; i<s.size(); ++i) {
            if (tmp.find(s[i])==18446744073709551615 && !s.empty()) {
                tmp.append(1,s[i]);
                if(i==s.size()-1){
                    Max.push_back(tmp.size());
                }
                
            }else{
                Max.push_back(tmp.size());
                i=i-tmp.size();
                tmp="";
            }
            
        }
        return *max_element(Max.begin(),Max.end());
    }
};

int main(int argc, const char * argv[]) {
    
    string a="ghhvvgyjkkkhgfrtyjjhhhhuuikikjhytyyuik";
    string b="";
    if (a==b) {
        cout<<"相等"<<endl;
    }
    //cout<<a[1]<<endl;
   // cout<<a.size()<<endl;
    //18446744073709551615
    //cout<<a.find("k")<<endl;
    if (-1!=18446744073709551615) {
        cout<<"相等了"<<endl;
    }
    
    Solution so;
    cout<<so.lengthOfLongestSubstring(a)<<endl;
    
    cout<<a.empty()<<endl;

    return 0;
}

运行结果

8
0
Program ended with exit code: 0

参考文献

Longest Substring Without Repeating Characters

标签:cto   参考   运行   with   tin   NPU   code   back   lse   

原文地址:https://www.cnblogs.com/overlows/p/11196105.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!