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. RNN-theano代码解析

    import theano import numpy import os import pdb from theano import tensor as T from collections impo ...

  2. 补充:sql server 中的相关查询、case函数

    相关查询(在同一个表中) 相关查询类似子查询,但是又不同于子查询:子查询中的子条件可以单独查出数据,但是相关查询的子条件不能查处数据.(可以理解成C#中for的穷举法,第一个for走一个,第二个for ...

  3. CentOS6.3系统安装SCP命令

    原文:http://www.111cn.net/sys/CentOS/58387.htm CP使用SSH协议在Linux系统中进行文件传输,但我最小安装的CentOS 6.3没有该命令.  代码如下 ...

  4. Cocos2d-x中创建SQLite数据库

    我们下边介绍如何通过SQLite3提供的API实现MyNotes数据库创建.创建数据库一般需要经过如下三个步骤.(1) 使用sqlite3_open函数打开数据库.(2) 使用sqlite3_exec ...

  5. 双网卡route配置

    目前仅适用于windows: 192.168.*.*网段适用于上外网的 10网段适用于内网 route add 10.0.0.0 mask 255.0.0.0 10.34.6.1route add 1 ...

  6. java synchronized关键字

    引用其他人的一段话 Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 一.当两个并发线程访问同一个对象object中的这个synchro ...

  7. CICS日志---内存问题

    Level 9 COCITOOL_XA: Connected! [2014-01-09 19:46:24.296834][22347888] Level 0 TestPerormence: GDAO ...

  8. 带搜索的下拉框Chosen

    一:参考 https://harvesthq.github.io/chosen/ Chosen是一个jQuery插件 二:引入js文件 <link href="plug-in/chos ...

  9. java集合的互转

    List<-->数组.List<-->Set.数组<-->Set.Map将键转化为Set.Map将值转化为Set.Map将值转化为List等集合常 public c ...

  10. Meta标签中的format-detection属性及含义

    format-detection翻译成中文的意思是“格式检测”,顾名思义,它是用来检测html里的一些格式的,那关于meta的format-detection属性主要是有以下几个设置: meta na ...