Leetcode-Longest Substring Without Repeating Characters

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode-Longest Substring Without Repeating Characters相关的知识,希望对你有一定的参考价值。

https://leetcode.com/problems/longest-substring-without-repeating-characters/

 

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

 

经典Hashtable + two pointer的题目。时间复杂度O(n), 空间复杂度O(n)

 1 public class Solution {
 2     public int lengthOfLongestSubstring(String s) {
 3         int res = 0, start = 0;
 4         Map<Character, Integer> map = new HashMap<>();
 5         for(int i = 0; i < s.length(); i++){
 6             char c = s.charAt(i);
 7             if(map.containsKey(c)){
 8                 start = Math.max(start, map.get(c)+1);
 9             }
10             map.put(c, i);
11             res = Math.max(res, i-start+1);
12         }
13         return res;
14     }
15 }

 

以上是关于Leetcode-Longest Substring Without Repeating Characters的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode-Longest Substring Without Repeating Characters

LeetCode-Longest Palindromic Subsequence

LeetCode-Longest Palindromic Substring

LeetCode-Longest Substring with At Least K Repeating Characters

Codeforces 392E Deleting Substrin(区间dp)

文件下载