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

003. Longest Substring Without Repeating Characters[M]

题目

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.

思路

题目意思是在一个字符串中找一个最长的子串(没有重复的字母)
简单的思路:
从左往右扩展子串,维持2个变量i和j来维持一个新的子串,j不断移动,每加入一个新的字符,判断是否有重复的,如果有重复的,移动i,生成新子串……
len = max(len,j-i+1);





这里有个问题就是,如何判断子串中是否有重复的字符,传统思路就是循环,这样每次查找重复的时间复杂度为O(n),导致整体时间复杂度为O(N^2),其实我们可以使用hashmap来存,这样可以保证每次查找的效率为O(1)。

代码

class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_map<char,int> mymap;
unordered_map<char,int>::iterator it;
int len = 0,i = -1;
for(int j=0;j < s.length();j++)
{
/***是否有重复******/
it = mymap.find(s.at(j));
if(it != mymap.end())
/*****有重复的时候,移动i*****/
i = std::max(it->second,i);
/****把新的字符加入*******/
mymap[s.at(j)] = j;
len = std::max(len,(j-i));
}
return len;
}
};

但是实际上,对于这个题目,不需要用hashmap,因为所有的字符ASCII码加起来也就最多255个,可以直接用数组来代替hashmap,效率更高。

class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> mymap(255,-1);
int len = 0,i = -1,tmp;
for(int j=0;j < s.length();j++)
{
tmp = mymap[s.at(j)];
i = std::max(tmp,i);
mymap[s.at(j)] = j;
len = std::max(len,(j-i));
}
return len;
}
};

上面用了一个trick就是每个数组的初始化为-1表示没有出现重复,它不可能比i的初始值大,如果有重复的,直接覆盖,这样可以不用额外的语句判断是否出现重复。

《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复的更多相关文章

  1. leetcode题解 3. Longest Substring Without Repeating Characters

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

  2. LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)

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

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

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

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

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

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

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

  6. 【LeetCode OJ】Longest Substring Without Repeating Characters

    题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...

  7. 【LeetCode】003. Longest Substring Without Repeating Characters

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

  8. LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)

    题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...

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

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

随机推荐

  1. FMX.Platform.TApplicationEvent

    FMX.Platform.TApplicationEvent http://docwiki.embarcadero.com/Libraries/Seattle/en/FMX.Platform.TApp ...

  2. 数据集和JSON相互转换

    使用DELPHI原生类实现数据集和JSON相互转换  JSON二要素:数组和对象.对象可以包含数组,数组可以包含对象.无层数限制.OLEVARIANT也类似,OLEVARIANT的一个元素又可以是OL ...

  3. AngularJS Backbone.js Ember.js 对比

    看到一篇关于AngularJS Backbone Ember.js的对比,建议看一看 说说个人的观点(本人学艺不精,只是个人的观点,不保证观点完全正确,请轻拍): backbone.js 短小精悍,非 ...

  4. linux 常用命令,开发记住这些基本能够玩转linux

    系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...

  5. javascript做的一个根据table中某个td的值为日期时的倒计时

    JavaScript代码: <script> window.onload = window.onload = function () { getTdValue(); } //根据传过来的天 ...

  6. 【转】.NET内存管理、垃圾回收

    1. Stack和Heap    每个线程对应一个stack,线程创建的时候CLR为其创建这个stack,stack主要作用是记录函数的执行情况.值类型变量(函数的参数.局部变量 等非成员变量)都分配 ...

  7. CF1109DSasha and Interesting Fact from Graph Theory(数数)

    题面 传送门 前置芝士 Prufer codes与Generalized Cayley's Formula 题解 不行了脑子已经咕咕了连这么简单的数数题都不会了-- 首先这两个特殊点到底是啥并没有影响 ...

  8. 记一次MBR锁机病毒分析

    有一天,在机缘巧合之下我获得了一个锁机软件(是多巧合阿喂!),然后兴高采烈的把它拖入了虚拟机里蹂躏(>_<!). 很巧,软件有虚拟机检测... Emmmm好吧,随便过一下... 我用的虚拟 ...

  9. Cannot sending data from mongodb into elasticsearch using logstash

    Question: Hi all, i have an issue when i try to get data from mongodb to elasticsearch using logstas ...

  10. mongodb 3.0下载安装、配置及mongodb最新特性、基本命令教程详细介绍

    mongoDB简介(本文由www.169it.com搜集整理) MongoDB是一个高性能,开源,无模式的文档型数据库,是目前在IT行业非常流行的一种非关系型数据库(NoSql).它在许多场景下可用于 ...