最长无重复字符的子串 · Longest Substring Without Repeating Characters
[抄题]:
给定一个字符串,请找出其中无重复字符的最长子字符串。
例如,在"abcabcbb"
中,其无重复字符的最长子字符串是"abc"
,其长度为 3
。
对于,"bbbbb"
,其无重复字符的最长子字符串为"b"
,长度为1
。
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
hashset, hashmap想着是简单,但是实现起来都要先存再取,其实比较麻烦
涉及到字母甚至256个字符的出现次数时,直接用数组int[256]反而比较方便,随意地可以设成0 1,修改极其方便。
有数组的话,和两根指针练习比较紧密
[一句话思路]:
同向不定窗口
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
i复位时写成j了,要小心
[总结]:
boy全部遍历到(再次初始化i)且只走一次,girl全部遍历到且只走一次(j不复位)
[复杂度]:Time complexity: O(2n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
charat(i)是圆括号不是方括号
[关键模板化代码]:
for (i = 0; i < s.length(); i++) {
while (j < s.length() && map[s.charAt(j)] == 0) {//bracket
j++
}
map[s.charAt(i)] = 0;
}
同向不定窗口
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
public class Solution {
/**
* @param s: a string
* @return: an integer
*/
public int lengthOfLongestSubstring(String s) {
//corner case
if (s == null) {
return 0;
}
//i,j go in the same dir
int i = 0, j = 0;
int ans = 0;
int[] map = new int[256]; for (i = 0; i < s.length(); i++) {
while (j < s.length() && map[s.charAt(j)] == 0) {//bracket
map[s.charAt(j)] = 1;
ans = Math.max(ans, j - i + 1);
j++;
}
map[s.charAt(i)] = 0;
} return ans;
}
}
最长无重复字符的子串 · Longest Substring Without Repeating Characters的更多相关文章
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- lintcode: 最长无重复字符的子串
题目 最长无重复字符的子串给定一个字符串,请找出其中无重复字符的最长子字符串. 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc",其长度为 ...
- lintcode-384-最长无重复字符的子串
384-最长无重复字符的子串 给定一个字符串,请找出其中无重复字符的最长子字符串. 样例 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc" ...
- [Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters
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. Example 1: In ...
- LeetCode 3: 无重复字符的最长子串 Longest Substring Without Repeating Characters
题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. Given a string, find the length of the longest substring withou ...
- LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)
这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...
- leetcode-最长无重复字符的子串
参考他的人代码:https://blog.csdn.net/littlebai07/article/details/79100081 给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: ...
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
随机推荐
- linux 服务器性能调优总结
1.性能分析的几个方面 https://blog.csdn.net/w174504744/article/details/53894127 2.cpu 性能分析工具 perf https://blog ...
- HDU 2485
http://acm.hdu.edu.cn/showproblem.php?pid=2485 n个车站,m条边,两边之间费用为1,问最少摧毁多少车站,使得1-n无法在k时间内到达 将2-(n-1)每个 ...
- Centos 中扩展 软件源 的安装 之 epel ( 为yum 扩展软件源 EPEL源 )
EPEL (Extra Packages for Enterprise Linux)是基于Fedora的一个项目,为“红帽系”的操作系统提供额外的软件包,适用于RHEL.CentOS和Scientif ...
- c# 添加注册表
- logback节点配置详解
一 :根节点 <configuration></configuration> 属性 : debug : 默认为false ,设置为true时,将打印出logback内部日志信 ...
- vim之vundle
git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle,下载到本地 gvim ~/.vimrc set nocompat ...
- java.nio.charset.UnsupportedCharsetException: cp0
使用jython调用python,提示console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0. ...
- php error_log记录日志的使用方法和配置 (日志目录一定要手动创建)
对于PHP开发者来 说,一旦某个产品投入使用,应该立即将 display_errors选项关闭,以免因为这些错误所透露的路径.数据库连接.数据表等信息而遭到黑客攻击.但是,任何一个产品在投入使用后,都 ...
- Ubuntu 18.04 gcc降级为4.8.5版本
1. 下载gcc/g++ 4.8 $ sudo apt-get install -y gcc-4.8.5 $ sudo apt-get install -y g++-4.8.5 2. 链接gcc/g ...
- CentOS 修改IP地址为静态IP
vi /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE=Ethernet BOOTPROTO=static DEFROUTE=yes NAME=eth0 ...