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. vue2.* 事件结合双向数据绑定、模块化以及封装Storage实现todolist 待办事项 已经完成 和进行中持久化 06

    ceshi.vue <template> <div id="app"> <input type='text' v-model='todo' @keyd ...

  2. 5、JVM--调优案例分析

    5.1.案例分析 5.1.1.高性能硬件上的程序部署策略 假如一个15w/天左右的在线文档类型网站再准备更换硬件系统 新的硬件为4个CPU.16GB物理内存,操作系统为64为Cento是 Resin作 ...

  3. 集合之ArrayList

    一.ArrayList概述 ArrayList是实现List接口的动态数组,所谓动态就是它的大小是可变的.实现了所有可选列表操作,并允许包括 null 在内的所有元素.除了实现 List 接口外,此类 ...

  4. block本质探寻七之内存管理

    说明: <1>阅读本问,请参照block前述文章加以理解: <2>环境:ARC: <3>变量类型:基本数据类型或者对象类型的auto局部变量: 一.三种情形 //代 ...

  5. OpenGL ES画板

    一.概述 利用自定义顶点和片元着色器渲染,并且设置图片纹理颜色为画笔颜色 二.核心代码 - (void)renderLineFromPoint:(CGPoint)start toPoint:(CGPo ...

  6. Java开发工程师基础Math,Random,Scanner类的使用

    Math类的使用(重点) (1)数学操作类:该类没有构造函数,方法均为静态的 (2)掌握内容 A:成员变量 **E:比任何其他值都更接近e(即自然对数的底数)的double值. **PI:比任何其他值 ...

  7. kNN分类算法实例1:用kNN改进约会网站的配对效果

    目录 实战内容 用sklearn自带库实现kNN算法分类 将内含非数值型的txt文件转化为csv文件 用sns.lmplot绘图反映几个特征之间的关系 参考资料 @ 实战内容 海伦女士一直使用在线约会 ...

  8. 【python3】将视频转换为代码视频

    一.环境准备 1.需要安装opencv,直接安装 pip install opencv-python 2.需要安装ffmpeg ,直接解压免安装,下载传送门: 将 ffmpeg.exe 的路径复制,替 ...

  9. CentOS 7 安装 caffe

    1.安装CUDA,很简单,傻瓜式安装 2.http://caffe.berkeleyvision.org/install_yum.html 按照里面安装 3.遇到的问题: LD -o .build_r ...

  10. java spring 等启动项目时的异常 或 程序异常的解决思路

    今天搭建ssm项目的时候,因为pagehelper的一个jar包没有导入idea的web项目下的lib目录中,异常报错找不到pagehelper,这个问题在出异常的时候疯狂crash,让人心情十分不舒 ...