题目:

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(最长无重复的字串)

    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. 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. Longest Substring Without Repeating Characters

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

  9. LeetCode Hash Table 3. 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. 配置DNS服务器,实现局域网内的地址正反向解析

    这里会用到BIND(Berkeley Internet Name Domain) 首先查询下本地bind有没有安装 yum list all | grep "^bind" 如果没有 ...

  2. 继续OI

    NOIP2016于2016.11.20日12:00正式结束. 我作为oi的生涯 或许会结束? 或者继续? 然而前途依然迷茫,我是否应该继?或是放弃? 距离省选还有3~4个月,我该何去何从? 虽然已经经 ...

  3. System.DateUtils 4. IsValidDateTime... 有效时间判断

    编译版本:Delphi XE7 function IsValidDate(const AYear, AMonth, ADay: Word): Boolean;function IsValidTime( ...

  4. 开发时建议关闭chrome的缓存[Disable cache(while DevTools open)]

    问题:修改了css,JS后,chrome看不到效果 具体情景:最近在写一个PHPbbs,在写CSS时,习惯性的按F5刷新,firefox ie等,都能即时见到效果,唯独chrome 怎么都不行,想了想 ...

  5. js 常用方法

    1: 五秒后跳出提示框 setTimeout("alert('已经过了五秒了')",5000) 2:  十秒后自动关闭窗口 <script language="ja ...

  6. CentOS源 Ubuntu 源 OpenSUSE-----持续汇总

    CentOS 基础(常用)的源:http://dl.fedoraproject.org/pub/epel/epel-release-latest-5.noarch.rpmhttp://dl.fedor ...

  7. web异常流量定位:iftop+tcpdump+wireshark

    一个简单的运维小经验. 场景:web服务器出现异常流量,web集群内部交互出现大流量,需要定位具体的http请求,以便解决问题. 目的:找出产生大流量的具体http请求. 工具:        ift ...

  8. A 最熟悉的陌生人 (纪念当年就读的梅州市江南高级中学)

    最熟悉的陌生人 作者:张慧桥 “枪与玫瑰” 就象瘟98有时会死机天有时会下雨枪有时会走火美国战机有时会掉下来那样,我上网聊天也只是个偶然. 都是栀子那死丫头惹的祸.让每天都觉得是情人节的我那天我自己都 ...

  9. hdu3294 girl‘s research

    题目大意:有多组数据,每组数据给出一个字符和一个字符串.该字符将变成’a‘,表示字符串中的所有该字符将变成’a‘,同时其他字符也将做相同的偏移.具体来说,如果该字符为’b‘,表示字符串中的’b‘都将变 ...

  10. [系统开发] 一个基于Django和PureCSS的内容管理系统

    这是我刚开发的一套基于Django和PureCSS的内容管理系统,目标是优雅.简洁.实用,目前功能还在完善中. 系统参考了网上的教程,除了文章管理.搜索.RSS,还增加了类别管理.用户管理,以及评论管 ...