拖延症太严重了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. 一个酷炫的,基于HTML5,Jquery和Css的全屏焦点图特效,兼容各种浏览器

    基于HTML5和CSS的焦点图特效,梅花图案的背景很有中国特色,而且还会动哦,效果超炫,推荐下载! 演示图 html代码 <!DOCTYPE html PUBLIC "-//W3C// ...

  2. C# 使用AutoResetEvent进行线程同步

    AutoResetEvent 允许线程通过发信号互相通信. 通常,当线程需要独占访问资源时使用该类. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号. 如果 AutoRe ...

  3. Java API —— IO流小结

    练习题: 1.复制文本文件 package cn.itcast_01; import java.io.BufferedReader; import java.io.BufferedWriter; im ...

  4. MongoDB 安装和即基本操作

    http://www.mongodb.org/ Agile and Scalable MongoDB (from "humongous") is an open-source do ...

  5. (sql server)数据分页的实现

    谈谈自己了解的几种数据库分页的方法,下面來分享下,有什么好的方法可以指导一下哦.. 方法一:利用ROW_NUMBER()方法 利用ROW_NUMBER 产生序列后直接取出 /*如要查詢的表為Test ...

  6. BZOJ 2154 Crash的数字表格

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2154 题意: 思路: i64 mou[N]; void init(int N){    ...

  7. Jquery的.post说解

    Jquery的.post说解(一) 准备工作 ·Customer类   public class Customer {     public int Unid { get; set; }     pu ...

  8. NuGet在2015中的使用

    NuGet Package Restore  https://docs.nuget.org/Consume/Package-Restore 以https://github.com/andburn/hd ...

  9. R语言字符串函数

    字符串长度: nchar("hello world") #字符串连接:paste) <- value substr("abcdef", 2, 4)[1] ...

  10. bzoj1563

    P<=10一开始是吓死我了 后来想到这就是一个经典的决策单调性解决1d1d动态规划的题目 像决策单调性完全可以打表找规律,这里有一篇严谨的证明https://www.byvoid.com/blo ...