leetcode-【中等题】3. Longest Substring Without Repeating Characters
题目:
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.
链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/
答案:
参考了这个链接后才明白解题思路,然后依据自己的理解写了代码。
大致思路是:从当前字符开始,往前看,没有出现过重复字符的字符串
代码:
#define MAX_LETTER_NUM 255
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if(s.empty())
{
return ;
} bool exist[MAX_LETTER_NUM];
int position[MAX_LETTER_NUM];
int index,rangeIndex;
int maxLen = ;
int start = ; for(index = ; index < MAX_LETTER_NUM; ++ index)
{
exist[index] = false;
position[index] = ;
} for(index = ; index < s.size(); ++ index)
{
if(exist[s[index]])
{
for(rangeIndex = start; rangeIndex <= position[s[index]]; ++ rangeIndex)
{
exist[s[rangeIndex]] = false;
}
start = position[s[index]] + ;
exist[s[index]] = true;
}else
{
exist[s[index]] = true;
maxLen = (maxLen < index - start + ) ? index - start + :maxLen;
} position[s[index]] = index;
} return maxLen;
}
};
leetcode-【中等题】3. Longest Substring Without Repeating Characters的更多相关文章
- 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...
- 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)
Given a string, find the length of the longest substring without repeating characters. Example 1: ...
- Leetcode第三题《Longest Substring Without Repeating Characters》
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- 【LeetCode】3 、Longest Substring Without Repeating Characters
题目等级:Medium 题目描述: Given a string, find the length of the longest substring without repeating chara ...
- Leetcode经典试题:Longest Substring Without Repeating Characters解析
题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...
- LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- LeetCode(3)Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- 刷题3. Longest Substring Without Repeating Characters
一.题目 Longest Substring Without Repeating Characters,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug ...
- LeetCode Hash Table 3. Longest Substring Without Repeating Characters
HashMap的应用可以提高查找的速度,键key,值value的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
随机推荐
- POJ1986 Distance Queries (LCA)(倍增)
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 12950 Accepted: 4577 ...
- postgresql修改最大连接数
1.合适的最大连接数 used_connections/max_connections在85%左右2.修改最大连接数postgresql最大连接数默认为1001)打开postgresql配置文件vim ...
- POSIX正则表达式
POSIX正则表达式规范 参考:http://en.wikipedia.org/wiki/Regular_expression POSIX正则表达式分为Basic Regular Expression ...
- Java--super关键字用法
看了马老师的视频,百度了一下,随即敲了一些代码,super是超人,超级的意思,层面上理解为父类 class Person { Person(){ System.out.prin ...
- List的Capacity
Capacity 在.NET中List的容量应该只是受到硬件限制. 属性Capacity的真正含义,是创建List时给它预分配的容量. 一旦项的数量超过了当前的Capacity,Capacity会以 ...
- Svg path画线(不管是直线还是曲线)在一定情况下线条的宽度不一的情况(记录)
在项目中涉及到svg: 使用path划线实现图表功能. 记录在实现的过程中发现的问题:path在小像素的情况下画出的线条宽度不一样.这是为什么呢? 以下是我做的猜想: 可以看图 在宽度给的很足的时候没 ...
- OWIN是什么?
OWIN的英文全称是Open Web Interface for .NET. 如果仅从名称上解析,可以得出这样的信息:OWIN是针对.NET平台的开放Web接口. 那Web接口是谁和谁之间的接口呢?是 ...
- STM32固件库3.5+uCOS2.86移植(转自暴走的工程师)
考了很多移植的资料和代码,终于移植好了...应该是完美移植吧~~哈哈哈~~ 编译环境是IAR 工程适用于STM32F10X大容量产品,如果不是,请自行修改启动文件和工程配置 编译器优化等级最高...这 ...
- 【zz】MIT牛人解说数学体系
作者:林达华 一.为什么要深入数学的世界 作为计算机的学生,我(原作者)没有任何企图要成为一个数学家.我学习数学的目 的,是要想爬上巨人的肩膀,希望站在更高的高度,能把我自己研究的东西看得更深广一些. ...
- U3V第三方软件驱动路径
NI驱动位置:C:\Program Files (x86)\National Instruments\NI-IMAQdx\Staging\NI USB3 VisionA&B驱动位置:D:\Pr ...