3. Find the longest substring without repeating characters

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.

思路:

遍历字符串,每个字符串都在map中查找是否有相同的字符,如果存在相同元素s[j],则计算(i-j)来计算长度;新的index_0即为j+1;然后在map中删除该元素,以确认下一次遇到相同元素是s[i]而不是s[j]。

map是一个(字符-index)的键值对 map[rune]int.


func lengthOfLongestSubstring(s string) int {
/*
* ret:最终结果
* index_0: 当前字符串的起始位置
*/
var ret, index_0 int
m := make(map[rune]int) for i, v := range s {
j, ok := m[v]
if ok && (j >= index_0) {
// 存在重复字符
if (i - index_0) > ret {
ret = i - index_0
}
index_0 = j + 1
delete(m, v)
} m[v] = i
} if len(s)-index_0 > ret {
ret = len(s) - index_0
} return ret
}

Leetcode_3. Find the longest substring without repeating characters的更多相关文章

  1. [LeetCode_3] Longest Substring Without Repeating Characters

    LeetCode: 3. Longest Substring Without Repeating Characters class Solution { public: int lengthOfLon ...

  2. LeetCode[3] Longest Substring Without Repeating Characters

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

  3. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

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

  4. Longest Substring Without Repeating Characters

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

  5. 3. Longest Substring Without Repeating Characters(c++) 15ms

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

  6. 【leetcode】Longest Substring Without Repeating Characters

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

  7. Longest Substring Without Repeating Characters(C语言实现)

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

  8. leetcode: longest substring without repeating characters

    July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...

  9. Longest Substring Without Repeating Characters (c#)

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

随机推荐

  1. 如何查看MySQL执行的每条SQL

    1.登录数据库 [root@mysqltest1 ~]# mysql -uroot -p -h172.16.*.*(你数据库的IP) 2.查看是否开启general_log mysql> sho ...

  2. Java基础加强之并发(四)synchronized关键字

    并发系列参考文章http://www.cnblogs.com/skywang12345/p/3323085.html#3907193 synchronized原理 在java中,每一个对象有且仅有一个 ...

  3. css自问自答(一)

    css自问自答(一) 1.块级元素和行内元素特性与区别? 块级:display:block <div>.<p>.<h1>...<h6>.<ol&g ...

  4. html手机网页自适应宽度

    #在head之间加如下代码即可 <meta name="viewport" content="width=device-width, initial-scale=1 ...

  5. canvas 绘制双线技巧

    楔子 最近一个项目,需要绘制双线的效果,双线效果表示的是轨道(类似铁轨之类的),如下图所示: 负责这块功能开发的小伙,姑且称之为L吧,最开始是通过数学计算的方式来实现这种双线,也就是在原来的路径的基础 ...

  6. Yii2 的安装及简单使用

    前段时间第一次使用Yii2框架,碰到了一些问题,这里记录一下. Yii2安装:通过composer安装 1.首先要安装composer,我在另外一篇博客中介绍了如何在Windows下安装compose ...

  7. 时间序列分析工具箱——timetk

    目录 时间序列分析工具箱--timetk timetk 的主要用途 加载包 数据 timetk 教程: PART 1:时间序列机器学习 PART 2:转换 翻译自<Demo Week: Time ...

  8. Docker-CE-CentOS安装&更新&卸载

    准备工作 系统准备 要求centos-7以上版本(包含7) 开启centos-extras 在线安装方式要求 卸载旧版本 卸载旧版本docker yum remove docker \ docker- ...

  9. ExcelVBA实现一键生成word文字报告及批量操作[原创]

    在很多工作中,经常需要写一些类似的报告,使用同一个模板,只是里面的数据不同,人工操作工程量大且容易出错,如果能用程序直接实现可以省去不少麻烦. 本文使用ExcelVBA实现,主要思路是使用word邮件 ...

  10. 课下测试补交(ch01、ch02、ch07)

    课下测试补交(ch01.ch02.ch07) 课下测试ch01 1.Amdahl定律说明,我们对系统的某个部分做出重大改进,可以显著获得一个系统的加速比.(B) A . 正确 B . 错误 解析:课本 ...