博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 3. Longest Substring Without Repeating Characters
阅读量:4313 次
发布时间:2019-06-06

本文共 915 字,大约阅读时间需要 3 分钟。

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.将每组子串的字母存在hashset中,判断当前字母是否存在set中(hash查找效率高)。

2.若存在则判断下一组子串。下组子串的初始位置在重复字母处位置+1。

3.若不存在则将此字母存在容器中,并判断当然容器大小是不是最大, 若是最大表示当前长度最大。

如abcabcbb,

1.容器中一次存入第一个子串abc,到第2个a事与第一个a重复,容器中移除第一个a到第一个子串的初始位置0的所有元素,容器中的元素为abc。

2.容器中子串bca,第二个b重复,相同办法。

注:判断下个子串的初始位置,为重复字母的位置+1,类似于kmp算法。

1 public int lengthOfLongestSubstring(String s) { 2         int max = 0; 3         int flag = 0; 4         Set
set = new HashSet
(); 5 char[] a = s.toCharArray(); 6 for(int i=0;i

 

转载于:https://www.cnblogs.com/lolybj/p/8453964.html

你可能感兴趣的文章
使用case语句给字体改变颜色
查看>>
JAVA基础-多线程
查看>>
面试题5:字符串替换空格
查看>>
JSP九大内置对象及四个作用域
查看>>
ConnectionString 属性尚未初始化
查看>>
数据结构-栈 C和C++的实现
查看>>
MySQL基本命令和常用数据库对象
查看>>
poj 1222 EXTENDED LIGHTS OUT(位运算+枚举)
查看>>
进程和线程概念及原理
查看>>
Lucene、ES好文章
查看>>
android 生命周期
查看>>
jquery--this
查看>>
MySQL 5.1参考手册
查看>>
TensorFlow安装流程(GPU加速)
查看>>
OpenStack的容器服务体验
查看>>
BZOJ1443: [JSOI2009]游戏Game
查看>>
【BZOJ 4059】 (分治暴力|扫描线+线段树)
查看>>
BZOJ 1066 蜥蜴(网络流)
查看>>
提高批量插入数据的方法
查看>>
Linux重启Mysql命令
查看>>