[leetcode]3. 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 subsequenceand not a substring.
题意:
给定一个字符串, 求其中无重复字母的最长子串
Solution1:
maintain a sliding window [start...i], making sure that each char in such window, its frequency is 1 (without Repeating chars)
when one char's frequency > 1, there is repeating char, then move pointer start to find the next window



code:
/*
Time Complexity: O(n)
Space Complexity: O(256)
*/
class Solution {
public int lengthOfLongestSubstring(String s) {
// corner case
if( s == null || s.length() == 0) return 0; int[] map = new int[256];
int result = 0;
int start = 0;
for(int i = 0; i < s.length(); i++){
map[s.charAt(i)] ++;
if(map[s.charAt(i)] > 1){
while(map[s.charAt(i)] > 1){
map[s.charAt(start)]--;
start++;
}
}
result = Math.max(result, i - start + 1);
}
return result;
}
}
[leetcode]3. Longest Substring Without Repeating Characters无重复字母的最长子串的更多相关文章
- [LeetCode]3. Longest Substring Without Repeating Characters无重复字符的最长子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- 【LeetCode】Longest Substring Without Repeating Characters(无重复字符的最长子串)
这道题是LeetCode里的第3道题. 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: ...
- leetcode 3. Longest Substring Without Repeating Characters 无重复字符的最长子串
一.题目大意 https://leetcode.cn/problems/longest-substring-without-repeating-characters/ 给定一个字符串 s ,请你找出其 ...
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- 3. Longest Substring Without Repeating Characters 无重复字符的最长子串
1. 原始题目 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 &quo ...
- 3. Longest Substring Without Repeating Characters无重复字符的最长子串
网址:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 显然采用sliding window滑 ...
- Leetcode3.Longest Substring Without Repeating Characters无重复字符的最长字串
给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 无重复字符的最长子串是 "abc",其长度为 ...
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
随机推荐
- java android 捕获未处理异常
1. 定义一个异常处理类 public class ExceptionHandler implements Thread.UncaughtExceptionHandler { public Excep ...
- jsp案例--展示数据库中的数据
一.什么是jsp? JAVA SERVER PAGES java的动态网页,servlet用来获取数据处理业务,擅长处理与java代码有关的内容.jsp展示数据,擅长处理与html有关的内容. 二.如 ...
- 更多FMK 的还是看万一的吧
http://www.cnblogs.com/del/category/323943.html 记录一下, 作为目录
- javascript mqtt 发布订阅消息
js client使用paho-mqtt,官网地址:http://www.eclipse.org/paho/,参考http://www.eclipse.org/paho/clients/js/官网给出 ...
- 学习笔记之Linux / Shell / QSHELL
shell(计算机壳层)_百度百科 http://baike.baidu.com/subview/849/15831672.htm Shell (computing) - Wikipedia, the ...
- 廖雪峰Java6 IO编程-2input和output-6classpath资源
1.从classpath读取文件可以避免不同环境下文件路径不一致的问题. Windows和Linux关于路径的表示不一致 Windows:C:\conf\default.properties Linu ...
- Word中一条删除不掉的单或双横线的解决办法
Word中一条删除不掉的单或双横线 有时你或许会遇到这样一种情况:在word中,有一条单或双横线怎么都删除不了,并且具有这样的特点: 在上面输入文字,横线会自动下调一行,如果文章过页,每页的尾部会有一 ...
- MPP数据库
MPP数据库 版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn.net/lyc417356935/article/details/45033069 MPP数据库定 ...
- 十六进制颜色值和rgb颜色值互相转换
在之前的一篇文章<将16进制的颜色转为rgb颜色>中,曾经写过将16进制的颜色转换为rgb颜色. 当然了,今天再看,还是有很多可以优化的地方,所以对之前的代码重构了一遍,并且同时写了一个反 ...
- Android SDK + Appium 环境搭建
一.JDK 安装 说明:JDK是包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具,所以必须最先安装. 链接: https://pan.baidu.com/s/1NfNK_K7vukF ...