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 ...
随机推荐
- 关于WdatePicker.js的结束时间大于开始时间
简单笔记 : WdatePicker.js 要使结束时间大于开始时间只要在线束时间的 minDate:'#F{$dp.$D(\'stimeParam\')}' 即可:不多说 详细代码如下: <t ...
- ZEROC ICE 跨平台间程序调用 java版
前言: 本来建博客是为了和大家分享一些前端的开发经验和心得呢,但是阴差阳错,第一篇技术博客确实关于跨平台,跨语言服务端调用的解决方案---ZEROC ICE. 最近一个项目涉及到java.python ...
- JavaScript高级程序设计---学习笔记(三)
函数表达式 定义函数的方式有两种:一种是函数声明,另一种是函数表达式. 关于函数声明,它的一个重要特征就是函数声明提升,意思是在执行代码之前会先读取函数声明所以可以把函数声明放在调用它的语句后面. 而 ...
- Git版本切换
前面的话 本文将以一个简单实例的形式来介绍Git版本切换 初始版本 首先,在一个自定义的位置,创建目录a,比如在D盘下 [注意]本文会用到一些常用的Linux的Shell命令,详细信息移步至此 先使用 ...
- OCR文字识别帮助录入文字信息
OCR文字识别是指将图片.照片上的文字内容,直接转换为可编辑文本的过程.目前各行各业不断地应用文字识别产品,解决文字录入工作的烦恼,提高工作效率. OCR文字识别用在哪里? 一个做社区工作的朋友透露, ...
- 表格组件神器:bootstrap table详细使用指南
1.bootstrap-table简介 1.1.bootstrap table简介及特征: Bootstrap table是国人开发的一款基于 Bootstrap 的 jQuery 表格插件,通过简单 ...
- MySQL一个简单的存储过程demo
使用的工具是Navicat for MySQL. 首先创建一个学生表 mysql) ) ) auto_increment,age ) ) not null,primary key(s_no)); Qu ...
- jQuery使用小结
$(document).ready( function(){} ); 选择器 $("p:first") 第一个元素 $("p.intro" ...
- struts2 之 ThreadLocal 和 ActionContext
1. ThreadLocal:该类提供了线程局部(thtead-local)变量.threadLocal是一个容器,该容器中存放的数据可以保证线程安全. 案例如: public class Threa ...
- AngularJS学习笔记2
3.AngularJS 表达式 AngularJS 表达式写在双大括号内:{{ expression }}.AngularJS 表达式把数据绑定到 HTML,这与 ng-bind 指令有异曲同工之妙. ...