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.

思路:

关键是记录当前子串的起始位置和用hash表记录每个字母出现的情况。

大神简约版的代码:

class Solution {
public:
int v[]; //asciib码最多256个
int lengthOfLongestSubstring(string s) {
memset(v,-,sizeof(v)); //位置先全部赋值为-1
int start = , ans = ;
for (int i = ; i < s.size(); ++i) {
if (v[s[i]] >= start) { //如果当前子串中已经出现了该字符 更新答案和起始位置
ans = ans > i - start ? ans : i - start;
start = v[s[i]] + ;
}
v[s[i]] = i;
}
ans = ans > s.size() - start ? ans : s.size() - start;
return ans;
}
};

思路是一样的,我自己好长好慢的代码:

int lengthOfLongestSubstring(string s) {
unordered_map<char, int> v;
int ans = ;
int len = ;
int start = ; //记录当前子串的起始位置
for(int i = ; i < s.size(); i++)
{
if(v.find(s[i]) == v.end())
{
len++;
v[s[i]] = i;
}
else
{
string tmp = s.substr(i - len, len);
ans = (len > ans) ? len : ans;
len = i - v[s[i]];
for(int j = start; j < v[s[i]]; j++) //把重复字符前面的字符次数都置0
{
v.erase(s[j]);
}
start = v[s[i]] + ;
v[s[i]] = i; }
}
ans = (len > ans) ? len : ans;
return ans;
}

【leetcode】Longest Substring Without Repeating Characters (middle)的更多相关文章

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

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

  2. 【leetcode】Longest Substring Without Repeating Characters

    题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

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

    这道题是LeetCode里的第3道题. 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: ...

  4. [Leetcode] 3.Longest Substring Without Repeating Characters(unordered_map)

    通过把未访问的结点放到unordered_map中来判断是否重复,代码如下: class Solution { public: int lengthOfLongestSubstring(string ...

  5. 【Leetcode】【Medium】Longest Substring Without Repeating Characters

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

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

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

  7. 【LeetCode3】Longest Substring Without Repeating Characters★★

    题目描述: 解题思路: 借用网上大神的思想:the basic idea is, keep a hashmap which stores the characters in string as key ...

  8. LeetCode #003# Longest Substring Without Repeating Characters(js描述)

    索引 思路1:分治策略 思路2:Brute Force - O(n^3) 思路3:动态规划? O(n^2)版,错解之一:420 ms O(n^2)版,错解之二:388 ms O(n)版,思路转变: 1 ...

  9. 「LeetCode」0002-Longest Substring Without Repeating Characters(C++)

    分析 贪心思想.注意更新每次判断的最长不同子串的左区间的时候,它是必须单调增的(有时候会在这里翻车). 代码 关掉流同步能有效提高速度. static const auto io_sync_off = ...

随机推荐

  1. asp.net C#数据导出Excel实例介绍

    excel导出在C#代码中应用己经很广泛了,我这里就做些总结,供自己和读者学习用. Excel知识点. 一.添加引用和命名空间 添加Microsoft.Office.Interop.Excel引用,它 ...

  2. js实现跨域(jsonp, iframe+window.name, iframe+window.domain, iframe+window.postMessage)

    一.浏览器同源策略 首先我们需要了解一下浏览器的同源策略,关于同源策略可以仔细看看知乎上的一个解释.传送门 总之:同协议,domain(或ip),同端口视为同一个域,一个域内的脚本仅仅具有本域内的权限 ...

  3. Poj 2583 Series Determination

    1.Link: http://poj.org/problem?id=2583 2.Content: Series Determination Time Limit: 1000MS   Memory L ...

  4. MySQL学习笔记之数据存储类型

    说明:本文是作者对MySQL数据库数据存储类型的小小总结. Numeric Type (数字类型) 1.TINYINT.SMALLINT.MEDIUMINT.INT.BIGINT主要根据存储字节长度不 ...

  5. select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET (转)

    select函数用于在非阻塞中,当一个套接字或一组套接字有信号时通知你,系统提供select函数来实现多路复用输入/输出模型,原型: #include <sys/time.h>       ...

  6. Js popup position which right under target item

    <div style="margin-left:600px;"> <div id="Span1" style="color:#eee ...

  7. 关于谷歌Chrome浏览器的两个Bug?

    1.网络资源下载莫名其妙的网络错误(也不支持续传?有时打开文件是损坏的?) 2.超级链接莫名的成了html下载?

  8. SQL学习中(一)序列

    序列可以理解数值序列生成器,通俗的说是按照已经设定的规则自动产生数据的方案对象.--SQL SERVER不支持 个人认为序列类似于SQLSERVER中的identity(1,1),可以用于在表中添加数 ...

  9. SQL 跨服务器数据库增、删、改、查(二)

    --创建链接服务器 exec sp_addlinkedserver 'jx3xxiednr3ucidf', ' ', 'SQLOLEDB', 'jx3xxiednr3ucidf' exec sp_ad ...

  10. 【BZOJ 2143】 飞飞侠

    Description 飞飞国是一个传说中的国度,国家的居民叫做飞飞侠.飞飞国是一个N×M的矩形方阵,每个格子代表一个街区.然而飞飞国是没有交通工具的.飞飞侠完全靠地面的弹射装置来移动.每个街区都装有 ...