Skip to content

feat(ml): $3.longest-substring-without-repeating-characters.md #453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 29, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion problems/3.longest-substring-without-repeating-characters.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,65 @@ https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/

## 代码

代码支持:Python3
代码支持:C++,Java,Python3


C++ Code:

```c++
class Solution {
public:
int lengthOfLongestSubstring(string s) {

int max_len = 0, start = 0;
int n = s.length();
//
map<char, int> mp;

for(int i=0;i<n;i++)
{
char alpha = s[i];
if(mp.count(alpha))
{
start = max(start, mp[alpha]+1);
}
max_len = max(max_len, i-start+1);
// 字符位置
mp[alpha] = i;
}

return max_len;
}
};
```


Java Code:

```java
class Solution {
public int lengthOfLongestSubstring(String s) {
int max_len = 0, start = 0;
int n = s.length();
//
Map<Character, Integer> map = new HashMap<>();

for(int i=0;i<n;i++)
{
char alpha = s.charAt(i);
if(map.containsKey(alpha))
{
start = Math.max(start, map.get(alpha)+1);
}
max_len = Math.max(max_len, i-start+1);
// 字符位置
map.put(alpha, i);
}

return max_len;
}
}
```

Python3 Code:

Expand Down