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如何关闭防火墙和查看防火墙的具体情况

    1.Linux下关闭和开启防火墙 1) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重启后失效 开启: ser ...

  2. RabbitMQ Management HTTP API--官方文档

    Introduction Apart from this help page, all URIs will serve only resources of type application/json, ...

  3. 体验Impress.js

    用腻了ppt,prezi的风格看起来更酷一点儿,无意中得知有Impress.js这么一个H5版的prezi,nice,值得一试. 关于Impress.js,网上教程很多,但说实话就跟教小朋友一样,一步 ...

  4. Lucene全文检索

    Lucene写入和更新操作: if (id.equals("")) { this.goodsService.save(goods); String goods_lucene_pat ...

  5. 数据库 ORM框架 ORMLite

    几个ORM框架的比较 先介绍一下ORM的概念,以前也一直听说,不过没详细了解啥意思.其全称叫做对象关系映射(Object Relation Mapping),是一种程序设计技术,用于实现面向对象编程语 ...

  6. uva 1391 Astronauts(2-SAT)

    /*翻译好题意 n个变量 不超过m*2句话*/ #include<iostream> #include<cstdio> #include<cstring> #inc ...

  7. html.day01

    1.web标准: 1. 结构  (xhtml)  2. 表现(css)  3.行为(js) html   超文本标记语言 xhtml  (严格型超文本标记语言) 2.规范: 1. 所有标签(标记)都要 ...

  8. request.getAttribute( "result");和request.setAttribute("result",username);

    request.setAttribute("result",username);在request对象中加入名为result的属性并附值为username,因为request对象是可 ...

  9. 64位Window操作系统下,Orcal数据访问服务器端和客户端版本对应与通讯问题

    最近做一个小系统,需要在客户现场搭建数据库环境.之前我们一直访问的是公司的一个测试库,现在需要在现场开发,现场的Orcal服务器是12C ,我们本不打算重装服务器端orcal,故将我们自己电脑的orc ...

  10. AutoIt3初探(1)

    AutoIt3可实现系统操作,键盘鼠标模拟,是自动化测试的一个好工具. 这个是在线帮助文档,http://www.jb51.net/shouce/autoit/ 需要先下载一个autoIt安装,然后将 ...