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. linux nadianshi

    http://www.cnblogs.com/fnng/archive/2012/03/19/2407162.html

  2. Linux入门基础教程

    转载自:http://www.centoscn.com/CentOS/2015/0528/5555.html 1. 1      Linux操作系统简介 Linux是一套免费使用和自由传播的类Unix ...

  3. Linux shell入门基础(五)

    五.bash运算及启动脚本 01.使用bash的命令历史 #history …… #set(显示所有的变量) | grep HIS HISTFILE=/root/.bash_history HISTF ...

  4. Python之路,Day10 - 异步IO\数据库\队列\缓存

    Python之路,Day9 - 异步IO\数据库\队列\缓存   本节内容 Gevent协程 Select\Poll\Epoll异步IO与事件驱动 Python连接Mysql数据库操作 RabbitM ...

  5. 如何开通www国际域名个人网站

    欢迎访问我的域名http://www.baiqiantao.xyz 一.准备知识 什么是域名? 所有以www开头的域名,就是国际域名:不以www开头的域名,都是为子域名. 域名都有后缀,后缀是有意义的 ...

  6. 提示用户体验的最佳免费 Jquery 表单插件

    网页表单是一个老生常谈的话题.出于这样或那样的目的,一些示例中都会包括用户注册,电子商务结算,用户设置甚至联系人表格.而输入栏是非常容易用现代的CSS3技术来应用样式.但是到底什么决定整体用户体验? ...

  7. jsp页面可以巧用模态框

    jsp页面使用模态框配合ajax出来的效果真的没话说,当然你也可以使用模态框配合action,但是在删除和更新的时候传值有点麻烦,用ajax 就没有这些问题 ,比如删除代码的时候在js文件中传值可以这 ...

  8. HTTP,TCP,Socket

    TCP/IP三次握手和HTTP过程   1.TCP连接 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终端通过无线网络建立TCP连接.TCP协议可以对上层网络提供接口,使上层网络 ...

  9. 保垒机SSH登录脚本

    线上服务器一般都会有一个保垒机,我们登录线上服务器的时候都要通过这个堡垒机来实现登录,这样我们就要维护很多线上服务器的ip,很麻烦. 所以写了一个脚本用来便捷的登录各个服务器,可以把这个脚本放到跳板机 ...

  10. 利用C++ RAII技术自动回收堆内存

    在C++的编程过程中,我们经常需要申请一块动态内存,然后当用完以后将其释放.通常而言,我们的代码是这样的: 1: void func() 2: { 3: //allocate a dynamic me ...