Longest Substring Without Repeating Character…
2019-08-16 07:50:18来源:博客园 阅读 ()
Longest Substring Without Repeating Characters
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
参考文献
- https://leetcode.com/problems/longest-substring-without-repeating-characters/
原文链接:https://www.cnblogs.com/overlows/p/11196105.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:c++练习—实现简单的4则运算
下一篇:李超线段树
- Longest Ordered Subsequence 2020-02-09
- 最长上升子序列(LIS: Longest Increasing Subsequence) 2019-09-08
- The Xor-longest Path(trie树) 2019-08-16
- SPOJ7258 SUBLEX - Lexicographical Substring Search(后缀 2018-06-29
- SPOJ8222 NSUBSTR - Substrings(后缀自动机) 2018-06-29
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash