[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 ...
随机推荐
- Linux进程调度与抢占
一.linux内核抢占介绍 1.抢占发生的必要条件 a.preempt_count抢占计数必须为0,不为0说明其它地方调用了禁止抢占的函数,比如spin_lock系列函数.b.中断必须是使能的状态,因 ...
- 第2章 Java基本语法(上): 变量与运算符
2-1 关键字与保留字 关键字(keyword) 保留字(reserved word) 2-2 标识符(Identifier) 案例 class Test{ public static void ma ...
- 算法笔记 3.2 codeup1934 找X
#include <stdio.h> ; int a[maxn]; int main(void){ int n; while(scanf("%d", &n)!= ...
- 轻量应用服务器安装 phpMyAdmin
第一步:在phpMyAdmin官方网站http://www.phpmyadmin.net/downloads/下载源码包并解压 cd /usr/local/src wget https://files ...
- Python中Lambda表达式使用
软件环境 Python: 2.7.13; win10 Lambda描述 python 使用 lambda 表达式来创建匿名函数 lambda只是一个表达式,函数体比def简单很多 lambda的主体是 ...
- HTTPS如何保证数据传输的安全性 -- 结合加密
什么是HTTPS: HTTP就是我们平时浏览网页时候使用的一种协议 HTTP协议传输的数据都是未加密的,也就是明文的,因此使用HTTP协议传输隐私信息非常不安全.为了保证这些隐私数据能加密传输,于是网 ...
- (整理)MySQL_REHL6.5 MySQL5.5 中文支持问题
1 查看字符集命令 SHOW VARIABLES LIKE ‘character%’; 2 修改my.cnf文件中来实现中文支持 2.1 复制my.cnf 安装结束后,etc目录下并没有my.cnf文 ...
- Struts2 中常用的代码
BaseAction public class BaseAction extends ActionSupport { protected String target; public Map getRe ...
- 用GDB调试程序(三)
四.维护停止点 上面说了如何设置程序的停止点,GDB中的停止点也就是上述的三类.在GDB中,如果你觉得已定义好的停止点没有用了,你可以使用delete.clear.disable.enable这几个命 ...
- 【Linux】【Chrome】安装Chrome浏览器的攻略
https://blog.csdn.net/chenlix/article/details/72526205 1.切换到root: su - 或者 sudo -i 2.下载新的软件源定义: cd /e ...