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. JavaWeb-SQL-Servlet-JSP学做购物系统——日志一

    一.JDBCUtil 本人自半路出家学码代码以来,bug不断,一直不顺利.路漫漫.话不多说,上bug:T_T; 代码是这样的: 自定义的方法,插入一个sql语句:execUpdate(String s ...

  2. 逆向分析一款国外Blackjack Card Counter软件并附上License生成脚本

    没有学过逆向,一时兴起,搞了一下这个小软件,名为“逆向分析”,其实过程非常简单,难登大雅之堂,就当段子看吧.首先介绍一下背景吧.这是一款国外的Blackjack也就是21点算牌软件,我从来不玩牌的,机 ...

  3. 团队作业第六次—团队Github实战训练

    作业描述 课程 软件工程1916|W(福州大学) 团队名称 修!咻咻! 作业要求 团队作业第六次-团队Github实战训练 团队目标 搭建一个相对公平公正的抽奖系统,根据QQ聊天记录,完成从统计参与抽 ...

  4. Spring Cloud微服务笔记(二)Spring Cloud 简介

    Spring Cloud 简介 Spring Cloud的设计理念是Integrate Everything,即充分利用现有的开源组件, 在它们之上设计一套统一的规范/接口使它们能够接入Spring ...

  5. ubuntu的安装及ubuntu中安装mysql和tomcat

    一.安装ubuntu 1.创建虚拟机 2.向导选择自定义 3.然后下一步再下一步,直到这里,稍后再安装系统 4.然后选择linux,注意这里下面的下拉选择Ubuntu64,因为我们下载的是64位的,如 ...

  6. ESP-01S刷ESPEasy固件,接入HA

    首先下载ESPEasy最新版 https://github.com/letscontrolit/ESPEasy/releases 准备接线从ESP01S到USB-TTL TTL——ESP01S 3.3 ...

  7. [POJ1193][NOI1999]内存分配(链表+模拟)

    题意 时 刻 T 内存占用情况 进程事件 0 1 2 3 4 5 6 7 8 9 进程A申请空间(M=3, P=10)<成功> 1 A 2 A B 进程B申请空间(M=4, P=3)< ...

  8. yii2 httpClient的用法

    yii2 httpClient的用法示例: <?php /* * @Purpose : yii2 httpClient 请求示例 * @Author : Chrdai * @Time : 201 ...

  9. ionic-基于angularjs实现的多级城市选择组件

    大家都知道在移动端的选择地区组件,大部分都是模拟IOS选择器做的城市三级联动,但是在IOS上比较好,在Android上因为有的不支持ion-scroll.所以就会出现滚动不会自动回滚到某一个的正中间. ...

  10. DHCP工作原理简析

    引言 DHCP是网络体系结构中应用层的一个重要协议,它可以帮助我们对要连接到互联网的计算机进行IP地址等信息的配置.本文从DHCP的原理出发,就DHCP的工作过程 进行详细的探讨. 主要报文 发现报文 ...