Longest Substring Without Repeating Characters
来源:互联网 发布:淘宝客知名论坛 编辑:程序博客网 时间:2024/06/03 16:58
Given a string, find the length of the longest substring without repeating characters.
Examples:
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.
只需要一次遍历
class Solution {public: int lengthOfLongestSubstring(string s) { int a[256]; memset(a,-1,sizeof(a)); int ans=0; int start=-1; for(int i=0;i<s.size();i++) { char c=s[i]; if(a[c]>start) start=a[c]; ans=max(ans,i-start); a[c]=i; } return ans; }};
还有一个复杂度高的算法
class Solution {public: int lengthOfLongestSubstring(string s) { int len=0; int pre=0; for(int i=0;i<s.size();i++) { unordered_map<char,int>hash; len=0; char ch; for(int j=i;j<s.size();j++) { ch=s[j]; if(hash.find(ch)!=hash.end()) break; hash[ch]=1; len++; } if(len>pre)pre=len; hash.clear(); } return pre; }};
阅读全文
0 0
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- java基础篇(一)————基本数据类型
- Java学习记录日志_0.Something For Nothing
- JDBC概述
- QQ音乐API整理
- SpringMVC学习-02天
- Longest Substring Without Repeating Characters
- 深度学习入门(五) 入门深度学习必看的系列教程
- CISC RISC ARM MIPS区别与联系
- IMWeb提升营Day3 | 训练题18:二叉树的镜像
- Kubernetes Scheduler源码分析--启动过程与多队列缓存
- 启动虚拟机错误 Transport(VMDB) error -44:Message
- Mayor's posters
- L1-005. 考试座位号
- idea创建项目报错 -Dmaven.multiModuleProjectDirectory system property is not