拖延症太严重了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. Android:改变Activity切换方式

    overridePendingTransition(enterAnim, exitAnim); Intent intent =new Intent(this,item2.class); startAc ...

  2. 几种C#程序读取MAC地址的方法

    原文:几种C#程序读取MAC地址的方法 以下是收集的几种C#程序读取MAC地址的方法,示例中是读取所有网卡的MAC地址,如果仅需要读取其中一个,稍作修改即可. 1 通过IPConfig命令读取MAC地 ...

  3. Maven找不到java编译器的问题

    当使用mvn package打包项目的时候,抛出下面这个错误: [ERROR] Unable to locate the Javac Compiler in: D:\jdk\..\lib\tools. ...

  4. PHP中该怎样防止SQL注入?

    因为用户的输入可能是这样的: ? 1 value'); DROP TABLE table;-- 那么SQL查询将变成如下: ? 1 INSERT INTO `table` (`column`) VAL ...

  5. openfire中mysql的前期设置

    使用openfire的时候如果需要使用自己的mysql数据库,需要提前进行设置,下面将记录下,基本的设置过程. 一.前期准备工作: 1.先下载两个工具一个是mysql数据库还有一个是SQLyog(可以 ...

  6. 制作SM2证书

    前段时间将系统的RSA算法全部升级为SM2国密算法,密码机和UKey硬件设备大都同时支持RSA和SM2算法,只是应用系统的加解密签名验证需要修改,这个更改底层调用的加密动态库来,原来RSA用的对称加密 ...

  7. 基于XMPP的即时通信系统的建立(三)— 程序设计概览

    XMPP与HTTP的比较 XMPP的优势 Ÿ   1. 推送数据 HTTP只能从服务器哪里请求数据,除非服务器正在响应客户端请求,否则不能向客户端发送数据.但XMPP连接是双向的,任何一方在任何时候都 ...

  8. UWP:本地应用数据

    获取应用的设置和文件容器 使用 ApplicationData.LocalSettings 属性可以获取 ApplicationDataContainer 对象中的设置. 注意:每个设置的名称最长可为 ...

  9. BZOJ2337: [HNOI2011]XOR和路径

    题解: 异或操作是每一位独立的,所以我们可以考虑每一位分开做. 假设当前正在处理第k位 那令f[i]表示从i到n 为1的概率.因为不是有向无环图(绿豆蛙的归宿),所以我们要用到高斯消元. 若有边i-& ...

  10. Eclipse @override报错解决 必须覆盖超类方法

    解决办法:Windows->Preferences-->java->Compiler-->compiler compliance level设置成1.6