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. OpenMP多线程linux下的使用,简单化

    http://hi.baidu.com/diwulechao/item/bc6d865c411b813c32e0a932 http://www.cnblogs.com/yangyangcv/archi ...

  2. php 中cookie和session的用法比较

    1.cookie数据存放在客户的浏览器上,session数据放在服务器上. 2.cookie不是很安全,别人可以分析存放在本地的COOKIE并进行COOKIE欺骗,考虑到安全应当使用session. ...

  3. js 后台弹窗

    后台弹出操作成功,失败信息 /// <summary> /// 弹出信息,并跳转指定页面. /// </summary> public static void AlertAnd ...

  4. Swift中的注释以及表达式

    Swift程序有两类注释:单行注释(//)和多行注释(/*...*/).注释方法与C.C++和Objective-C语言都是类似的,下面详细介绍一下.1. 单行注释单行注释可以注释整行或者一行中的一部 ...

  5. XMPP登录应用

    一.导入框架 1.XMMP框架,写入头文件,设置XMPPStreamDelegate代理,定义 XMPPStream *_xmppStream; 2.libresolv.dylib 3.libxml2 ...

  6. 《RedHatLinux系统修复视频(通过本地镜像)》

    此视频的测试环境: win7下安装的VMware    redhatlinux6.3系统的修复     以删掉kernel系统引导故障重新安装kernel为例 基于本地镜像来对系统进行修复 如疑问可留 ...

  7. Android 源码VecotorDrawable

    1 R.styleable.VectorDrawable_viewportWidth 该资源的名字并非VectorDrawable_viewportWidth 而是 attrs.xml 下的声明 &l ...

  8. DTCMS添加文章,将tags标签的值赋到SEO关键词上,以及将摘要的值赋到SEO描述

    将tags标签的值赋到SEO关键词上 admin\article_edit.aspx中 $(function () {  方法中加上 //tags的值赋到SEO关键词上 $("#txtTag ...

  9. JS滑动门,JQuery滑动门

    <a href="#" id="one1" onmouseover="setTab('one',1,2)" class="h ...

  10. 【Qt】数据库连接池

    请查看公孙二狗的文章 数据库连接池