leetcode-【中等题】3. Longest Substring Without Repeating Characters

时间:2021-06-12 02:28:57

题目:

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的更多相关文章

  1. 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...

  2. 【LeetCode每天一题】Longest Substring Without Repeating Characters&lpar;最长无重复的字串&rpar;

    Given a string, find the length of the longest substring without repeating characters. Example 1:    ...

  3. Leetcode第三题《Longest Substring Without Repeating Characters》

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  4. 【LeetCode】3 、Longest Substring Without Repeating Characters

    题目等级:Medium 题目描述:   Given a string, find the length of the longest substring without repeating chara ...

  5. Leetcode经典试题:Longest Substring Without Repeating Characters解析

    题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...

  6. LeetCode解题笔记 - 3&period; Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  7. LeetCode(3)Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  8. 刷题3&period; Longest Substring Without Repeating Characters

    一.题目 Longest Substring Without Repeating Characters,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug ...

  9. LeetCode Hash Table 3&period; Longest Substring Without Repeating Characters

    HashMap的应用可以提高查找的速度,键key,值value的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...

  10. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

随机推荐

  1. 如何防止应用因获取IDFA被AppStore拒绝

    由于Appstore禁止不使用广告而采集IDFA的app上架,友盟提供IDFA版和不含IDFA版两个SDK,两个SDK在数据上并没有差异,采集IDFA是为了防止今后因为苹果可能禁止目前使用的openu ...

  2. c&plus;&plus;防止客户端多开巧妙代码

    在读OBS源码时看到一个比较有意思的关于防止用户多开程序的写法,简单有效,记录下 //make sure only one instance of the application can be ope ...

  3. python计算文件的行数和读取某一行内容的实现方法

    一.计算文件的行数 最简单的办法是把文件读入一个大的列表中,然后统计列表的长度.如果文件的路径是以参数的形式filepath传递的,那么只用一行代码就可以完成我们的需求了:count = len(op ...

  4. 依赖注入及AOP简述(七)——FQCN请求模式

    2.2.    FQCN请求模式 为了弥补纯字符串请求模式中的类型安全问题,全类名(FQCN)请求模式就应运而生了.其思想便是,在向容器请求依赖对象的时候,不是通过字符串的标识符.而是通过被请求的依赖 ...

  5. Linux c 信号量

    信号量(通过进程通信实现进程间的同步) 信号量(semaphore)信号灯 信号量是共享内存整数数组.根据需要定义指定的数组长度 信号量就是根据数组中的值,决定阻塞还是解除阻塞 编程模型: 1.    ...

  6. cglib源码学习交流

    背景 前段时间在工作中,包括一些代码阅读过程中,spring aop经常性的会看到cglib中的相关内容,包括BeanCopier,BulkBean,Enancher等内容,以前虽大致知道一些内容,原 ...

  7. ISAPI&lowbar;Rewrite 3 伪静态软件

    下载地址 http://www.helicontech.com/isapi_rewrite/download.html

  8. Program terminated with signal SIGKILL&comma;Killed

    车载后视镜机器,Liinux + qtUI形式,前后双路,前一天晚上开机用gdb run DvrUI,第二天早上回来一看,机器绿屏卡死了,录像预览停止刷新了,sd录像也停止了.点击无任何反应. 看gd ...

  9. Strategic Game

    Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...

  10. &lbrack;20171120&rsqb;关于INBOUND&lowbar;CONNECT&lowbar;TIMEOUT设置&period;txt

    [20171120]关于INBOUND_CONNECT_TIMEOUT设置.txt --//上午翻看以前我的发的帖子,发现链接:http://www.itpub.net/thread-2066758- ...