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, ...
随机推荐
- tbb静态库编译
源自Intel论坛 Jeff的方法https://software.intel.com/en-us/forums/intel-threading-building-blocks/topic/29779 ...
- (第二章)改善JavaScript,编写高质量代码。
建议34:字符串是非值操作 var a = "javascript"; var b = a; b = b.toUpperCase(); alert(a); //javascript ...
- css中位置计算
一.position属性 1. position的属性: ①absolute :绝对定位:脱离文档流的布局,遗留下来的空间由后面的元素填充.定位的起始位置为最近的已定位(设定了position)父元素 ...
- Kafka设计解析(十八)Kafka与Flink集成
转载自 huxihx,原文链接 Kafka与Flink集成 Apache Flink是新一代的分布式流式数据处理框架,它统一的处理引擎既可以处理批数据(batch data)也可以处理流式数据(str ...
- HDU 3591 (完全背包+二进制优化的多重背包)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3591 The trouble of Xiaoqian Time Limit: 2000/1000 M ...
- block本质探寻六之修改变量
说明: <1>阅读本文章,请参照前面的block文章加以理解: <2>本文的变量指的是auto类型的局部变量(包括实例对象): <3>ARC和MRC两种模式均适用: ...
- [iOS]异常捕捉
UncaughtExceptionHandler.h #import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interfac ...
- 【OC底层】KVC原理
定义 KVC的全称是Key-Value Coding,俗称“键值编码”,可以通过一个key来访问某个属性 常见的API有: - (void)setValue:(id)value forKeyPath: ...
- Linux学习笔记(第六章)
第六章-档案权限与目录配置#chgrp:改变档案的所属群组#chown:改变档案的拥有者#chmod:改变档案的权限及属性 chown用法 chmod用法: r:4 w:2 x:1对于文档: 对于目录 ...
- asp.net页面中实现如果图片不存在则显示默认图片
onerror="this.src='/SysAdmin/images/noTouXiang.jpg';"