leetcode146 longest-substring-without-repeating-character
题目描述
repeating characters. For example, the longest substring without
repeating letters for "abcabcbb" is "abc", which the length is 3. For
"bbbbb" the longest substring is "b", with the length of 1.
import java.util.HashMap;
/*
滑动窗口,比方说,abcabccc 当你右边扫描到abca的时候,你得把第一个a删掉得到bca,
然后,“窗口”继续向右滑动,每当加到一个新的char的时候,左边检查有无重复的char,然后如果没有重复的就正常添加
有重复的话就左边扔掉一部分,从最左到重复char这段扔掉,在这个过程中记录最大窗口长度
*/
public class Solution {
/**
*
* @param s string字符串
* @return int整型
*/
public int lengthOfLongestSubstring (String s) {
// write code here
if (s==null || s.length()==0) return 0;
HashMap <Character,Integer> map=new HashMap<Character ,Integer>();
int leftBound=0;
int max=0;
for (int i=0;i<s.length();i++){
char c=s.charAt(i);
leftBound=Math.max(leftBound,(map.containsKey(c))?map.get(c)+1:0);
max=Math.max(max,i-leftBound+1);
map.put(c,i);
}
return max;
}
}
leetcode146 longest-substring-without-repeating-character的更多相关文章
- length of the longest substring without repeating character
Given a string, find the length of the longest substring without repeating characters. 来源:力扣(LeetCod ...
- 3. Longest Substring Without Repeating Character[M] 最大不重复子串
题目 Given a string, find the length of the longest substring without repeating characters. Example 1: ...
- [刷题] 3 Longest Substring Without Repeating Character
要求 在一个字符串中寻找没有重复字母的最长子串 举例 输入:abcabcbb 输出:abc 细节 字符集?字母?数字+字母?ASCII? 大小写是否敏感? 思路 滑动窗口 如果当前窗口没有重复字母,j ...
- Leetcode 解题 Longest Substring without repeating charcater python
原题: Given a string, find the length of the longest substring without repeating character For example ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- No.003:Longest Substring Without Repeating Characters
问题: Given a string, find the length of the longest substring without repeating characters.Example:Gi ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- No.003 Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters Total Accepted: 167158 Total Submissions: 735821 Diff ...
- Java [leetcode 3] Longest Substring Without Repeating Characters
问题描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
随机推荐
- Jmeter JDBC Request 使用详解
本篇博文讲解以MySQL为例,搞懂JDBC Request中MySQL的使用方法,换成其它数据库, 如Oracle.PSQL也会很容易上手. 一.基本配置 1.首先我们先了解一下,不同数据库的驱动类和 ...
- ORA-28001: the password has expired 密码已过期
ORA-28001: the password has expiredORA-28001: 密码已过期 Cause: The user's account has expired and ...
- auto_send_tablespace.sh
简述:周期定时发送表空间到指定邮箱内 1.修改邮箱配置 /etc/mail.rc,具体细节见网上教程 $ vi /etc/mail.rc set from=123456@qq.comset smtp= ...
- 多测师讲解html _图片标签003_高级讲师肖sir
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>段 ...
- 【python】python返回结果多了none(递归时)
把每个返回值的print使用return替代即可 例子: def trim(s): if s[:1]==" ": s=s[1:] retrim(s) elif s[-1:]==&q ...
- mac安装go环境
下载pkg文件 https://golang.google.cn/dl/ sudo vim /etc/profile export GOROOT=/usr/local/go export GOPA ...
- Hugo+Github 搭建个人博客(Windows环境下)
目录 Hugo+Github 搭建个人博客(Windows环境下) 1.前言 2.Differences 2.1 https vs SSH 2.2 新建的github的仓库名必须为 用户名+githu ...
- 主流开源分布式图数据库 Benchmark
本文由美团 NLP 团队高辰.赵登昌撰写 首发于 Nebula Graph 官方论坛:https://discuss.nebula-graph.com.cn/t/topic/1377 1. 前言 近年 ...
- influxdb集群部署
环境准备 influxdb enterprise运行条件最低需要三个meta nodes节点以及两个data nodes Meta nodes之间使用TCP和Raft一致性协议通信,默认端口为8089 ...
- Redis (总结)
transactions redis的事务并不能回滚,即使执行失败了,后面的命令一样会执行 exec命令触发前面被queue的命令原子执行 最后:transaction最终将被scripts替代,因为 ...