LeetCode 3 Longest Substring Without Repeating Characters
2016-04-01 15:34
477 查看
题目:
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.
参考思路:从左向右扫描,扫描一个字母记录为True,同时right++,一旦遇到扫描过的字母,则将该字母记为false同时将lft++;(即跳过该字母,以他的后面第一个元素作为起点,从刚才的right继续往后扫描,找到最大的不重复子串)
leetcode给的参考代码。
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.
参考思路:从左向右扫描,扫描一个字母记录为True,同时right++,一旦遇到扫描过的字母,则将该字母记为false同时将lft++;(即跳过该字母,以他的后面第一个元素作为起点,从刚才的right继续往后扫描,找到最大的不重复子串)
leetcode给的参考代码。
class Solution { public: int lengthOfLongestSubstring(string s) { int n = s.length(); if (n<2) return n; int lft = 0, rgt = 0, maxlen = 0; bool sign[128] = { false }; while (rgt<n) { if (sign[(int)s[rgt]] == false) { sign[(int)s[rgt]] = true; rgt++; if (maxlen<rgt - lft) maxlen = rgt - lft; continue; } sign[(int)s[lft]] = false;//跳过这个字母扫描,假设这个字母没有出现过 lft++; } return maxlen; } };
相关文章推荐
- 经典算法——最长回文子串
- DeepID2+人脸识别算法学习
- 安卓按比例布局,layout_weight用法、合用weightSum属性和layout_weight属性
- 抽象类与接口的典型应用:猫狗案例,加入调高的额外功能
- Opencv 学习笔记之——鼠标操作画出感兴趣区域
- 视频框架Vitamio学习
- 【转】Android循环滚动广告条的完美实现,封装方便,平滑过渡,从网络加载图片,点击广告进入对应网址
- hdu2046(dp&&规律)
- Eclipse如何导出安卓apk文件
- FLV文件格式解析
- android:radius设置圆角失败的问题
- 设计模式——代理模式
- Android中的FrameLayout帧布局
- 四则运算网页版
- Android 禁止屏幕休眠和锁屏的方法
- VM虚拟机下给Ubuntu 目录分区增加容量的方法
- UI控件的事件监听
- 具有美感的API设计
- Tomcat发布多个项目时抛的webAppRootKey错误
- Codis编译