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 ...
随机推荐
- popupwindow 与 输入法
有时候popupwindow会被输入法覆盖, 有时候popupwindow会被输入法给顶上去. 而且这个问题还跟theme的windowFullscreen属性相关. 不过这些可以都不用管, 根据项目 ...
- iOS开发 - AVPlayer实现流音频边播边存
边播边下有三套左右实现思路,本文使用AVPlayer + AVURLAsset实现. 概述 1. AVPlayer简介 AVPlayer存在于AVFoundation中,可以播放视频和音频,可以理解为 ...
- URL 学习总结
1.绝对路径(以"/"斜线开头的路径,代表相对于当前Web应用): a)地址给服务器用,web应用名称可以省略. 请求包含:request.getRequestDispatcher ...
- CSS - Tooltip-arrow 绘制三角形
问题:纯CSS实现bubble的三角形部分 方法:使用border来绘制三角形:例如 .trangle { ; border-color: transparent; border-style: sol ...
- pptv破解版程序,能够免费观看所有蓝光和会员影片!
pptv破解版程序,能够免费观看所有蓝光和会员影片!PPTV网络电视3.4.1.0012绿色版(去广告本地vip版)由Black Hawk精简破解,去掉播放时缓冲.暂停广告.去掉迷你推荐和推荐弹窗.禁 ...
- 【cs229-Lecture18】线性二次型调节控制
本节内容: 控制MDP的算法: 状态行动奖励: 非线性动力学系统: 模型: LQR:线性二次型调节控制:(Riccati方程)
- 【转载】[JS]让表单提交返回后保持在原来提交的位置上
有时候,在网页中点击了页面中的按钮或是刷新了页面后,页面滚动条又 会回到顶部,想看后面的记录就又要拖动滚动条,或者要按翻页键,非常不方便,想在提交页面或者在页面刷新的时候仍然保持滚动条的位置不变,最好 ...
- 在JAVA和android中常用的单列模式
在很多开发中,项目为了节约资源,都把一个类的构造函数变为私有化,这样整个项目中就不能创建多个实例,这样的方法我们称为单例模式 现在通过代码来简介下这个单例模式: 在新建一个java项目后,创建一个实体 ...
- Spring Remoting: HTTP Invoker--转
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-http-invoker.jsp Concept Overview ...
- Java定位CPU使用高问题--转载
原文地址:http://www.cnblogs.com/guoyuqiangf8/p/3545687.html 1.TOP命令,查询消耗CPU高的进程号 PID,并记录下来,按下键盘"H&q ...