Longest Substring Without Repeating Characters2015年6月9日
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日的更多相关文章
- leetcode: longest substring without repeating characters
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)
这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 3. Longest Substring Without Repeating Characters(c++) 15ms
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
随机推荐
- 关于block使用的几点注意事项
1.在使用block前需要对block指针做判空处理. 不判空直接使用,一旦指针为空直接产生崩溃. if (!self.isOnlyNet) { if (succBlock == NULL) { // ...
- Python练习册 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-),(http://tieba.baidu.com/p/2166231880)
这道题是一道爬虫练习题,需要爬链接http://tieba.baidu.com/p/2166231880里的所有妹子图片,点进链接看一下,这位妹子是日本著名性感女演员--杉本由美,^_^好漂亮啊,赶紧 ...
- C#中的ArrayList
ArrayList非常类似于数组,也有人称它为数组列表,ArrayList可以动态维护 提示: 和数组相似,ArrayList中存储的数据称为元素,ArrayList可以保存的元素数就是ArrayLi ...
- html5脚本编程
(1)跨文档消息传递,XDM.指的是来自不同域的页面间传递消息. XDM的核心是postMessage();向另一个地方传递数据,指是包含在当前页面中的iframe元素,由当前页面弹出的窗口. var ...
- callLater
UIComponent的方法,该方法在每次更新屏幕之前,Flash Player 或 AIR 都会调用为更新预定的函数集.有时,应在下次更新时调用函数,以执行为当前更新预定的其余代码.部分功能(如效果 ...
- 【Spark2.0源码学习】-4.Master启动
Master作为Endpoint的具体实例,下面我们介绍一下Master启动以及OnStart指令后的相关工作 一.脚本概览 下面是一个举例: /opt/jdk1..0_79/ ...
- windows的bat脚本
一个小小的设置固定ip和关闭防火墙的脚本: @echo //-=-=-=-=-=-=-=-=-=-=-=-=-=-=@echo // [固定设置]@echo // 设置IP,子网掩码,网关@echo ...
- Java计算两个程序运行时间
一.获取系统当前时间 long startTime = System.currentTimeMillis(); //获取开始时间 doSomething(); //测试的代码段 long endTim ...
- 转Fiddler 构造http请求
今天使用Fiddler构造一个POST请求,server端的PHP脚本的 $_POST数组中怎么也获取不到值,后来偶然发现是因为缺少了一个http头:Content-Type: application ...
- GitHub 入门不完全指南(未完待续)
我一直认为 GitHub 是一座宝藏,想让更多人的知道它.加入到这个社区中.本人能力有限,如果文中出现不对的地方,欢迎指正交流. 一.前言 大家好,我是削微寒(xuē wēi hán),一个走在进阶路 ...