问题:

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。

  1. 用String.toCharArray()方法,将给定字符串拆成单个字符数组形式,开始遍历。
  2. 记录最长子串长度maxLength,初始值赋为0;记录当前子串列表list。
  3. 如果当前字符,不在当前子串内,将当前字符加入子串。
  4. 如果当前字符,存在当前子串内,先根据当前子串长度list.size()和maxLength的值,取其中大值,更新maxLength。然后找到上一个重复字符的index,删除子串内index位置之前的所有字符(包括index位置的字符)。最后,将当前字符加入子串。
  5. 在结束循环之后,再次检查当前子串长度list.size()和maxLength的值,取大值更新maxLength。因为可能出现最长子串是包含最后一个字符的子串。
  6. 注意检查入参。

解题代码:

 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/

https://github.com/Gerrard-Feng/LeetCode/blob/master/LeetCode/src/com/gerrard/algorithm/medium/Q003.java

PS:如有不正确或提高效率的方法,欢迎留言,谢谢!

No.003:Longest Substring Without Repeating Characters的更多相关文章

  1. LeetCode3:Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  2. Leetcode经典试题:Longest Substring Without Repeating Characters解析

    题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...

  3. LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  4. LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List

    题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: Given a string, find the length of the longest substring without ...

  5. leetcode笔记:Longest Substring Without Repeating Characters

    一. 题目描写叙述 Given a string, find the length of the longest substring without repeating characters. For ...

  6. Q3:Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters 官方的链接:3. Longest Substring Without Repeating Chara ...

  7. leetcode:Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  8. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  9. No.003 Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters Total Accepted: 167158 Total Submissions: 735821 Diff ...

随机推荐

  1. Exists 比Contains 慢非常多。

    void Main() { List<string> s = new List<string>(){}; for(int i=0;i<10000;i++) { s.Add ...

  2. 打包并压缩seajs代码

    背景 seajs是一款优秀的模块开发插件,但是当我们使用它来进行模块化开发的时候,由于它的每个模块的加载都会进行一次http请求,那么当模块数量倍增的时候,会拖慢页面的加载速度. 通常我们为了能加快页 ...

  3. 让我们一起Go(九)

    前言: 又好久么更新了,无奈公司项目多,自己又接了私活,于是时间更不够了......不过我是不会让它流产的,坚持! 一.Go语言中的函数 终于轮到函数了,其实也没有什么好说的,无非就是一个语法问题,c ...

  4. HTML5[6]:多行文本显示省略号

    CSS3新增text-overflow: ellipse; 只支持单行文本 如果是多行文本, 在无法完全显示的情况下,可以按下面这样写: overflow:hidden; display: -webk ...

  5. datatables.js 简单使用--多选框和服务器端分页

    说明:datatables是一款jQuery表格插件.感觉EasyUI的datagrid更易用 内容:多选框和服务器端分页 缘由:写这篇博客的原因是datatables的文档写的不怎么样,找东西很麻烦 ...

  6. form表单action提交表单,页面不跳转且表单数据含文件的处理方法

    在最近的项目中需要将含 input[type='file']的表单提交给后台 ,并且后台需要将文件存储在数据库中.之前所用的方法都是先将文件上传到七牛服务器上,然后七牛会返回文件的下载地址,在提交表单 ...

  7. DDD:Can I DDD?

    下面是<实现领域驱动>的作者给出的一段话: You can implement DDD if you have: A passion for creating excellent soft ...

  8. multi-CPU, multi-core and hyper-thread--转

    原文地址:http://stackoverflow.com/questions/680684/multi-cpu-multi-core-and-hyper-thread Multi-CPU was t ...

  9. Marvel – 将图像和源文件转换成互动,共享的原型

    Marvel 是一款非常简单的工具,将图像和设计源文件转换成互动,共享的原型,无需任何编码.原型可以通过点击几下鼠标就创建出来,能工作在任何设备上的浏览器,包括移动设备,台式机.Marvel 的一个特 ...

  10. C# 获取磁盘空间大小的方法

    方法一:利用System.IO.DriveInfo.GetDrives方法来获取 /// /// 获取指定驱动器的空间总大小(单位为B) /// /// 只需输入代表驱动器的字母即可 (大写) /// ...