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. 安装Fedora(附镜像下载地址)

    近期又试着装了一遍Fedora,强迫症迫使我写一些简单的教程,方便以后有用 先把VM配置好,然后进入Fedora 点击Skip 这几按照提示一步一步来 选个人桌面 手工分区 分区的时候注意下每个区的容 ...

  2. maven指定构建的编码格式

    pom.xml文件添加如下内容: <properties>    <project.build.sourceEncoding>UTF-8</project.build.s ...

  3. (转)ThinkPHP find方法 查询一条数据记录

    find() ThinkPHP find() 方法是和 select() 用法类似的一个方法,不同之处 find() 查询出来的始终只有一条数据,即系统自动加上了 LIMIT 1 限制. 当确认查询的 ...

  4. 【USACO 2.1.4】荷斯坦奶牛

    [题目描述] 纪念“逝去”的Wecing 农民JOHN以拥有世界上最健康的奶牛为傲.他知道每种饲料中所包含的牛所需的最低的维他命量是多少.请你帮助农夫喂养他的牛,以保持它们的健康,使喂给牛的饲料的种数 ...

  5. Asp.net的IP地址屏蔽功能设计

    "IP地址的长度为32位,分为4段,每段8位,用十进制数字表示,每段数字范围为0~255,段与段之间用句点隔开." 由此我们了解到,IP地址实际上是一个32位正整数,在C#中可以使 ...

  6. Linux进程或线程绑定到CPU

    Linux进程或线程绑定到CPU 为了让程序拥有更好的性能,有时候需要将进程或线程绑定到特定的CPU,这样可以减少调度的开销和保护关键进程或线程. 进程绑定到CPU Linux提供一个接口,可以将进程 ...

  7. js 判断url的?后参数是否包含某个字符串

    function GetQueryString(name){    var reg=eval("/"+name+"/g");   var r = window. ...

  8. C# 中使用 OpenSSL 的公钥/私钥进行加密和解密

    在C#中进行RSA解密,需要用RSACryptoServiceProvider,但是不支持OpenSSL格式的公钥或者私钥. X509 公钥 -----BEGIN PUBLIC KEY----- MI ...

  9. 织梦dede标签tags的美化教程

    我们在行dede仿站的时候,经常会遇到tags标签的调用,调用非常简单,官方有专门的调用标签,但是官方的调用是一成不变的模式,dede5.6以前的版本,基本上都是黑色相同大小的表示,而在dede5.7 ...

  10. 平稳退化,JS和HTML标记分离,极致性能的JavaScript图片库

    index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...