(LeetCode) 3. Longest Substring Without Repeating Characters

文章目录

  1. 1. Original URL: 3. Longest Substring Without Repeating Characters
  2. 2. My solutions:
    1. 2.1. With C++:

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.

Difficulty: Medium
Tags: Hash Table, Two Pointers, String

Original URL: 3. Longest Substring Without Repeating Characters

My solutions:

With C++:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
int lengthOfLongestSubstring(string s) {
string sub = "";
int max = 0,j = 0,start = 0,i = 0;
int n = s.length();
for(i;i < n + 1;i++)
{
if(max < i - start) {
max = i - start;}
j = sub.find(s[i]);
start = start + j + 1;
sub = s.substr(start,i - start + 1);
}
return max;
}
};

See my 30+ solutions of LeetCode on GitHub