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. Desert King POJ - 2728(最优比率生产树/(二分+生成树))

    David the Great has just become the king of a desert country. To win the respect of his people, he d ...

  2. python3.X中try/except

    包含try...except...在3.x版本中与2.x版本中的用法差异. 1.先说差异: 在2.x的python中用法实例: try: ...... except Exception,e: rais ...

  3. Capslock+ 键盘党都爱的高效利器 - 让 Windows 快捷键操作更加灵活强大

    Capslock+ 键盘党都爱的高效利器 - 让 Windows 快捷键操作更加灵活强大  优化辅助    Windows   2016-06-05 91,167   微博微信QQ空间印象有道邮件   ...

  4. 2019年3月2日-小雨.md

    2019年3月2日, 星期六 开学已经一周了,时间好像限制了自己进步的脚步,一个人的精力有限,想做好方方面面实在是太难了,有很多事儿最后都没做的完美.相反,自己应该放下繁琐的包袱,简简单单的干一件事儿 ...

  5. Pandas 0 数据结构Series

    # -*- encoding:utf-8 -*- # Copyright (c) 2015 Shiye Inc. # All rights reserved. # # Author: ldq < ...

  6. win10自带的防火墙Windows Defender

    Windows Defender防火墙(别名:windows守卫者)是微软公司自主研发的一款基于windows自身保护的一款系统. Windows Defender可以对系统进行实时监控,对于Wind ...

  7. BZOJ1386 : [Baltic2000]Stickers

    显然每一位的限制独立,对于每一位求出仅限制该位下的最大数,然后求最小值即可. 假设当前要求数字$d$的答案: 考虑填数字的过程,可以看作依次考虑一个序列中的每个数,当前缀和$<0$时退出. 设$ ...

  8. c# asp.net mvc4 使用uploadify插件实现上传功能

    [1]首先去官网下载插件:http://www.uploadify.com/download/ .ww我使用的是免费的,基于flash的版本.因为基于H5的版本需付费使用,然后使用该插件也就是做做毕设 ...

  9. c#坐标系互相转换

    转自群友的博客:https://www.xiaofengyu.com/?p=108 群友的github地址:https://github.com/jfwangncs/GPSConvert  各种坐标系 ...

  10. 安装使用eclipse

    安装使用eclipse 目标 java学习需要,老师说要安装eclipse,,其实已经下了IDEA了,不过还是听老师的比较好( 准备 jre 也就是java运行环境,因为之前就下了jdk(里面包含jr ...