Leetcode-Longest Substring Without Repeating Characters

来源:互联网 发布:spss输入数据是问号 编辑:程序博客网 时间:2024/06/06 08:52

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 asubstring, "pwke" is a subsequence and not a substring.

Subscribe to see which companies asked this question.

以下代码为测试将返回结果改为了打印结果

#include<iostream>#include<stdio.h>#include<string>#include<vector>using namespace std;class Solution{public:void lengthOfLongestSubstring(string s) {int start = -1;int max = 0;vector<int> location(300, -1);for (int i = 0; i < s.size(); i++) {if (location[s[i]]>start)start = location[s[i]];if (i - start > max)max = i - start;location[s[i]] = i;}cout << max << endl;}};int main() {string s = "abcabcbb";Solution *p = new Solution;p->lengthOfLongestSubstring(s);system("pause");return 0;}
0 0
原创粉丝点击