Question:

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.

问题描述:

给一个字符串,找出这个字符串的无重复字符的最长子串的长度。

实例:

1,“abcabcbb”的最长子串是“abc”,长度为3。

2,“bbbbb”的最长子串是“b”,长度为1。

3,“pwwkew”的最长子串是“wke”,长度为3。

解题思路:

这个题的关键在于重复的字符!

一个子串不可能出现重复的字符,那么我们可以用两个指针,一个指针用来遍历字符串,两个指针之间保持无重复字符,那么两个指针之间的长度即最大子串的长度。当发现有重复的字符时,另一个指针指向这个字符上一次出现的位置的下一个字符,继续遍历,直到找到最长子串。

代码示例:

 public int lengthOfLongestSubstring(String s) {
if (s.length()==0) return 0;
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int max=0;
for (int i=0, j=0; i<s.length(); ++i){
if (map.containsKey(s.charAt(i))){
j = Math.max(j,map.get(s.charAt(i))+1);
}
map.put(s.charAt(i),i);
max = Math.max(max,i-j+1);
}
return max;
}

最长子串(Leetcode-3 Longest Substring Without Repeating Characters)的更多相关文章

  1. [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  2. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

  3. [leetcode]3. Longest Substring Without Repeating Characters无重复字母的最长子串

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  4. [LeetCode]3. Longest Substring Without Repeating Characters无重复字符的最长子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  5. leetcode 3 Longest Substring Without Repeating Characters最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  6. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  7. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  8. Leetcode 3. Longest Substring Without Repeating Characters (Medium)

    Description Given a string, find the length of the longest substring without repeating characters. E ...

  9. [Leetcode Week1]Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...

随机推荐

  1. CF1083C Max Mex 线段树

    题面 CF1083C Max Mex 题解 首先我们考虑,如果一个数x是某条路径上的mex,那么这个数要满足什么条件? 1 ~ x - 1的数都必须出现过. x必须没出现过. 现在我们要最大化x,那么 ...

  2. 【BZOJ1041】圆上的整点(数论)

    [BZOJ1041]圆上的整点(数论) 题面 BZOJ 洛谷 题解 好神仙的题目啊. 安利一个视频,大概是第\(7\)到\(19\)分钟的样子 因为要质因数分解,所以复习了一下\(Pollard\_r ...

  3. 【转】高手带你深入理解ucos任务堆栈

    首先,我们来理解一下两个概念: 1.堆栈就是一段连续的空间.用于存储数据的,在c计算机中有很多应用,比如发生中断时保存现场,c语言函数调用时保存现场和临时变量. 2.堆栈指针就是一个数据指针.有时候计 ...

  4. 转载OPENCV学习随笔

    转载自 亦轩Dhc http://www.cnblogs.com/daihengchen/p/5492729.html 学习笔记:使用opencv做双目测距(相机标定+立体匹配+测距).   最近在做 ...

  5. Kubernetes集群中Service的滚动更新

    Kubernetes集群中Service的滚动更新 二月 9, 2017 0 条评论 在移动互联网时代,消费者的消费行为已经“全天候化”,为此,商家的业务系统也要保持7×24小时不间断地提供服务以满足 ...

  6. nginx 代理tcp长连接短连接配置

    https://blog.csdn.net/tayinyinyueyue/article/details/78932697 nginx使用ngx_stream_core_module模块代理tcp长连 ...

  7. 个人常用Linux文件操作命令

    本文只记录个人工作中最常用到的命令.   关机和用户 shutdown -h now 关机 reboot 重启 sync 非正常关机前执行,强制将内存缓冲区中的数据立即写入磁盘中 logout 注销 ...

  8. numpy二进制转换和范围缩放

    numpy二进制转换和范围缩放 觉得有用的话,欢迎一起讨论相互学习~Follow Me 一维二进制转换 import numpy as np # 一维二进制数组转换 a=np.array([0,1,1 ...

  9. C语言复习---判断素数

    一般 int main01() { ; scanf("%d", &a); n_sqrt = sqrt(a); ; i <= n_sqrt; i++) ) { flag ...

  10. python---基础知识回顾(六)网络编程2(处理粘包)

    前戏: 之前在python---基础知识回顾(六)网络编程异步模块中提到过粘包现象,而且在使用twisted中提到过一种处理办法,按行接收lineReceived,当收到\r\n换行符时,才去缓冲区中 ...