LeetCode 3. Longest Substring Without Repeating Characters(最长不重复子串)
2016-05-01 07:12
381 查看
原题网址:https://leetcode.com/problems/longest-substring-without-repeating-characters/
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given
which the length is 3.
Given
with the length of 1.
Given
with the length of 3. Note that the answer must be a substring,
a subsequence and not a substring.
方法:通过哈希表/直方图来标记字符是否出现过。
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 the length of 1.
Given
"pwwkew", the answer is
"wke",
with the length of 3. Note that the answer must be a substring,
"pwke"is
a subsequence and not a substring.
方法:通过哈希表/直方图来标记字符是否出现过。
public class Solution { public int lengthOfLongestSubstring(String s) { if (s == null) return 0; boolean[] has = new boolean[256]; int max = 0; int from = 0; char[] sa = s.toCharArray(); for(int i=0; i<sa.length; i++) { while (has[sa[i]]) has[sa[from++]] = false; has[sa[i]] = true; max = Math.max(max, i-from+1); } return max; } }
相关文章推荐
- Android Studio 常用快捷键详解
- C++对string进行大小写转换
- LeetCode 2. Add Two Numbers(两数相加)
- 4月11日作业
- 让我们Core在一起:ASP.NET Core & .NET Core
- LeetCode 1. Two Sum(两数之和)
- 第9周学习进度
- 易經大意 361_380
- LeetCode 009 Palindrome Number
- LeetCode 346. Moving Average from Data Stream(数据流移动平均值)
- git log
- LeetCode 334. Increasing Triplet Subsequence
- 04- Shell脚本学习--条件控制和循环语句
- LeetCode 008 String to Integer (atoi)
- HTTP客户端识别与cookie机制
- reverse a string反转字符串
- [LeetCode] Moving Average from Data Stream 从数据流中移动平均值
- Causing exception while using AlertDialog
- 数据库设计中实体关联的四种类型
- LeetCode 007 Reverse Integer