Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

问题描述:

给定一个字符串,寻找最长无重复子串,返回最长长度。

解决方案:

1、暴力搜索,O(n^2)不考虑。

2、举个例子,abcdecf

说明:使用两个游标,i、j 初始为0,len为字符串长度,maxLen为要返回的无重复最长子串的长度,exist数组为标识元素是否重复,默认支持ASCII,数组大小开到256.

a)对于字符串 a b c d e c f 来说:i 增长到第二个'c'的下标[5],发现之前'c'出现过;

b)这时,首先更新最大长度;

c)然后while循环执行 j++ 自增到第一个'c',同时将第一个'c'之前的元素的存在标志清除。

d)i++,j++,从a b c d e c f 处继续向后执行

e)最后在return之前,再更新maxLen

 public int lengthOfLongestSubstring(String s) {
int len = s.length();
boolean[] exist = new boolean[256];
for (int i = 0; i < 256; i++) {
exist[i] = false;
}
int i = 0, j = 0;
int maxLen = 0;
while (i < len) {
if (!exist[s.charAt(i)]) {//如果访问的元素没有出现过
exist[s.charAt(i)] = true;
i++;
} else {//发现两个一样的,现在i指向两个元素中的第二个 maxLen = Math.max(i - j, maxLen);//更新最大长度
while (s.charAt(i) != s.charAt(j)) {
exist[s.charAt(j)] = false;//重置exist数组
j++;
}//while循环结束后,现在i、j都是指向那个重复元素,j指向第一个
i++;
j++;
}
}
maxLen = Math.max(maxLen, len - j);//最后再更新最大程度
//System.out.println(maxLen);
return maxLen;
}

重新做了一遍,发现还没第一次做的好:

public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0) {
return 0;
}
HashMap<Character, Integer> exist = new HashMap<>();
int len = 0, res = 0, last_j = 0;
for (int i = 0; i < s.length(); i++) {
if (exist.get(s.charAt(i)) == null) {
exist.put(s.charAt(i), i);
len++;
} else {
int j = exist.get(s.charAt(i));
len = i - j;
for (int k = last_j; k <= j; k++) {
exist.remove(s.charAt(k));
}
exist.put(s.charAt(i), i);
last_j = j + 1;
}
res = Math.max(len, res);
}
System.out.println(res);
return res;
}

原创文章,转载请注明出处。

LeetCode——Longest Substring Without Repeating Characters的更多相关文章

  1. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

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

  2. leetcode: longest substring without repeating characters

    July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...

  3. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  4. C++ leetcode Longest Substring Without Repeating Characters

    要开学了,不开森.键盘声音有点大,担心会吵到舍友.今年要当个可爱的技术宅呀~ 题目:Given a string, find the length of the longest substring w ...

  5. [LeetCode]Longest Substring Without Repeating Characters题解

    Longest Substring Without Repeating Characters: Given a string, find the length of the longest subst ...

  6. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

  7. LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)

    题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...

  8. [Leetcode] Longest Substring Without Repeating Characters (C++)

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

  9. [LeetCode] Longest Substring Without Repeating Characters (LinkedHashSet的妙用)

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

随机推荐

  1. Qt 学习之路:QSortFilterProxyModel

    从本章开始,我们将逐步了解有关自定义模型的相关内容.尽管前面我们曾经介绍过 Qt 提供的几个内置模型:QStringListModel和QFileSystemModel,但对于千变万化的需求而言,这些 ...

  2. winserver2008下创建计划任务注意点

    winserver2008下创建任务计划注意点: 1.建立独立用户,可以给其赋予administrator权限 2.起始于(可选):要填写exe文件所在路径 3.设置成“不管用户是否登录都运行”,同时 ...

  3. xUtils3源码分析(一):view的绑定

    概述 xUtils3是国人开发的一款功能丰富的Android快速开发框架,值得研究下.zip包下载:[ZIP]xutils主要分以下几个模块 视图绑定模块 网络请求模块 数据库模块 图片加载模块 我们 ...

  4. HTML5 离线缓存忽略主页实例

    默认情况下 指定html mianfest的当前页面会自动离线缓存到客户端. 取消的方法,可以使用iframe类实现 1.主页定义: <iframe frameborder="no&q ...

  5. ecshop首页调用指定商品分类下的商品品牌列表

    转之--http://www.16css.com/ecshop/735.html 通过二次开发可以实现ECSHOP首页调用指定分类下的品牌列表. 第一步: 打开根目录下的index.php 在最后面 ...

  6. java 安卓开发之文件的读与写

    java文件的读与写,代码: String file="user.txt"; private void writeFileData(String str1, String str2 ...

  7. JavaScript--垃圾回收器

    垃圾回收: 释放不再被任何变量引用的对象 垃圾回收器: 专门记录对象的引用次数,并回收不再被引用的对象的程序. 垃圾回收器和主程序并行在后台执行 垃圾回收器会为每个对象创建一个引用计数器(counte ...

  8. hive 桶相关特性分析

    1. hive 桶相关概念     桶(bucket)是指将表或分区中指定列的值为key进行hash,hash到指定的桶中,这样可以支持高效采样工作.     抽样( sampling )可以在全体数 ...

  9. linux的du和df命令

    今天也有同学问我Linux下查看目录大小的命令,现在也将前阵子学习到du/df两个命令总结一下吧.前阵子测试工作中有遇到过由于磁盘空间满导致程序无法执行到情况,所以使用了df和du两个命令. du查看 ...

  10. underscorejs-find学习

    2.5 find 2.5.1 语法: _.find(list, predicate, [context]) 2.5.2 说明: 对list集合的每个成员依次进行匹配(根据predicate迭代函数检测 ...