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的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...
随机推荐
- iOS 7设计备忘单
With the release of iOS 7, app designers and developers will need to adjust their visual language to ...
- Web前端,HTML5开发,前端资源,前端网址,前端博客,前端框架整理 - 转改
Web前端/H5开发,前端资源,前端网址,前端博客,前端框架整理 综合类 前端知识体系 前端知识结构 Web前端开发大系概览 Web前端开发大系概览-中文版 Web Front-end Stack v ...
- OpenCV 学习笔记 04 深度估计与分割
本章节主要是使用深度摄像头的数据来识别前景区和背景区,这样就可以分别对前景和背景做不同的处理. 1 创建模块
- Atitit phpstorm配置attilax总结
Atitit phpstorm配置attilax总结 1. 前期准备 1 1.1. 配置interpreter 1 1.2. debug需要xdebug的支持,不管是script模式还是web模式 3 ...
- python 函数的参数的几种类型
定义函数的时候,我们把参数的名字和位置确定下来,函数的接口定义就完成了.对于函数的调用者来说,只需要知道如何传递正确的参数,以及函数将返回什么样的值就够了,函数内部的复杂逻辑被封装起来,调用者无需了解 ...
- MXNET:欠拟合、过拟合和模型选择
当模型在训练数据集上更准确时,在测试数据集上的准确率既可能上升又可能下降.这是为什么呢? 训练误差和泛化误差 在解释上面提到的现象之前,我们需要区分训练误差(training error)和泛化误差( ...
- ecshop首页调用团购信息产品购买人数
当我们在ecshop后台录入ecshop的团购信息之后,我们在ecshop的前台首页index.php里面,可以看到他能调用ecshop的团购商品信息,其中就包括团购商品的一些价格信息.但是如何在ec ...
- http://www.cnblogs.com/chenmeng0818/p/6370819.html
http://www.cnblogs.com/chenmeng0818/p/6370819.html js中的正则表达式入门 什么是正则表达式呢? 正则表达式(regular expression ...
- Java 继承中构造方法的执行顺序问题
在Java中,如果一个类没有任何显式创建的构造器则该类默认会有一个无参构造器:如果显式创建了有参构造器则该类就不再有默认无参构造器. 在Java继承中,构造器并不能被继承,而是被显示或隐式调用. 1. ...
- 【Oracle】删除所有表
BEGIN FOR cur_rec IN (SELECT object_name, object_type FROM user_objects WHERE object_type IN ('TABLE ...