[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 ...
随机推荐
- GanttProject 项目管理软件的优点
GanttProject 的优点 GanttProject 是一款基于 GPL 协议的开源软件,代码完全开源,使用的是 Java 语言编写. 最近在试用,有以下一些优大. 文件格式为 xml,这个是我 ...
- Linux之目录结构配置
因为 Linux 的开发者实在太多了,如果每个人都发展出属于自己的目录配置方法, 那么将可能会造成很多管理上的困扰.所以,就有一个叫做Filesystem Hierarchy Standard (FH ...
- erlang遍历目录
{ok, Cwd} = file:get_cwd(). Filelist = filelib:fold_files( Cwd, ".*", true, fun(File, Acc) ...
- jenkins构建触发器详解-不登录触发远程构建详解
利用jenkins的远程构建功能,我们可以使用任何脚本,甚至定制一个Web页来控制Job的执行,但是远程构建你如果直接使用的话,老是需要登录才能执行,如何避免登录?稍微折腾了一下,调通了. 1.首先去 ...
- Response的Content-Type一览
文件扩展名 Content-Type(Mime-Type) 文件扩展名 Content-Type(Mime-Type) .* application/octet-stream .tif image/t ...
- NodeJs中类定义及类使用
1.首先定义类Point,文件名为point.class.js: // 定义类 class Point { //构造函数 constructor(x, y) { this.x = x;//类中变量 t ...
- CSS之img标签
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- python之路——9
王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 学习内容 1.文件处理 1.打开文件 open(‘路径’,‘打开方式’) 打开方式 r w a r+ w+ ...
- Vue--父子组件之间的传值
1.父传子 1.在父组件中给子组件绑定动态属性 <v-home :title="title"></v-home> 2.在子组件中用propos[]接收 ex ...
- kubernetes学习笔记之十二:资源指标API及自定义指标API
第一章.前言 以前是用heapster来收集资源指标才能看,现在heapster要废弃了从1.8以后引入了资源api指标监视 资源指标:metrics-server(核心指标) 自定义指标:prome ...