Leetcode 3. Longest Substring Without Repeating Characters

提交网址: https://leetcode.com/problems/longest-substring-without-repeating-characters/

Total Accepted: 149135 Total Submissions: 674678 Difficulty: Medium

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.

分析:

使用哈希表,保存每个字符上一次出现的位置,时间复杂度为O(n).

AC代码:

class Solution {
public:
int lengthOfLongestSubstring(string s) {
int loc[256]; // 哈希表保存字符上一次出现的位置,ASCII码是8位,一般用前7位,是128(2^7)个可用字符。IBM机器的扩展使用了最高位,故用2^8个(256)
memset(loc, -1, sizeof(loc)); int curStartIdx = -1, max_len = 0; //curStartIdx为当前子串的开始位置,初始化为-1
for(int i = 0; i < s.size(); i++)
{
if(loc[s[i]] > curStartIdx) //如果当前字符出现过,那么当前子串的起始位置为这个字符上一次出现的位置+1
{
curStartIdx = loc[s[i]];
}
if(i - curStartIdx > max_len) // 使用贪心算法进行子串延伸,关键!!!
{
max_len = i - curStartIdx;
}
loc[s[i]] = i; //如果当前字符没出现过,将其位置记录在loc数组中
}
return max_len;
}
};

You are here! 
Your runtime beats 61.72% of cppsubmissions.

C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告的更多相关文章

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

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

  2. 【LeetCode】Longest Substring Without Repeating Characters 解题报告

    [题意] Given a string, find the length of the longest substring without repeating characters. For exam ...

  3. [LeetCode] 3. Longest Substring Without Repeating Characters 解题思路

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

  4. Leetcode:Longest Substring Without Repeating Characters 解题报告

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

  5. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  6. leetcode:Longest Substring Without Repeating Characters

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

  7. [LeetCode][Python]Longest Substring Without Repeating Characters

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  8. LeetCode之Longest Substring Without Repeating Characters

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

  9. Leetcode 3. Longest Substring Without Repeating Characters (Medium)

    Description Given a string, find the length of the longest substring without repeating characters. E ...

随机推荐

  1. DAY1 VS2017&CUDA10.01环境搭建

    Visual Studio工程配置情况: VC++目录配置: C:\ProgramData\NVIDIA Corporation\CUDA Samples\v10.\common\lib\x64 C: ...

  2. 基于Cmake+QT+VS的C++项目构建开发编译简明教程

    目录 一.工具下载与安装 1.     Qt 2.     Visual Studio 2015 3.     Cmake 二.C++及Qt项目构建 1.     基于VS构建Qt项目 2.     ...

  3. SQL 收缩日志

    USE [master]ALTER DATABASE RcBalance SET RECOVERY SIMPLE WITH NO_WAITALTER DATABASE RcBalance SET RE ...

  4. 3dmax 3dmax计算机要求 3dmax下载

    渲染首先是要X64兼容台式电脑,笔记本不行,笔记本就是学生拿来玩还行,渲染大图笔记本真的是发热. 配置一般的电脑和笔记本千万不要尝试安装3dmax2019了,很卡的,3dmax2019只有64位,没有 ...

  5. photoshop改变图片大小,不改变像素

    用画图修改了图片像素,360*440 但是图片30K,要求图片20K 打开photoshop,打开图片,点击文件--存储为web所用格式,调节品质大小到20K以下,保存即可

  6. 学习使用Mendeley1

    原文来自:https://www.mendeley.com/guides/desktop/01-desktop-interface 1.添加文件菜单 - 使用此功能将新条目添加到您的Mendeley库 ...

  7. Linux 下redis 集群搭建练习

    Redis集群 学习参考:https://blog.csdn.net/jeffleo/article/details/54848428https://my.oschina.net/iyinghui/b ...

  8. 转 the best for wcf client

    原文:http://stackoverflow.com/questions/573872/what-is-the-best-workaround-for-the-wcf-client-using-bl ...

  9. 马昕璐 201771010118《面向对象程序设计(java)》第十五周学习总结

    第一部分:理论知识学习部分 JAR文件:将.class文件压缩打包为.jar文件后,使用ZIP压缩格式,GUI界面程序就可以直接双击图标运行. 既可以包含类文件,也可以包含诸如图像和声音这些其它类型的 ...

  10. oracle 创建的表为什么在table里没有,但是可以查出来

    有两种的可能: 1这个表在其他用户下创建的,当前用户没有权限访问,此表不在属于当前用户 2查询时写的表名,并不是真正意义的表名,可能指向其他用户,或者就不是这个表