leetcode第三题
leetcode第三题:
题目:
给定一个字符串,找出不含有重复字符的最长子串的长度。
源码(使用java语言):
class Solution {
public int lengthOfLongestSubstring(String s) {
int result = 0;
int front = 0 ;
int after = 0;
char[] temp = s.toCharArray();//save the String to a chararray
Map<Character,Integer> map = new HashMap<>();
while(after<temp.length&&front<temp.length){
if(map.containsKey(temp[after])){
if(front<map.get(temp[after])+1){
front = map.get(temp[after])+1;
}
}
map.put(temp[after],after);
after++;
if(after - front >result){
result = after -front;
}
}
return result;
}
}
用时64ms
算法亮点,利用map键值对以及游标来巧妙获取最长子串长度,且仅需遍历一遍
因为map的containskey在查询到一个相同的值是就返回true,所以二层嵌套的if语句要加上front<map.get(temp[after])以保证front游标一直向前浮动
以上
leetcode第三题的更多相关文章
- leetcode第三题--Longest Substring Without Repeating Characters
Problem:Given a string, find the length of the longest substring without repeating characters. For e ...
- leetcode第三题Longest Substring Without Repeating Characters java
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- 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)
LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...
- 刷题之路第三题--Longest Substring Without Repeating Characters
问题简介:求给定字符串中最长的字符不重复的字符串的长度 问题详解: 给定一个字符串,寻找给定字符串中包含的最长的字符不重复的字符串的长度 注:答案必须是子字符串,不是子序列 是连续的字符不重复的字符串 ...
- 【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. Examples: Giv ...
- LeetCode(3)Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- LeetCode Hash Table 3. Longest Substring Without Repeating Characters
HashMap的应用可以提高查找的速度,键key,值value的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...
随机推荐
- [转]Visual Studio 2010 中安装Qt 5.1
截至目前(2013年7月12日)为止,Qt 的最高版本为Qt5.1,在该版本中已经将Qt Creator与Qt Lib集成在一个文件夹中,因此安装的时候较为方便,只需安装一个即可.因为Qt具有超强的可 ...
- 逼格高又实用的Linux高级命令,开发运维都要懂!
在运维的坑里摸爬滚打好几年了,我还记得我刚开始的时候,我只会使用一些简单的命令,写脚本的时候,也是要多简单有多简单,所以有时候写出来的脚本又长又臭. 像一些高级点的命令,比如说 Xargs 命令.管道 ...
- ASP.NET MVC 自定义处理JSON ActionResult类
1.统一JSON格式处理方式,同时指定ContentType类型,解决低版本浏览器获取json时ContentType为application/json提示下载的问题. public abstract ...
- Effective Java 第三版——61. 基本类型优于装箱的基本类型
Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...
- HTML5 完美解决javascript中iphone手机和android手机复制文本到剪切板问题
1.执行以下解决方案条件:(这个是原理) ①执行复制方法时 所复制文字不能被任何 块级元素和行内块元素和行内元素遮盖否则无效:(解决方案:将文本通过绝对定位或其他方式移除屏幕外) ②ios中不能复制属 ...
- [k8s]jenkins配合kubernetes插件实现k8s集群构建的持续集成
另一个结合harbor自动构建镜像的思路: 即code+baseimage一体的方案 - 程序员将代码提交到代码仓库gitlab - 钩子触发jenkins master启动一次构建 - jenkin ...
- JPA+Hibernate 3.3 ——增删改查
1. 查找对象 1) 用find()方法查找对象 public void getPerson(){ EntityManagerFactory factory = Persistence. ...
- mongodb浅析
设计特征: MongoDB 的设计目标是高性能.可扩展.易部署.易使用,存储数据非常方便.其主要功能特性如下. (1)面向集合存储,容易存储对象类型的数据.在MongoDB 中数据被分组存储在集合中, ...
- ES6,Array.copyWithin()函数的用法
ES6为Array增加了copyWithin函数,用于操作当前数组自身,用来把某些个位置的元素复制并覆盖到其他位置上去. Array.prototype.copyWithin(target, star ...
- pandas遍历行数据
假设我的DataFrame如图所示: 我可以这样遍历它: for index,row in list.iterrows(): id =row["id"] x=row["x ...