3.Longest Substring Without Repeating Characters
2016-07-25 15:54
357 查看
for example:
Given
which the length is 3.
Given
with the length of 1.
Given
with the length of 3. Note that the answer must be a substring,
a subsequence and not a substring.
code:
time complexity o(2n)
think:
clear_thought is the key to clear_code
Given
"abcabcbb", the answer is
"abc",
which the length is 3.
Given
"bbbbb", the answer is
"b",
with the length of 1.
Given
"pwwkew", the answer is
"wke",
with the length of 3. Note that the answer must be a substring,
"pwke"is
a subsequence and not a substring.
code:
time complexity o(2n)
think:
clear_thought is the key to clear_code
class Solution { public: int lengthOfLongestSubstring(string str) { int result = 0; int i = 0; int j = 0; int len = str.length(); set<char> s; while(i<len && j < len){ if(s.count(str[j])==0){ s.insert(str[j]); j++; result = (j-i)>result?(j-i):result; }else{ s.erase(str[i]); i++; } } return result; } };
相关文章推荐
- Linux进程通信(四)IPC共享内存
- uva10943 How do you add?(隔板法+递推)
- textview加线
- The near side of the pie.
- 深度学习CNN用于目标检测的方法总结
- 学习《spring 3.x企业应用开发实战》之Spring的事务管理
- python 生成ssh秘钥对
- **一个好幼稚的javascript变量加减的小东西**
- [专题训练]数论专题1
- WIFI RSSI
- shell--3.运算符
- 使用Windows service创建一个简单的定时器
- C#中DataGridView控件使用大全
- Linux学习----开启篇
- Android Studio 中ADB WIFI 插件
- 为Paint设置渐变器
- 动态规划思想
- Git 工作区与缓存区的文件回退(06)
- linux下安装jdk
- java文件流——文件编码