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 ...
随机推荐
- Rxlifecycle(一):使用
Rxlifecycle使用非常方便简单,如下: 1.集成 build.gradle添加 //Rxlifecycle compile 'com.trello:rxlifecycle:0.3.1' com ...
- nohup启动java命令导致dubbo无法注册
没有任何异常,日志和正常注册日志一模一样,但服务就是没注册成功. 解决办法,screen代替nohup命令. screen screen -ls screen -r pid (ctrl + a)+d
- JavaScript中的setMonth()方法的小问题 解决:setMonth(month, 1)
今天测试人员发现一个问题,从英文日期转化中文日期,月份总会有“6月”变为“7月”.于是我在本地反复测试,发现如下规律:只要setMonth()的参数为小于31天的月份时就会变为下一个月. 原因是:因为 ...
- SAP ECC FI配置文档
SAP ECC 6.0 Configuration Document Financial Accounting (FI) Table of Content TOC \O "1-2" ...
- Unity数据存储路径总结
一.在项目根目录中创建Resources文件夹来保存文件.可以使用Resources.Load("文件名字,注:不包括文件后缀名");把文件夹中的对象加载出来.注:此方可实现对文件 ...
- UFT\QTP 12 新特性
UFT\QTP 12 新特性 http://blog.csdn.net/testing_is_believing/article/details/22310297
- 使用 Portable Class Library(可移植类库)开发 Universal Windows App
今天在这里跟大家聊聊关于 Windows Universal 应用夸平台的问题,首先Universal Windows App的定义相信大家已经有所了解了(如果你是一个刚刚接触 Universal A ...
- 写出几种IE6 BUG的解决方法
1.双边距BUG float引起的 使用display:inline 2.3像素问题 使用多个float和注释引起的 使用dislpay:inline -3px 3.超链接hover 点击后失效 ...
- SQL Server 问题 1 - SQL Server encountered error 0x80070422/0x8007042d
今天执行SQL Server 2014的full-text search 查询操作:select * from table where contains(summary, 'smith') 报出如下错 ...
- [LeetCode] Combination Sum 回溯
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...