【leetcode刷题笔记】Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without 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.
题解:贪心算法。
利用一个hashmap存放当前最长的无重substring所包含的元素,变量leftbound存放当前的最左边界。则当前遍历的字符s[i]有两种情况:
- s[i]所指的元素已经存在在map中了,那么就左移leftbound直到leftbound到i这一段不再包含s[i],并且在map中清除leftbound扫过的元素;
- s[i]所指的元素不在map中,保持leftbound不动,查看是否需要更新当前最长substring的长度。
代码如下:
public class Solution {
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0)
return 0; HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int leftebound = 0;
int answer = 0; for(int i = 0;i < s.length();i++){
char current = s.charAt(i);
if(map.containsKey(s.charAt(i))){
while(s.charAt(leftebound) != current && leftebound < i){
map.remove(s.charAt(leftebound));
leftebound++;
}
leftebound++;
}
else {
map.put(current, 0);
answer = Math.max(i-leftebound+1, answer);
}
} return answer;
}
}
【leetcode刷题笔记】Longest Substring Without Repeating Characters的更多相关文章
- 刷题3. Longest Substring Without Repeating Characters
一.题目 Longest Substring Without Repeating Characters,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug ...
- 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...
- 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)
Given a string, find the length of the longest substring without repeating characters. Example 1: ...
- Leetcode第三题《Longest Substring Without Repeating Characters》
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- 【LeetCode】3 、Longest Substring Without Repeating Characters
题目等级:Medium 题目描述: Given a string, find the length of the longest substring without repeating chara ...
- Leetcode经典试题:Longest Substring Without Repeating Characters解析
题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...
- LeetCode(3)Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- 【leetcode刷题笔记】Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...
- [刷题] 3 Longest Substring Without Repeating Character
要求 在一个字符串中寻找没有重复字母的最长子串 举例 输入:abcabcbb 输出:abc 细节 字符集?字母?数字+字母?ASCII? 大小写是否敏感? 思路 滑动窗口 如果当前窗口没有重复字母,j ...
- LeetCode Hash Table 3. Longest Substring Without Repeating Characters
HashMap的应用可以提高查找的速度,键key,值value的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...
随机推荐
- ABP框架EF6链接Oracle数据库手动迁移
环境:VS2017 + ABP官方模板(不含Zero) +Oracle 11Gx64DB + Oracle 11Gx32Client(PLSQL工具访问) 一.Abp项目的下载以及运行 1.创建ab ...
- 后期给项目加入Git版本控制
一.为项目加上Git 1.进入对应文件夹 2.git init(初始化一个空的代码仓库) 3.git add .(将当前目录和子目录的文件标记为要添加到代码仓库) 4.git commit -m &q ...
- Excel中判断一个表中的某一列的数据在另一列中是否存在
A B C D 1 10 3 有 2 6 e 无 3 3 6 有 判断c列的值在A列中是否存在(假定C列为需要判断列,A列为目标列) 在D1中输入以下公式,然后下拉公式即可 =IF(C ...
- 复习mybatis框架(一)----映射文件
参考博主的文章,尊重原创:https://blog.csdn.net/qq_35246620/article/details/54837618 一.给出映射文件 Mapper.xml 的总结: ① 设 ...
- Linux基础之常用基本命令备忘
Linux基础之常用基本命令备忘 PWD 查询当前所在Linux上的位置 / 根目录 CD(change directory)切换目录 语法 CD /(注意添加空格) LS ...
- Quartz.Net - Lesson2: 任务和触发器
Lesson 2: 任务和触发器 本系列文章是官方3.x文档的翻译,原文地址:https://www.quartz-scheduler.net/documentation/quartz-3.x/tut ...
- Error in as.POSIXlt.character(x, tz, ...) :
> sqlFetch(channel,"user")Error in as.POSIXlt.character(x, tz, ...) : character strin ...
- 在Ubuntu下编译FFmpeg
第一步:准备编译环境 .tar.bz2 -2245/ ./configure --enable-static--enable-shared--prefix=/usr/localmakesudomake ...
- yum 无法安装mysql
昨晚帮盆友搭建服务器时,一直出现yum mysql 无法安装.报错信息如下: Transaction Check Error: file /etc/my.cnf from install of my ...
- strpos 判断字符串是否存在
strpos 中为什么要用逗号隔开的原因是因为 防止找出相匹配的中 , 如 查找1 而数组中 存在 12 那么这个结果也是可以找出来的 ,分别在1 前后加个, 就是为了区 ...