No.003:Longest Substring Without Repeating Characters
问题:
Given a string, find the length of the longest substring without repeating characters.
Example:
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.
官方难度:
Medium
翻译:
给定一个字符串,找出最长子串的长度,且在子串中无重复字符。
例子:
字符串1:“abcabcbb”,长度为3。
字符串2:“bbbbb”,长度为1。
字符串3:“pwwkew”,长度为3。
- 用String.toCharArray()方法,将给定字符串拆成单个字符数组形式,开始遍历。
- 记录最长子串长度maxLength,初始值赋为0;记录当前子串列表list。
- 如果当前字符,不在当前子串内,将当前字符加入子串。
- 如果当前字符,存在当前子串内,先根据当前子串长度list.size()和maxLength的值,取其中大值,更新maxLength。然后找到上一个重复字符的index,删除子串内index位置之前的所有字符(包括index位置的字符)。最后,将当前字符加入子串。
- 在结束循环之后,再次检查当前子串长度list.size()和maxLength的值,取大值更新maxLength。因为可能出现最长子串是包含最后一个字符的子串。
- 注意检查入参。
解题代码:
public static int lengthOfLongestSubstring(String s) {
if (s == null) {
throw new IllegalArgumentException("Input error");
}
char[] charArray = s.toCharArray();
int maxLength = 0;
List<Character> list = new ArrayList<>();
for (int i = 0; i < charArray.length; i++) {
Character c = charArray[i];
// 判断当前字符是否重复
if (list.contains(c)) {
// 更新最大长度
maxLength = list.size() > maxLength ? list.size() : maxLength;
// 删除重复之前的字符
int lastIndex = list.indexOf(c);
// lastIndex+1 代表删除次数
while (lastIndex + 1 > 0) {
list.remove(0);
lastIndex--;
} }
// 当前字符是一定要加的
list.add(c);
}
// 特殊情况考虑:最长字符串在原字符串尾部
maxLength = list.size() > maxLength ? list.size() : maxLength;
return maxLength;
}
lengthOfLongestSubstring
相关链接:
https://leetcode.com/problems/longest-substring-without-repeating-characters/
PS:如有不正确或提高效率的方法,欢迎留言,谢谢!
No.003:Longest Substring Without Repeating Characters的更多相关文章
- LeetCode3:Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- Leetcode经典试题:Longest Substring Without Repeating Characters解析
题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...
- LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List
题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: Given a string, find the length of the longest substring without ...
- leetcode笔记:Longest Substring Without Repeating Characters
一. 题目描写叙述 Given a string, find the length of the longest substring without repeating characters. For ...
- Q3:Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters 官方的链接:3. Longest Substring Without Repeating Chara ...
- leetcode:Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- No.003 Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters Total Accepted: 167158 Total Submissions: 735821 Diff ...
随机推荐
- Openvswitch原理与代码分析(6):用户态流表flow table的操作
当内核无法查找到流表项的时候,则会通过upcall来调用用户态ovs-vswtichd中的flow table. 会调用ofproto-dpif-upcall.c中的udpif_upcall_hand ...
- Run same command on all SQL Server databases without cursors
original: https://www.mssqltips.com/sqlservertip/1414/run-same-command-on-all-sql-server-databases-w ...
- 学习之路三十九:新手学习 - Windows API
来到了新公司,一开始就要做个程序去获取另外一个程序里的数据,哇,挑战性很大. 经过两周的学习,终于搞定,主要还是对Windows API有了更多的了解. 文中所有的消息常量,API,结构体都整理出来了 ...
- JavaMelody应用监控使用指南
原文链接:http://www.cnblogs.com/xing901022/p/4116430.html 前言 本文参考JavaMelody的UserGuide编写,部分文字均来自文档,添加有个人理 ...
- 专访Linux嵌入式开发韦东山操作系统图书作者--转
CSDN学院讲师韦东山:悦己之作,方能悦人 发表于2015-04-28 08:09| 6669次阅读| 来源CSDN| 24 条评论| 作者夏梦竹 专访Linux嵌入式开发韦东山操作系统图书作者 摘要 ...
- asp.net MVC之 自定义过滤器(Filter)
一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration:缓存的时间,以秒为 ...
- Bulk Insert的用法 .
/******* 导出到excel */EXEC master..xp_cmdshell 'bcp SettleDB.dbo.shanghu out c:/temp1.xls -c -q -S&quo ...
- js两种定义函数、继承方式及区别
一:js两种定义函数的方式及区别 1:函数声明: function sayA() { alert("i am A"); } 2:函数表达式: var sayB = function ...
- java8 函数式编程
/*** * 测试统计API <br> * 小结:<br> * 1.逻辑代码应用{} 包起来 不写{} 默认 + return <br> * 2.重载方法参数必须加 ...
- Django数据模型及操作
转自:http://blog.sina.com.cn/s/blog_a73687bc0101cygy.html (一) 初始化测试运行环境 import os; import sys; sys.pat ...