Problem:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

意思是给定字符串s, 返回最长的没有重复字符的字符串的长度。例如“abcddd”就是“abcd”长度为4,这题我自己试了试发现不是错误就是超时。最终还是不得不参考了其他大神。代码如下:

class Solution
{
public:
int lengthOfLongestSubstring(string s)
{
int start = ;
int max = ;
int len = s.length();
if( len == )
return ;
if (len ==)
return ; for (int i = ; i < len; i++)
{
for (int j = i - ; j >= start; j--) // 注意了是 >= 忘记=就错了
{
if (s[i] == s[j])
{
start = j + ;
break;
}
else
{
if (max < i - j + )
max = i - j + ;
}
}
}
return max;
}
};

如果输入的字符串为空后者为一,可以直接返回长度零或者1。

如果输入的长度大于1,那么我们就可以从下标1个开始(因为下标是从零开始的,所以下标1其实是第二个)

每次都只需要判断当前往回回溯到start点有没有重复字符。注意了这里的start不总是指第一个字符,而是根据相同字符在变。

举个例子吧:abcabcdf

第一次的时候当前的为第二个字符,也就是b,这时前面只有一个a,没有重复,所以记录i-j+1为2到max中,同样c的时候max就是3了,再到第二个a的时候,我们可以注意到现在的最大长度已经在刚才c的时候记录了。这时因为重复了这是第一次发现有重复的字符,所以我们的答案中肯定不可能包含这两个重复字符(因为题目要求就是不重复)。那么由于前面的可能的最大字符已经记录在max了。我们只需把start点定位到第一个重复字符的下一位,此例中是b。

同样在继续当前已经到第二个b了发现重复,再把start点定位到c,继续,当前到第二个c,又重复,则start定位到a,当前点到d,此时i-j+1是4,更新max,最后当前到f,max更新为5

所以此例最终答案为5.就是这样了。

leetcode第三题--Longest Substring Without Repeating Characters的更多相关文章

  1. leetcode第三题Longest Substring Without Repeating Characters java

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

  2. LeetCode第三题—— Longest Substring Without Repeating Characters(最长无重复子字符串)

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

  3. LeetCode 第 3 题(Longest Substring Without Repeating Characters)

    LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...

  4. 刷题之路第三题--Longest Substring Without Repeating Characters

    问题简介:求给定字符串中最长的字符不重复的字符串的长度 问题详解: 给定一个字符串,寻找给定字符串中包含的最长的字符不重复的字符串的长度 注:答案必须是子字符串,不是子序列 是连续的字符不重复的字符串 ...

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

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

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

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

  7. LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters

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

  8. LeetCode(3)Longest Substring Without Repeating Characters

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

  9. LeetCode Hash Table 3. Longest Substring Without Repeating Characters

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

随机推荐

  1. BZOJ 2947 Poi2000 促销 set

    标题效果:特定n天,首先插入一些每天.然后去掉最高值和最低值,要付出最大的值-至少值价格.乞讨n总天数支付的价格 堆/段树/平衡树光秃秃的标题 #include <set> #includ ...

  2. C代码分析器(一个 公开赛冠军)

    最近心血来潮,我希望能写一个通用的代码分析工具(其实这个词有点太.事实上为C代码).看到这几天我看到代码头晕眼花,尽管Source Insight救命,仍然没有足够的智慧思考很多地方. 如今主要遇到的 ...

  3. zTree实现访问到第一节点在相同水平当前所选节点数目

    zTree实现访问到第一节点在相同水平当前所选节点数目 1.实现源代码 <!DOCTYPE html> <html> <head> <title>zTr ...

  4. 为网站添加IE6升级提示

    原文 为网站添加IE6升级提示 IE6的是一款横跨十年的浏览器,作为一枚前端,对其已经失望透顶,但其在中国的市场比仍旧很高,中国大量的PC上安装的都是盗版Windows XP,而Windows XP上 ...

  5. (大数据工程师学习路径)第一步 Linux 基础入门----简单的文本处理

    介绍 这一节我们将介绍这几个命令tr(注意不是tar),col,join,paste.实际这一节是上一节关于能实现管道操作的命令的延续,所以我们依然将结合管道来熟悉这些命令的使用. 一.常用的文本处理 ...

  6. C 这些东西的内存管理

    一.内存介绍 本文主要介绍C内存管理基本概念,以及C语言编译后的可执行程序的存储结构和执行结构. 在用户存储空间,一个C程序的在内存中的分配分类5大部分:代码段.全局已初始化数据段.bss段.堆和栈. ...

  7. 【android】ImageView的src和background以及两者之间的神奇的差异

    一.ImageView中XML属性src和background的差别: background会依据ImageView组件给定的长宽进行拉伸.而src就存放的是原图的大小,不会进行拉伸.src是图片内容 ...

  8. ftp server来源分析20140602

    ftp  server学习位和源代码分析片 记录自己的第一个开源的分析过程: 从源代码:野狐灯(我接下来的几篇文章是从源头:野狐灯,每个以下哪项不是他们设置.) 20140602 Ftp的源码目录例如 ...

  9. oracle_五千万数据插入测试

    --创建表 tab_a -- create table tab_a (id int primary key not null,pid int); --创建序列 /** create sequence ...

  10. oracle创建user具体指示

    一个.用户的概念 用户,这是user,通俗的讲就是参观oracle数据库"人".在oracle在.的各种安全参数的用户可控制,为了保持数据库的安全性,的概念包括模型(schema) ...