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, ...
随机推荐
- 【转】decorView和window之间的层级及关系
转载请注明出处:http://blog.csdn.net/guxiao1201/article/details/41744107 首先贴出实现Activity对话框圆角的核心代码 @Override ...
- HBase学习之路 (十一)HBase的协过滤器
协处理器—Coprocessor 1. 起源 Hbase 作为列族数据库最经常被人诟病的特性包括:无法轻易建立“二级索引”,难以执 行求和.计数.排序等操作.比如,在旧版本的(<0.92)Hba ...
- 模拟器配置Burpsuite抓取https包
在模拟器中设置代理,长按WiredSSID会弹出菜单: 点击修改网络: 显示高级选项打勾,然后设置代理ip,也就是你运行burp的机器ip: 然后导出burp的证书: 设置保存的路径和文件名: 模拟器 ...
- C# 生成自签名CA证书
"; string signatureAlgorithm = "SHA1WithRSA"; // Generate RSA key pair var rsaGenerat ...
- opencv——Rect和RotatedRect类详解
https://blog.csdn.net/u012819339/article/details/82217667 //不好 https://blog.csdn.net/mailzst1/artic ...
- 学习笔记·堆优化$\mathscr{dijkstra}$
嘤嘤嘤今天被迫学了这个算法--其实对于学习图论来说我内心是拒绝的\(\mathscr{qnq}\) 由于发现关于这个\(\mathscr{SPFA}\)的时间复杂度\(O(kE)\)中的\(k \ap ...
- C语言程序设计I—第十一周教学
第十一周教学总结(12/11-17/11) 教学内容 第4章 循环结构-break continue嵌套循环 4.3 判断素数,4.4求1! + 2! + -. + 100! 课前准备 在蓝墨云班课发 ...
- OVF3为订单原因分配成本中心时报错“成本中心未定义”,消息号:VT806
问题:OVF3为订单原因分配成本中心时报错“成本中心未定义”,消息号:VT806.KS03检查成本中心数据是已经建立的. 原因:OVF3往右边拉动,还有一个需要填入的字段“有效起始日”,此字段值必须在 ...
- Echarts根据数据长度变换柱状图柱状的颜色
//查询图表数据 function GetData() { var qs = $("#qs").val(); ...
- Disruptor时序调用
接下来,我会对disruptor的设计和源码上进行讲解,先来一篇时序图,简单理解下disruptor的工作过程