拖延症太严重了TAT,真心要处理一下这个问题了,感觉很不好!

----------------------------------------------------------------------------------------------------------------

Longest Substring Without Repeating Characters

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.

【题意】:这道题的意思是说,给你一个字符串,让你找出最长字串要求没有重复字符的长度。比如,

字符串“abcabcbb”里最长的不重复字符串是“abc”,所以最后的答案是3

【心路历程】:刚开始看这道题的时候其实没啥想法,最直接的想法是枚举,O(n*2)的时间复杂度,肯定是不行的,就算能过

也不是最好的,所以直接pass掉这种想法。于是,开始往dp的方向想,想了一会发现找不到可以构建的动态转移方程。

之后就是深陷思考的漩涡中。。。

后来考虑用一个数组index记录当前位置下每个字符最后出现的位置,一个下标cur维护当前的字符串开始位置。

遍历一遍字符串,当这个字符没出现过时,cur不变,最大长度加1。

当这个字符出现过时,cur变为字符上次出现位置加1。不断维护没有重复字符的一个字符串,和一个最大长度的变量。

----------------------------------------------------------------------------------------------------------------------

代码如下:

 int lengthOfLongestSubstring(char* s) {
int index[];
int len = strlen(s);
int i,max = ,ans = ,cur = ;
memset(index,,sizeof(index));
for( i = ; i < len; i++) {
int c = (int)(s[i]);
if(!index[c] || index[c] < cur){
index[c] = i+;
max = i - cur + ;
if(max > ans) ans = max;
}else {
cur = index[c] + ;
max = i+ - index[c];
index[c] = i + ;
if(max > ans) ans = max;
}
}
return ans;
}

一起刷LeetCode3-Longest Substring With Repeating Characters的更多相关文章

  1. Leetcode3:Longest Substring Without Repeating Characters@Python

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

  2. LeetCode3 Longest Substring Without Repeating Characters

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

  3. 最长子串(Leetcode-3 Longest Substring Without Repeating Characters)

    Question: Given a string, find the length of the longest substring without repeating characters. Exa ...

  4. 滑动窗口解决最小子串问题 leetcode3. Longest Substring Without Repeating Characters

    问题描述: Given a string, find the length of the longest substring without repeating characters. Example ...

  5. Leetcode3.Longest Substring Without Repeating Characters无重复字符的最长字串

    给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 无重复字符的最长子串是 "abc",其长度为 ...

  6. LeetCode3:Longest Substring Without Repeating Characters

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

  7. (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...

  8. [Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters

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

  9. 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...

  10. 周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)

    这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a ...

随机推荐

  1. TCL语言笔记:TCL过程控制练习

    1.青蛙爬井 for {set day 1;set cur 7} {$cur<77} {incr cur 2 ;incr day } { } puts $day 2.求1加到100 proc g ...

  2. 3.Spring-用反射模拟IoC

    1.BeanFactory.java package com.jike.spring.chapter03.reflect; import java.io.InputStream; import jav ...

  3. CentOS安装VSFTP及配置用户

    第一步,安装vsftp # 1.以管理员(root)身份执行以下命令 yum install vsftpd # 2.设置开机启动vsftpd ftp服务 chkconfig vsftpd on # 3 ...

  4. SELinux开启与关闭

    SELinux是「Security-Enhanced Linux」的简称,是美国国家安全局「NSA=The National Security Agency」 和SCC(Secure Computin ...

  5. Android学习之-TextView的滑动效果

    textView中如何设置滚动条 在xml中定义: <TextView            android:layout_width="wrap_content"      ...

  6. Java API —— IO流(数据操作流 & 内存操作流 & 打印流 & 标准输入输出流 & 随机访问流 & 合并流 & 序列化流 & Properties & NIO)

    1.操作基本数据类型的流     1) 操作基本数据类型 · DataInputStream:数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型.应用程序可以使用数据输出 ...

  7. C++RAII

    http://baike.baidu.com/view/1120455.htm?fr=aladdin http://www.cnblogs.com/gnuhpc/archive/2012/12/04/ ...

  8. SQL语句练习

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  9. android自动更新软件版本

    根据网上的然后小改 import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import j ...

  10. Main()方法

    C#是从方法Main()开始执行的.这个方法必须是类或结构的静态方法,并且其返回类型必须是int或void .虽然显式指定p山屺修饰符是很常见的,因为按照定义,必须在程序外部调用该方法,但我们给该入口 ...