Description:

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.

给定一个字符串,求字符串中最长不重复的字串长度。

又是一道没有数据范围的题目,这里字符串长可以超过3w,所以暴力不可搞。最后用很巧妙的运用起点和重点位置的 顺序选择,和对字符先后出现位置的统计,来求得结果。

class Solution {

public:

int lengthOfLongestSubstring(string s) {

int maxLen = 0, start = -1;

map<char, int>mapChar;

for (int i = 0; s[i]; i ++) {

if (mapChar.count(s[i])) {//这里学了一招,map的count方法可以统计对应类型出现的次数,不用再dt的初始化了

start = max(start, mapChar[s[i]]);

}

mapChar[s[i]] = i;

maxLen = max(maxLen, i - start);

}

return maxLen;

}

};

最新文章

  1. PHP中的魔术方法(2)
  2. 最新微信公众平台js sdk整合PHP版
  3. [JavaEE]调用Restful Service 出现415 Unsupported Media Type的问题(Rest Request Header中的Content-Type问题)
  4. ACM/ICPC 之 数据结构-邻接表+BFS(TSH OJ-无线广播Broadcast)
  5. http协议读书笔记2-连接管理
  6. POJ 1002 487-3279
  7. 安卓开发service
  8. Oracle行转列实例
  9. 编译C语言单元测试框架CUnit库的方法
  10. MyBatis之级联——一对多关系
  11. JSP的执行原理
  12. Asp.Net WebAPI核心对象解析(三)
  13. Android 音视频开发(二):使用 AudioRecord 采集音频PCM并保存到文件
  14. LINUX内核分析第八周学习总结
  15. 潭州课堂25班:Ph201805201 第五课:格式化输出和深浅复制 (课堂笔记)
  16. node使用 mongoose聚合 group
  17. php包含那点事情[WOOYUN]
  18. C# online update demo
  19. 【Python】print array时array中间是省略号没有输出全部的解决方法
  20. Luogu 3373 - 【模板】线段树 2 - [加乘线段树]

热门文章

  1. change legend layout from &#39;vertical&#39; to &#39;horizontal&#39; in Paraview
  2. RAII手法封装互斥锁
  3. BZOJ 1468 Tree 【模板】树上点分治
  4. 51NOD 1183编辑距离(动态规划)
  5. 2.5.5 基本的 I/0 重定向
  6. Luogu P1257 平面上的最接近点对 暴力
  7. Sql语句中关于如何在like &#39;%?%&#39;中给?赋值
  8. ZOJ 3868 GCD Expectation (容斥+莫比乌斯反演)
  9. 万能存储工具类SDCard存储 /data/data/存储 assets存储 raw存储
  10. 拉普拉斯分布,高斯分布,L1 L2