Leetcode_3. Find the longest substring without repeating characters
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的更多相关文章
- [LeetCode_3] Longest Substring Without Repeating Characters
LeetCode: 3. Longest Substring Without Repeating Characters class Solution { public: int lengthOfLon ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 3. Longest Substring Without Repeating Characters(c++) 15ms
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
- Longest Substring Without Repeating Characters(C语言实现)
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- leetcode: longest substring without repeating characters
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- Longest Substring Without Repeating Characters (c#)
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- macbook下 go 语言的 helloworld
go语言开发的目录 一般go语言$GOPATH 目录约定有三个子目录: src 存放源代码(比如:.go .c .h .s等) pkg 编译后生成的文件(比如:.a) bin 编译后生成的可执行文件( ...
- iPhone X进入DFU模式
下面把iphoneX进dfu模式的方法分享给大家.在开机状态下,按一下音量加键后松开,然后按一下音量减键后松开,接着按住电源键,屏幕完全熄灭后,松开电源键,接着同时按住电源键和音量减,保持5秒左右,再 ...
- 【Java123】JDBC数据库连接池建立
需求场景:多SQL任务多线程并行执行 解决方案:建立JDBC数据库连接池,将线程与连接一对一绑定 https://www.cnblogs.com/panxuejun/p/5920845.html ht ...
- Redis——总结
启动 redis 客户端,打开终端并输入命令 redis-cli.该命令会连接本地的 redis 服务. $redis-cli redis 127.0.0.1:6379> redis 127.0 ...
- active developer path ("/Applications/Xcode.app/Contents/Developer")
-> git xcrun: error: active developer path ("/Applications/Xcode.app/Contents/Developer" ...
- selenium和PhantomJS的安装
针对w10系统 selenium安装 pip install selenium 默认安装的是3.x版本,但是3.x版本不支持PhantomJS,所以要安装2.x版本 pip install selen ...
- mybatis框架下使用generator插件自动生成domain/mapping/mapper
手动去创建domain/mapping/mapper费时费力还容易出错,用插件自动生成非常的方便. 这里以MySQL数据库为例,也可以改成Oracle,改成相应的驱动和URL即可. 下载generat ...
- Xamarin 编写混合APP趟坑记录(二)
前言 公司要开发一个App,为了便于维护和更新,而不用每次去苹果审核,采用的是混合开发方式:用WebVie+WebApp的方式. 因为本人不会Java和ObjectC,公司又不想花钱招这两个岗位的人, ...
- H5 开发中常见的小问题
1.解决 浏览器 返回按钮不刷新的问题 window.onpageshow = function(event) { if (event.persisted) { window.location.rel ...
- Mac开发中遇到的一些小问题解析
通过mac开发的过程中,有一些小问题出现,列如下,后续会持续增加: 1. 命令行清空废纸篓(jar包太多,倾倒废纸篓太慢) sudo rm -rfv ~/.Trash /Volumes/*/.Tras ...