- 浏览: 183343 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
题目要求很简单,给定一个字符串,找到一个不包含重复元素的最长字符串。
我们可以用哈希表来处理,key中存放字符,value存放相应字符的下标,max记录不包含重复元素的最长字符串的长度。从第一个字符开始,如果当前字符在哈希表中,那么就更新遍历的开始位置,同时维护max;如果不在哈希表中,就将当前元素记录到哈希表中。代码如下:
另外我们也可以用两个指针来处理这个题目。两个指针始终维护着一个不包含重复元素的字符串,不断的更新max来得到结果。代码如下:
题目要求很简单,给定一个字符串,找到一个不包含重复元素的最长字符串。
我们可以用哈希表来处理,key中存放字符,value存放相应字符的下标,max记录不包含重复元素的最长字符串的长度。从第一个字符开始,如果当前字符在哈希表中,那么就更新遍历的开始位置,同时维护max;如果不在哈希表中,就将当前元素记录到哈希表中。代码如下:
public class Solution { public int lengthOfLongestSubstring(String s) { HashMap<Character, Integer> hm = new HashMap<Character, Integer>(); int max = 0; for(int i = 0; i < s.length(); i++) { if(!hm.containsKey(s.charAt(i))) { hm.put(s.charAt(i), i); } else { max = Math.max(max, hm.size()); i = hm.get(s.charAt(i)); hm.clear(); } } return Math.max(max, hm.size()); } }
另外我们也可以用两个指针来处理这个题目。两个指针始终维护着一个不包含重复元素的字符串,不断的更新max来得到结果。代码如下:
public class Solution { public int lengthOfLongestSubstring(String s) { if(s == null || s.length() == 0) return 0; int max = 1; int start = 0, end = 1; String current = s.substring(start, end); for(int i = 1; i < s.length(); i++) { if(current.indexOf(s.charAt(i)) >= 0 ) { max = Math.max(max, end - start); start += current.indexOf(s.charAt(i)) + 1; } end = end + 1; current = s.substring(start, end); } return Math.max(max, end - start); } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 264Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 266You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 383Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 373Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 562Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 474Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 662Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 468The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 428Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 573Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 425All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 896Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 929Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 672Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 781You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
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...
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...
Longest Substring Without Repeating Characters" 描述的是一个经典的计算机编程问题,通常出现在面试或编程竞赛中。这个题目要求我们找到一个字符串中最长的子串,该子串中没有重复的字符。这是一个非常基础但又...
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....
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...
说明 ⽬录 第⼀章 序章 关于 LeetCode 什么是 Cookbook 为什么会写这个开源书 关于书的封⾯ 关于作者 关于书中的代码 ⽬标读者 编程语⾔ ...3. Longest Substring Without Repeating Characters 4. ......
#3:Longest Substring Without Repeating Characters #5:Longest Palindromic Substring 链表 #2:Add Two Numbers 分而治之 #53:Maximum Subarray 队列/集 #3:Longest Substring Without Repeating Characters 优先...
c语言入门 c语言_leetcode题解03-longest-substring-without-repeating-characters
Characters.cpp │ │ ├── 004 - Median of Two Sorted Arrays.cpp │ │ └── 005 - Longest Palindromic Substring.cpp │ └── python │ ├── 001 - Two Sum.py │ ├── 002 - Add Two...
js js_leetcode题解3-longest-substring-without-repeating-characters.js
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.
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 ...
Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 Longest Palindromic Substring 中等 回文 ZigZag Conversion 中等 矩阵 重要 Reverse Integer 简单 字串 String to Integer ...
Characters4. Median of Two Sorted Arrays 004. Median of Two Sorted Arrays 005. Longest Palindromic Substring 006. ZigZag Conversion 007. Reverse Integer 008. String to Integer 009. Palindrome Number ...
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 ...
Without Repeating Characters 5.Longest Palindromic Substring 20.Valid Parentheses 26.Remove Duplicates from Sorted Array 53.Maximum Subarray 70.Climbing Stairs 121.Best Time to Buy and Sell Stock 122....
Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7.Reverse Integer 8.String to Integer (atoi) 9.Palindrome Number 10....
java lru leetcode Fighting for a job! 记录找工作期间搬运的题,全部使用Java实现,本人C++鶸 1 ...Longest Substring Without Repeating Characters LeetCode 13 Roman to Integer LeetCode 6 Zi
Longest Substring Without Repeating Characters 哈希表 双指针 滑动窗口 Substring with Concatenation of All Words 哈希表 注意匹配方向 Valid Sudoku 数组 遍历 Sudoku Solver 深度优先遍历 回溯 先检查后修改 ...