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.
求最长不重复子串,很经典的DP问题(DP好渣。。。。)
第一反应时用递归:但是时间复杂度还是达到了O(n^2),leetcode的数据总是强到本地无法测试。。。。,以下是递归实现的代码:
public int lengthOfLongestSubstring(String s) { if(s == null || s.isEmpty()){ return 0; } int maxLength = 0; int tem = 0; int from = 0; Map<Character,Integer> map = new HashMap<Character,Integer>(); for(int i = 0 ; i < s.length(); i++){ if(!map.containsKey(s.charAt(i))){ map.put(s.charAt(i),i); maxLength++; }else{ from = map.get(s.charAt(i)); //重复字母上次出现的位置 tem = lengthOfLongestSubstring(s.substring(from + 1)); break; } } return maxLength > tem ? maxLength : tem ; }
超时。。。。
下面来看DP实现,由于懒得再写Hash,偷懒用了hashMap,道理是一样的。
public int lengthOfLongestSubstring(String s) { int length = s.length(); Map<Character,Integer> hash = new HashMap<Character,Integer>(); int currentLength = 0; int maxLength = 0; int from = 0; for(int i = 0; i < length; i++){ if(!hash.containsKey(s.charAt(i))){ currentLength++; hash.put(s.charAt(i), i); }else{ if(from <= hash.get(s.charAt(i))){ from = hash.get(s.charAt(i)) + 1; currentLength = i - hash.get(s.charAt(i)); hash.put(s.charAt(i), i); }else{ currentLength++; hash.put(s.charAt(i), i); } } if(currentLength > maxLength){ maxLength = currentLength; } } return maxLength; }
相关推荐
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...
java java_leetcode题解之Longest Substring Without Repeating
Longest Substring Without Repeating Characters" 描述的是一个经典的计算机编程问题,它源自LeetCode中的第3题——“无重复字符的最长子串”。这个题目要求我们找出一个字符串中没有重复字符的最长子串的长度。在...
Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring 6.ZigZag Conversion 7.Reverse Integer 8.String To Integer 9.Palindrome Number 10.String To Integer 11....
c语言入门 c语言_leetcode题解03-longest-substring-without-repeating-characters
js js_leetcode题解3-longest-substring-without-repeating-characters.js
leetcode 分类leetcode 问题分类 leetcode代码仓库,我的解题思路写在我的博客里: leetcode 代码库,我博客上的解题思路: mkdir 列表: 大批 #1:Two Sum #4:Median of Two Sorted Arrays 地图 #1:Two Sum #3:...
3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum 16. 3Sum Closest 17...
java入门 java_leetcode题解之003_Longest_Substring_Without_Repeating
说明 ⽬录 第⼀章 序章 关于 LeetCode 什么是 Cookbook 为什么会写这个开源书 关于书的封⾯ 关于作者 关于书中的代码 ⽬标读者 ...第四章 Leetcode 题解 ...3. Longest Substring Without Repeating Characters 4. ......
java lru leetcode Fighting for a job! 记录找工作期间搬运的题,全部使用Java实现,本人C++鶸 1 ...LeetCode ...LeetCode ...LeetCode ...LeetCode ...Characters LeetCode 13 Roman to Integer LeetCode 6 Zi
答案LeetCode-Longest_Substring_Without_Repeating_Characters 这是LeetCode上“最长子串无重复字符”问题的解决方案。 问题描述:给定一个字符串,求没有重复字符的最长子串的长度。 示例 1:输入:“abcabcbb” ...
leetcode Python 001 力码 ├── Algorithms │ ├── cpp │ │ ├── 001 - Two Sum.cpp │ │ ├── 002 - Add Two Numbers.cpp │ │ ├── 003 - Longest Substring Without Repeating ...
Substring Without Repeating Characters], N/A 2017.06.15 打卡[LeetCode 407. Trapping Rain Water II], BFS/Priority queue 2017.06.19 打卡[LeetCode 145. Binary Tree Postorder Traversal], Tree/stack 2017....
Without Repeating Characters 4. Median of Two Sorted Arrays 5. Longest Palindromic Substring 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 12. Integer to Roman 13. Roman to ...
leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...
Repeating Characters 31.1% Medium 0004 Median of Two Sorted Arrays 30.7% Hard 0005 Longest Palindromic Substring 30.1% Medium 0006 ZigZag Conversion 37.5% Medium 0007 Reverse Integer 25.8% Easy 0008 ...
LeetCode-3.Longest_Substring_Without_Repeating_Characters 给定一个字符串,找出没有重复字符的最长子字符串的长度。 示例 1: 输入:“abcabcbb” 输出:3 解释:答案是“abc”,长度为 3。 解决方案 Python3:...
Longest Substring Without Repeating Characters 哈希表 双指针 滑动窗口 Substring with Concatenation of All Words 哈希表 注意匹配方向 Valid Sudoku 数组 遍历 Sudoku Solver 深度优先遍历 回溯 先检查后修改 ...
LeetCode刷题总结 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7....