拖延症太严重了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. 用ISO C++实现自己的信号槽(Qt另类学习)

    qtc++objectsignalclassstring   目录(?)[-] Qt信号与槽 引入元对象系统 建立信号槽链接 信号的激活 槽的调用 全家福 零零散散写在后面 Q_OBJECT Conn ...

  2. WCF入门(四)---WCF架构

    WCF是一个分层架构,为开发各种分布式应用的充分支持.该体系结构在下面将详细说明. 约定 约定层旁边就是应用层,并含有类似于现实世界的约定,指定服务和什么样的信息可以访问它会使操作的信息.约定基本都是 ...

  3. iOS KVO的原理

    KVO(Key Value Observing),是观察者模式在Foundation中的实现.   KVO的原理   简而言之就是:   1.当一个object有观察者时,动态创建这个object的类 ...

  4. *两个关键字static和final

    static关键字:可以用于修饰属性,也可以用于修饰方法,还可以用于修饰类. static 修饰属性: 无论一个类生成了多少个对象,所有这些对象共同使用唯一一份静态的成员变量:一个对象对该静态成员变量 ...

  5. zenmap 的扫描方式

    第一种:Intense scan (nmap -T4 -A -v) 一般来说,Intense scan可以满足一般扫描 -T4 加快执行速度 -A 操作系统及版本探测 -v 显示详细的输出 第二种:I ...

  6. WEBBROWSER中模拟鼠标点击(SendMessage/PostMessage)

    好久没有写文章,发一篇顶顶博客访问量.别人建议转一些比较好的代码也贴过来,但是我打算这里主要发自己原创的代码,所以么..流量该多少就多少吧... 回到主题,在webbrowser中点击某链接网上几乎都 ...

  7. Codeforces Round #262 (Div. 2) 二分+贪心

    题目链接 B Little Dima and Equation 题意:给a, b,c 给一个公式,s(x)为x的各个位上的数字和,求有多少个x. 分析:直接枚举x肯定超时,会发现s(x)范围只有只有1 ...

  8. 一台电脑同时运行多个tomcat配置方法

    当第一个tomcat启动后,后面tomcat的server.xml中的端口不管怎么改,仍然会报端口冲突.后来在dos下运行才发现所有的tomcat都会去找CATALINA_HOME和CATALINA_ ...

  9. asp.net实现GZip压缩和GZip解压

    最近在开发一个网站doc.115sou.com,使用到了GZip压缩技术,经过多次搜索找到asp.net中用GZip对数据压缩和解压缩非常方便,当我第一次拿到这个类的时候却感觉很迷茫,无从下手.主要是 ...

  10. LeetCode Single Number II 单元素2

    题意:给一个序列,其中只有1个元素只出现1次,其他的都一定出现3次.问这个出现一次的元素是多少? 思路: (1)全部元素拆成二进制,那么每个位上的1的个数应该是3的倍数,如果不是3的倍数,则ans的这 ...