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. Excel公式-求最低价网站名字

    p{ font-size: 15px; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid #aaa; width: 99%; ...

  2. 老李分享:android手机测试之适配(2)

    但 Android 版本低于 3.2 的设备不支持此技术,原因是这些设备无法将 sw600dp 识别为尺寸限定符,因此我们仍需使用 large 限定符.这样一来,就会有一个名称为 res/layout ...

  3. 浩哥解析MyBatis源码(六)——DataSource数据源模块之池型数据源

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6675674.html 1 回顾 上一文中解读了MyBatis中非池型数据源的源码,非池型也 ...

  4. App forensics

    A friend of mine claimed that someone stole her personal data via hacking certain App. She installed ...

  5. [笔记]Learning to Rank算法介绍:RankNet,LambdaRank,LambdaMart

    之前的博客:http://www.cnblogs.com/bentuwuying/p/6681943.html中简单介绍了Learning to Rank的基本原理,也讲到了Learning to R ...

  6. C# 遍历泛型集合

    /// <summary> /// 遍历泛型 /// </summary> /// <typeparam name="T"></typep ...

  7. ios deprecated 警告消除 强迫症的选择

    #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" ...

  8. Angular.js学习笔记(三)

    一.过滤器 1.uppercase,lowercase 大小写转换{{ "lower cap string" | uppercase }} // 结果:LOWER CAP STRI ...

  9. require.js学习笔记

    使用require.js的好处? 1 有效的防止命名冲突(可以将变量封装在模块内,通过暴露出的接口解决命名冲突) 2 解决不同JS文件中的依赖 3 可以让我们的代码以模块化的方式组织 官方网站http ...

  10. delphi 7 mdi子窗体。。。无法更改以命令对象为源的记录集对象的 ActiveConnection 属性。

    问题是这样的 我做了一个小程序 把 adoconnection放到了主窗体  连接的是access数据库; 新建了一个子窗体继承自FBase  新建了一个pubulic方法 qrySearch 实现了 ...