题目:
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 ...
随机推荐
-
如何防止应用因获取IDFA被AppStore拒绝
由于Appstore禁止不使用广告而采集IDFA的app上架,友盟提供IDFA版和不含IDFA版两个SDK,两个SDK在数据上并没有差异,采集IDFA是为了防止今后因为苹果可能禁止目前使用的openu ...
-
c++防止客户端多开巧妙代码
在读OBS源码时看到一个比较有意思的关于防止用户多开程序的写法,简单有效,记录下 //make sure only one instance of the application can be ope ...
-
python计算文件的行数和读取某一行内容的实现方法
一.计算文件的行数 最简单的办法是把文件读入一个大的列表中,然后统计列表的长度.如果文件的路径是以参数的形式filepath传递的,那么只用一行代码就可以完成我们的需求了:count = len(op ...
-
依赖注入及AOP简述(七)——FQCN请求模式
2.2. FQCN请求模式 为了弥补纯字符串请求模式中的类型安全问题,全类名(FQCN)请求模式就应运而生了.其思想便是,在向容器请求依赖对象的时候,不是通过字符串的标识符.而是通过被请求的依赖 ...
-
Linux c 信号量
信号量(通过进程通信实现进程间的同步) 信号量(semaphore)信号灯 信号量是共享内存整数数组.根据需要定义指定的数组长度 信号量就是根据数组中的值,决定阻塞还是解除阻塞 编程模型: 1. ...
-
cglib源码学习交流
背景 前段时间在工作中,包括一些代码阅读过程中,spring aop经常性的会看到cglib中的相关内容,包括BeanCopier,BulkBean,Enancher等内容,以前虽大致知道一些内容,原 ...
-
ISAPI_Rewrite 3 伪静态软件
下载地址 http://www.helicontech.com/isapi_rewrite/download.html
-
Program terminated with signal SIGKILL,Killed
车载后视镜机器,Liinux + qtUI形式,前后双路,前一天晚上开机用gdb run DvrUI,第二天早上回来一看,机器绿屏卡死了,录像预览停止刷新了,sd录像也停止了.点击无任何反应. 看gd ...
-
Strategic Game
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
-
[20171120]关于INBOUND_CONNECT_TIMEOUT设置.txt
[20171120]关于INBOUND_CONNECT_TIMEOUT设置.txt --//上午翻看以前我的发的帖子,发现链接:http://www.itpub.net/thread-2066758- ...