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 ...
随机推荐
- 八、多线程爬虫(先占个位置,等整理好线程,进程,协程,异步IO在来写)
计算机的核心是CPU,CPU承担了所有的计算任务. 一个CPU核心,一次只能执行一个任务: 多个CPU核心同时可以执行多个任务. 一个CPU一次只能执行一个进程,其他进程处于非运行状态. 进程里包含的 ...
- ✅Vue选择图像
下载 Vue选择图像Vue选择图像 Vue 2.用于从列表中选择图像的组件 演示 https://mazipan.github.io/vue-select-image/ 安装 #纱 纱添加vue-se ...
- iOS企业重签名管理软件之风车签名
这是一款在Mac平台下安全可控的iOS签名管理软件,旨在对签名后的APP能够完全控制,包括APP的开启或禁用.设置到期时间锁.注入第三方动态库文件.设置安装限量.修改APP名称和自定义Bundle I ...
- [Docker]linux异常关机,docker镜像丢失
在运行中的docker容器遇到意外情况,可能会自动终止运行,例如磁盘空间不足. 解决办法: 找到/var/lib/docker/containers文件夹下的所有容器ID 执行命令,查看容器信息 ,找 ...
- python程序整理(2)
# 写一个函数完成三次登陆功能: # 用户的用户名密码从一个文件register中取出. # register文件包含多个用户名,密码,用户名密码通过|隔开,每个人的用户名密码占用文件中一行. # 完 ...
- 为什么大部分的程序员学编程,都会选择从C语言开始?
软件行业经过几十年的发展,编程语言的种类已经越来越多了,而且很多新的编程语言已经在这个领域从开始的默默无闻到如今风风火火,整个编程语言朝着集成化方向发展,这样会导致很多的初学者选择上不像以前那么单一了 ...
- linux(centos8):kubernetes安装的准备工作
一,安装docker-ce19.03.11 1,卸载podman [root@kubemaster ~]# dnf remove podman podman是红帽系os自带的容器,卸载是为了避免冲突 ...
- MVC注册
前言 最近没什么写的,写个MVC注册巩固一下 HTML @{ Layout = null; } <!DOCTYPE html> <html> <head> < ...
- Vue +WebSocket + WaveSurferJS 实现H5聊天对话交互
引言 在与实现了语音合成.语义分析.机器翻译等算法的后端交互时,页面可以设计成更为人性化.亲切的方式.我们采用类似于聊天对话的实现,效果如下: 智能客服(输入文本,返回引擎处理后的文本结果) 语音合成 ...
- RPM与YUM使用
1.RPM 1.1RPM简介 RPM全名RedHat Package Manager 优点: 1. 由于已经编译完成并且打包完毕,所以软件传输与安装上很方便 (不需要再重新编译): 2. 由于软件的信 ...