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. HBase学习之路 (五)MapReduce操作Hbase

    MapReduce从HDFS读取数据存储到HBase中 现有HDFS中有一个student.txt文件,格式如下 95002,刘晨,女,19,IS 95017,王风娟,女,18,IS 95018,王一 ...

  2. jenkins 基本插件

  3. c++——register关键字、struct类型、bool关键字、三目运算符

    register关键字增强 //register关键字 请求编译器让变量a直接放在寄存器里面,速度快 //在c语言中 register修饰的变量 不能取地址,但是在c++里面做了内容 /* //1 r ...

  4. HihoCoder - 1336 二维数状数组(单点更新 区间查询)

    You are given an N × N matrix. At the beginning every element is 0. Write a program supporting 2 ope ...

  5. vue watch关于对象内的属性监听

    vue可以通过watch监听data内数据的变化.通常写法是: data: { a: 100 }, watch: { a(newval, oldVal) { // 做点什么... console.lo ...

  6. spark练习——影评案例

    第一次写博客,新人上路,欢迎大家多多指教!!! ---------------------------------------------------------------------分割线---- ...

  7. Echarts插件

    <%@ page contentType="text/html;charset=UTF-8" language="java" %><% Str ...

  8. 第十周课下作业-IPC

    第十周课下作业-IPC 题目:研究Linux下IPC机制:原理,优缺点,每种机制至少给一个示例,提交研究博客的链接 共享内存 管道 FIFO 信号 消息队列 共享内存 共享内存允许两个或多个进程进程共 ...

  9. 对Oracle的游标进行更为精细的控制

    摘要自 Oracle性能控制艺术 DECLARE l_ename emp.ename%TYPE :='SCOTT'; l_empno emp.empno%TYPE; l_cursor INTEGER; ...

  10. 苏州Uber优步司机奖励政策(4月24日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...