3. Longest Substring Without Repeating Characters : LeetCode 题解
来源:互联网 发布:linux安装php环境 编辑:程序博客网 时间:2024/06/15 23:09
Problem Description:
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.
- C++ Solution:
- 从网上查阅了一些资料,并对
代码
进行了改进,然而很遗憾,并没有通过提交
……
或许换一种思想来提高速度
,是非常有必要的。
第一次尝试(提交失败
):
class Solution {public: int lengthOfLongestSubstring(string s) { int len = s.length(); int Max_Len = 0; int index = 0; for (int i = 1; i < len; i++) { map<char,int> MAP; MAP[s[i]] = 1; int Count = 1; for (int j = i-1; j >= index; j--) { if ( MAP.find(s[j]) == MAP.end() ) { MAP[s[j]] = 1; Count++; } else { index = j+1; break; } }//For()-1.1 End. Max_Len < Count ? (Max_Len=Count) : 0; }//For()-1 End. return Max_Len; }};
第二次尝试(提交成功
):
class Solution {public: int lengthOfLongestSubstring(string s) { int len = s.length(); int Max_Len = 0; int index = 0; for (int i = 1; i < len; i++) { int Count = 1; for (int j = i-1; j >= index; j--) { if ( s[i] == s[j] ) { index = j+1; break; } else Count++; }//For()_1.1 End. Max_Len < Count ? (Max_Len=Count) : 0; }//For()_1 End. return Max_Len; }};
备注:答案多有借鉴,请多担待
0 0
- LeetCode题解:Longest Substring Without Repeating Characters
- LeetCode题解:Longest Substring Without Repeating Characters
- Leetcode题解:Longest Substring Without Repeating Characters
- [LeetCode]Longest Substring Without Repeating Characters题解
- LeetCode题解--3. Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters : LeetCode 题解
- Leetcode题解(Python): 3.Longest Substring Without Repeating Characters
- LeetCode题解:3. Longest Substring Without Repeating Characters
- leetcode 题解 3. Longest Substring Without Repeating Characters
- Leetcode题解 - 3. Longest Substring Without Repeating Characters
- LeetCode Algorithms 3. Longest Substring Without Repeating Characters 题解
- leetcode题解-3. Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters LeetCode题解
- LeetCode题解(Week 16):3. Longest Substring Without Repeating Characters
- LeetCode 3. Longest Substring Without Repeating Characters题解python
- Leetcode题解-3. Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters题解
- [LeetCode]3.Longest Substring Without Repeating Characters
- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- C库函数-----sin()
- 心血来潮 写一篇关于python list 的用法
- ASP.NET简介
- IIS发布wcf服务后,点击svc不能再浏览器中打开,出现直接下载的情况的解决方案
- 3. Longest Substring Without Repeating Characters : LeetCode 题解
- 开源项目共享平台推荐
- php实现excel的导出
- linux下目录结构与常用命令
- runtime简介
- java文件输入输出流
- Apache-tomcat-7.0.65\bin\tcnative-1.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform]
- HDU 4350 (生成树计数 判断点在线段上)
- 【java设计模式】之 建造者(Builder)模式