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.
Solution:
除了HashMap,我们还可以使用int[256]的数组来保存字符的索引。
public int lengthOfLongestSubstring(String s) { int n = s.length(); if(n == 0) return 0; Map<Character, Integer> map = new HashMap<>(); int start = 0, len = 0; for(int i=0; i<n; i++) { char c = s.charAt(i); if(map.containsKey(c) && map.get(c) >= start) { start = map.get(c) + 1; // test case: abba, when last 'a', we need to check start position } len = Math.max(len, i-start+1); map.put(c, i); } return len; }
相关推荐
js js_leetcode题解3-longest-substring-without-repeating-characters.js
c语言入门 c语言_leetcode题解03-longest-substring-without-repeating-characters
java java_leetcode题解之Longest Substring Without Repeating
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题解之003_Longest_Substring_Without_Repeating
答案LeetCode-Longest_Substring_Without_Repeating_Characters 这是LeetCode上“最长子串无重复字符”问题的解决方案。 问题描述:给定一个字符串,求没有重复字符的最长子串的长度。 示例 1:输入:“abcabcbb” ...
Longest Substring Without Repeating Characters" 描述的是一个经典的计算机编程问题,它源自LeetCode中的第3题——“无重复字符的最长子串”。这个题目要求我们找出一个字符串中没有重复字符的最长子串的长度。在...
#3:Longest Substring Without Repeating Characters #5:Longest Palindromic Substring 链表 #2:Add Two Numbers 分而治之 #53:Maximum Subarray 队列/集 #3:Longest Substring Without Repeating Characters 优先...
leetcode卡 LeetCode 记录一下再LeetCode上刷的题,坚持每天刷一道吧 2017.06.12 打卡[LeetCode 2. Add Two Numbers], Linked list 2017.06.13 打卡[LeetCode 200. Number of Islands], BFS 2017.06.14 打卡...
leetcode题库 LeetCode-Web 初始化 前端库依赖 下载,并将jquery-3.x.x.min.js移动到static目录下。 下载,并将semantic.min.js、semantic.min.css、components和themes...longest-substring-without-repeating-charac
3.Longest Substring 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 ...
3. **Longest Substring Without Repeating Characters**(无重复字符的最长子串):该问题旨在找到一个字符串中最长的子串,其中不包含任何重复字符。解题策略可能包括滑动窗口或使用哈希表来跟踪字符出现的状态。...
Leetcode.3 Longest Substring Without Repeating Characters Leetcode.76 Minimum Window Substring Leetcode.438 Find All Anagrams in a String Pattern: two points 双指针是这样的模式:两个指针朝着左右方向...
leetcode Python 001 力码 ├── Algorithms │ ├── cpp │ │ ├── 001 - Two Sum.cpp │ │ ├── 002 - Add Two Numbers.cpp │ │ ├── 003 - Longest Substring Without Repeating ...
LeetCode-3.Longest_Substring_Without_Repeating_Characters 给定一个字符串,找出没有重复字符的最长子字符串的长度。 示例 1: 输入:“abcabcbb” 输出:3 解释:答案是“abc”,长度为 3。 解决方案 Python3:...
leetcode题库 LeetCode-Go 理论基础 见Note 脑图 TODO 待填充 算法题 面试高频出现,以及一些非常经典重要的算法题优先 题目列表 No Title Link Solution Acceptance Difficulty Topics Solution 0001 Two Sum 46.1%...
7. 题目3:无重复字符的最长子串 (Longest Substring Without Repeating Characters) 使用滑动窗口的思想,维护一个哈希表来跟踪字符的出现情况,从而找到没有重复字符的最长子串。 8. 题目11:Container With ...
Longest Substring Without Repeating Characters 哈希表 双指针 滑动窗口 Substring with Concatenation of All Words 哈希表 注意匹配方向 Valid Sudoku 数组 遍历 Sudoku Solver 深度优先遍历 回溯 先检查后修改 ...
leetcode 答案 SH_LeetCode LeetCode-Swift解答记录(Answer records of LeetCode) 11月21日 更新:添加README文件以及LeetCode前三题代码(Add README.md and Code for the first three questions) 1. 两数之和...
leetcode 跳跃 leetcode 动态规划,背包问题 01背包问题:416. Partition Equal Subset Sum 最长回文:5. Longest Palindromic Substring - 字数组余数:523. Continuous Subarray Sum - 无重复字符的最长子串:3. ...