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.

题解:

  设置left来标定现无重复字母的字符串的开始位置,i 表示遍历的位置,则无重复字母的最大子字符串的长度为 i - left + 1, 字典中存储的是字母与其对应的字符串的位置(以0为起始点)。变量res存储当前无重复字母的最大子字符串长度。需要注意的是这里不是先确定无重复字母的最大子字符串再求得其长度,而是确定每一个无重复字母的子字符串,求其长度,然后再与保存的最大长度作比较。相当于用一个数组不断的存储每一个无重复字母的字符串的长度,遍历完之后再从这些长度中取最大值作为结果,这里只不过是简化了操作,利用缓存记录上一无重复字母的子字符串的长度,然后不断与现无重复字母的子字符串的长度作比较,取最大值。所以,遇到区间类问题的最大最小值,可以先求得满足条件的所有情况,同时记录这些情况下的最大最小值结果,然后再对比这些结果取最大最小,或者用res = max(res, 现结果)来简化程序。

  鉴于此题针对的字符串,可以建立一个数组来模拟字典,如果再遍历过程中map[s[i]]不等于-1, 说明此字母在 i 之前出现过:1. map[s[i]]<left, 说明之前的字母已经不在现非重复字符串中,所以仍然计算字符串长度。2. map[s[i]] >= left, 说明此字母在现字符串中已经出现过一次,则需要调整left的位置,具体调整为此字母上一次出现位置的下一位置,即left = map[s[i]] + 1;

 class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> map(, -);
int res = , left = ;
for (int i = ; i < s.size(); ++i) {
if (map[s[i]] == - || map[s[i]] < left) {
res = max(res, i - left + );
} else {
left = map[s[i]] + ;
}
map[s[i]] = i;
} return res;
}
};

  利用set,把出现过的字符都放入set中,遇到set中没有的字符就加入set中并更新结果res,如果遇到重复的,则从左边开始删字符,直到删到重复的字符停止

 class Solution {
public:
int lengthOfLongestSubstring(string str) {
set<char> s;
int res = , left = ; for (int i = ; i < str.size(); ++i) {
if (s.find(str[i]) == s.end()) {
s.insert(str[i]);
res = max(res, i - left + ); // 也可以写作 res = max(res, (int)s.size()),此时的set中即是当前无重复字母的子字符串
} else {
s.erase(str[left++]);
--i;
}
} return res;
}
};

【LeetCode】003. Longest Substring Without Repeating Characters的更多相关文章

  1. 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...

  2. 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

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

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

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

  4. 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)

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

  5. 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters

    一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...

  6. 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  7. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

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

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

  9. 003 Longest Substring Without Repeating Characters 最长不重复子串

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

随机推荐

  1. C#中往数据库插入空值报错解决方法

    C#中的NUll于SQL中的null是不一样的, SQL中的null用C#表示出来就是DBNull.Value 在用C#往数据库里面插入记录的时候, 可能有的字段你不赋值,那么这个字段的值就为null ...

  2. JQuery 获取父元素方法

    ---恢复内容开始--- <tr class="removerow" style=""> <td> <input type=&qu ...

  3. PHP 数字转大写

    <?php header("content-type:text/html;charset=utf-8"); function numToRmb($num){ $rmbNum ...

  4. 每日练习level-7

    1.有序列表.无序列表.自定义列表如何使用?写个简单的例子.三者在语义上有什么区别?使用场景是什么? 能否嵌套? 有序列表是一列使用数字进行标记的项目,它使用<li>包含于<ol&g ...

  5. Shell编程之Linux信号及信号跟踪

    一.Linux信号 1.什么是信号? Linux信号是由一个整数构成的异步消息,它可以由某个进程发给其他进程,也可以在用户按下特定键发生某种异常事件时,由系统发给某个进程. 2.信号列表 [root@ ...

  6. cocos2dx打飞机项目笔记五:CCSpriteBatchNode 的使用

    在上一节里,在头文件看到 定义了一个 CCSpriteBatchNode* batchNode;,在addEnemy方法里看到 batchNode->addChild(enemy); 新建的敌机 ...

  7. poj 1961 Period 【KMP-next前缀数组的应用】

    题目地址:http://poj.org/problem?id=1961 Sample Input 3 aaa 12 aabaabaabaab 0 Sample Output Test case #1 ...

  8. linux 安装tomcat7

    wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.81/bin/apache-tomcat-7.0.81.tar.gz 解压安装包 t ...

  9. how to use Inspector in fiddler

    打开fiddler之后,会自动捕获本机的http请求,以列表的形式显示在左侧 双击左侧列表中的某一个request,右侧会自动切换到Inspectors窗口. 右侧上半部分是request的raw G ...

  10. <转>xshell的快捷键

    xshell中现有的快捷键 快捷方式键 说明 Alt + N 与文件菜单的新建相同 Alt + O 与文件菜单的打开相同 Alt + C 与文件菜单的断开相同 Alt + Enter 切换到全屏模式 ...