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.

初步解决方法:

思路:把所有连续的子串找出来,再判断各个子串是否有重复元素

原字符串长度为n的话,所有子串的个数就是c(n,2) = n+n-1+...+1;

public class Solution {
public int lengthOfLongestSubstring(String s) {
int length = s.length();
boolean reflag = false;
int max = 0;
List<String> lists = new ArrayList<String> ();
for(int i=0; i<length; i++) {
for(int j=i+1; j<length; j++) {
lists.add(s.substring(i,j+1));
}
} for(String ssub : lists) {
reflag = false;
for(int i=0; i<ssub.length(); i++){
char c = ssub.charAt(i);
if(ssub.indexOf(c,i+1)!=-1){
reflag = true;
break;
}
} if(!reflag){
int sublen = ssub.length();
if(sublen > max) {
max = sublen;
}
}
}
return max;
} }
    public static void main(String[] args) {
String s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&";
boolean reflag = false;
int max = 1;
List<String> lists= new ArrayList<String>();
int length = s.length();
for(int i=0; i<length; i++) {
for(int j=i+1; j<length; j++) {
lists.add(s.substring(i, j+1));
}
}
for(String subS:lists){
reflag = false;
for(int i=0; i<subS.length(); i++){
char c = subS.charAt(i);
if(subS.indexOf(c, i+1)!=-1){
reflag = true;
break;
}
} if(!reflag){
int space = subS.length();
if(space > max){
max = space;
} } }
System.out.println(max);
System.out.println(lists.size());
System.out.println(lists.toString());
}

这种方法并不能通过leetcode的测试,但是逻辑上确实是正确的,对于特别长的字符串,会提示超时错误。

改进方法:

边查找子串边判断子串是否重复

1、依次查找长度为s.length--的所有子串,把相同长度的子串放在一个lists里面。

public class Solution {
public int lengthOfLongestSubstring(String s) {
int length = s.length();
boolean reflag = false;
int max = 1;
List<String> lists = new ArrayList<String> ();
for(int len = length; len>1; len--){
lists.clear();
for(int i=0; i+len<=length; i++){
lists.add(s.substring(i, i+len));
}
for(String ssub : lists) {
reflag = false;
for(int i=0; i<ssub.length(); i++){ char c = ssub.charAt(i);
if(ssub.indexOf(c,i+1)!=-1){
reflag = true;
break;
}
} if(!reflag){
return len;
}
} }
return max;
} }

2、依次查找长度为s.length--的所有子串,每查到一个就进行检测。

可惜以上两种方案仍然不能通过测试,超时报错!

无奈,在网上找到解决方法:

public class Solution {
public int lengthOfLongestSubstring(String s) {
int length = 0;
String subString = new String();
int[] indices = new int[s.length()];
int lenSub = 0;
int index = 0;
//part1
for (int i = 0; i < s.length(); i++) {
int j = i;
index = s.indexOf(s.charAt(i), ++j);
if (index < 0) {
index = s.length();
}
indices[i] = index;
}
    //part2
for (int i = 0; i < indices.length; i++) {
int index1 = i;
int index2 = indices[i];
if (index2 >= indices.length) {
index2 = indices.length - 1;
}
lenSub = 0;
for (; index2 != index1; index1++) {
lenSub++;
if (indices[index1] < index2) {
index2 = indices[index1];
}
}
if (lenSub >= length) {
length = lenSub;
}
}
    //part3
int lenSub1 = 0;
for (int j2 = 0; j2 < indices.length; j2++) { if (indices[j2] == s.length())
lenSub1++;
else
lenSub1 = 0; }
if (lenSub1 > length)
return lenSub1;
else
return length;
}
}

上面这段代码可以分成三个部分看:

part1完成int[] indices的赋值操作,part2和part3其实是对应两种情况.

以字符串“aacdxdabcee”为例,代码执行过程如下

T代表10,B代表11
T
a a c d x d a b c e e
int[] indices:
B B B B B T B i=:
index1
index2
indeces[index1]
lensub indexes[index1]和上一次循环的index2比较 i=:
index1 5exit
index2
indeces[index1] B
lensub i=:
index1 5exit
index2
indeces[index1] B
lensub i=:
index1 5exit
index2
indeces[index1] T
lensub i=:
index1 Texit
index2 T T T T T T
indeces[index1] B B B B B T
lensub ....循环执行完length=; 下面来看lensub1对应的循环
j2 T
lensub1:

对于上面这这种类型的字符串part1的长度肯定是大于part2的

但对于下面这种无重复的字符串就要依靠part2啦

a b c d e f
int[] indices:
part2中这段代码的缘故
if (index2 >= indices.length) {
index2 = indices.length - 1;
}
index2 被赋值为5
所以part2中循环执行完lensub也只被赋值到5

Longest Substring Without Repeating Characters2015年6月9日的更多相关文章

  1. leetcode: longest substring without repeating characters

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

  2. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

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

    这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...

  4. 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...

  5. LeetCode[3] Longest Substring Without Repeating Characters

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

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

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

  7. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  8. 3. Longest Substring Without Repeating Characters(c++) 15ms

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  9. 【leetcode】Longest Substring Without Repeating Characters

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

随机推荐

  1. Letter Combinations of a Phone Number:深度优先和广度优先两种解法

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  2. "!function",自执行函数表达式

    如题为自执行函数表达式.在这种情况下,解析器在解析function关键字的时候,会将相应的代码解析成function表达式,而不是function声明.下面2个括弧()都会立即执行 (function ...

  3. html/css/javascript的含义、作用及理解

    HTML(HyperText Markup Language/超文本标记语言) 含义:HTML是一种用于创建网页的标准标记语言. 作用:页面内可以包含图片.链接,甚至音乐.程序等非文字元素. 理解:主 ...

  4. python 实例方法,类方法和静态方法

    在学习python代码时,看到有的类的方法中第一参数是cls,有的是self,经过了解得知,python并没有对类中方法的第一个参数名字做限制,可以是self,也可以是cls,不过根据人们的惯用用法, ...

  5. 基本数据结构——堆(Heap)的基本概念及其操作

    基本数据结构――堆的基本概念及其操作 小广告:福建安溪一中在线评测系统 Online Judge 在我刚听到堆这个名词的时候,我认为它是一堆东西的集合... 但其实吧它是利用完全二叉树的结构来维护一组 ...

  6. 任务十二:学习CSS 3的新特性

    任务目的 学习了解 CSS 3 都有哪些新特性,并选取其中一些进行实战小练习 任务描述 实现 示例图(点击查看) 中的几个例子 实现单双行列不同颜色,且前三行特殊表示的表格 实现正常状态和focus状 ...

  7. Java 中的接口有什么作用?好处?

    接口的作用就是把使用接口的人和实现接口的人分开,实现接口的人不必要关心谁去使用,而使用接口的人也不用关心谁实现的接口,由接口将他们联系在一起. 很多JAVA初级程序员对于接口存在的意义很疑惑.不知道接 ...

  8. mui开发app之联网应用传输数据

    手机的app分为,在线和单机,在线就是类似于C/S模式,能与服务器与他人共享数据的程序,单机就是在没有网络下可以玩转的app. 目前互联网盛行的时代,99%的程序都是联网环境下工作的.那么如何开发本地 ...

  9. 【算法系列学习】Dijkstra算法变形 [kuangbin带你飞]专题四 最短路练习

    https://vjudge.net/contest/66569#problem/B 类试题:noip2013 货物运输 POJ 1797 Heavy Transportation 方法一:Dijks ...

  10. JDBC连接错误(Illegal mix of collations。。。)

    连接java和mysql时出现了这样的报错: java.sql.SQLException: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) ...