此解法的时间复杂度是O(N),最坏情况下时间复杂度是O(N^2),因为HashSet的contains方法;空间复杂度是O(N)。 publicintlengthOfLongestSubstring3(String s){ Set<Character> set =newHashSet<Character>();intn=s.length(), left =0, right =0;intresult=0;while(left < n && right < n) {if(!set...
参考代码如下: 1classSolution {2public:3intlengthOfLongestSubstring(strings) {4constintsize =s.size();5if(s.empty())return0;6if(size<=1)returnsize;7intdp[size];//dp[i]表示以s[i]结尾的不重复子串8memset(dp,0,size);9intres =1;10dp[0]=1;//dp初始值11for(inti =1;i<size;++i)12...
题目地址:https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ 题目描述 Given a string, find the length of thelongest substringwithout repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", which the length is 3. 1. ...
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", ...
Title :Longest Substring Without Repeating Characters Given a string, find the length of thelongest substringwithout 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 a string, find the length of thelongest substringwithout repeating characters. 给定字符串,找到最大无重复子字符串。 样例 Input:"abcabcbb"Output:3Explanation:The answeris"abc",withthe length of3.Input:"bbbbb"Output:1Explanation:The answeris"b",withthe length of1.Input:"pwwkew"Output:3...
Title :Longest Substring Without Repeating Characters Given a string, find the length of thelongest substringwithout 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. ...
classSolution{public:intlengthOfLongestSubstring(string s){if(s.length()==0){return0;}unordered_map<char,int>hash;intbegin=0,end=0;intrepeat=0;intmax_length=0;while(end<s.length()){if(hash[s[end]]>0){repeat++;}hash[s[end]]++;end++;while(repeat>0){if(hash[s[begin]]>1){repe...
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. 代码语言:javascript 复制 pu...
class Solution { public int lengthOfLongestSubstring(String s) { // 记录字符上一次出现的位置 int[] last = new int[128]; for(int i = 0; i < 128; i++) { last[i] = -1; } int n = s.length(); int res = 0; int start = 0; // 窗口开始位置 for(int i = 0; i < n;...