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. [XML123] XSLT

    XSLT简介 http://www.w3school.com.cn/xsl/index.asp XSLT测试 http://www.veryhuo.com/down/html/54703.html

  2. transfer function

    线性变化后,往往希望进行非线性变化,常用的非线性变化函数有Sigmoid,Tanh,ReLU.会发现,经过这三个函数变化后,Tensor的维度并不发生变化. tanh(双曲正切函数):

  3. 404 Note Found 队-Beta7

    目录 组员情况 组员1:胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:何宇恒 组员11:刘一好 展示组内最新 ...

  4. apue2e unp安装

    最近在读 Richard Stevens 的大作<UNIX环境高级编程>,相信很多初读此书的人都会与我一样遇到这个问题,编译书中的程序实例时会出现问题,提示 “错误:apue.h:没有那个 ...

  5. angularJs实现星星等级评分

    星期六加班,教育后台也要有星级评分等级的需求,醉了……基本知道些怎么做,网上也随便找了找,没什么合意的,毕竟需求不同,也不能完全一样不是.学习之,改之╮(╯▽╰)╭ Directive angular ...

  6. FPGA入门实例一:LFSR

    一:任务: 要求使用Verilog语言在Xilinx Virtex-6开发板上实现线性反馈移位寄存器(LFSR)的硬件逻辑设计. 二:前期准备: 基本上完成一个简单的设计需要用到以下几个软件 逻辑:U ...

  7. 大数据入门第七天——MapReduce详解(一)入门与简单示例

    一.概述 1.map-reduce是什么 Hadoop MapReduce is a software framework for easily writing applications which ...

  8. Memcached 集群架构问题归纳

    集群架构方面的问题o memcached是怎么工作的?o memcached最大的优势是什么?o memcached和MySQL的query cache相比,有什么优缺点?o memcached和服务 ...

  9. mysql 导出表,导出数据 命令

    mysql mysqldump 只导出表结构 不导出数据 复制代码代码如下: mysqldump --opt -d 数据库名 -u root -p > xxx.sql  备份数据库  复制代码代 ...

  10. BZOJ2539 Spoj 10707 Count on a tree II

    题面 题解 因为这道题目我也不太会做,所以借鉴了一下大佬heyujun的博客 如果不强制在线,这道题目是树上莫队练手题 我们知道莫队是离线的,但是万一强制在线就凉凉了 于是我们就需要一些操作:树分块 ...