LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
给定一个字符串,找到最长的子串的长度没有重复字符。
这题其实思路就是把字符串中的字符遍历,记录每个字符的位置,遇到相同的字符更新位置,然后比较长度。可是自己做的乱七八糟,多出很多没用的东西。
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
var count = 0,//本来想用来记录当前连续无重复字符数,但其实不需要,i-start+1就等于它
obj = {},
arr = s.split(''),
start = 0,
length = 0,
del = "",//本来想用来记录,然后删除重复字符之前的,没用了的字符,但其实完全没必要,只需要记录历史以前最长长度length和当前无重复字符开始位置start就可以
Substring = "";//审题不仔细,本来以为要返回子字符串的
for(var i=0;i<arr.length;i++){
//debugger;
if(obj[arr[i]] === undefined){
}else{
start = obj[arr[i]] + 1; }
if(i-start+1 > length){
Substring = s.slice(start,i+1);
}
obj[arr[i]] = i;
}
return length;
};
然后对比热门答案,真是无地自容,解题方法没想到都还算了,本质思路其实不差多少,却多出来那么多垃圾代码。以后要注意审视自己写的东西了
public int lengthOfLongestSubstring(String s) {
if (s.length()==0) return 0;
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int max=0;
for (int i=0, j=0; i<s.length(); ++i){
if (map.containsKey(s.charAt(i))){
j = Math.max(j,map.get(s.charAt(i))+1);
}
map.put(s.charAt(i),i);
max = Math.max(max,i-j+1);
}
return max;
}
var lengthOfLongestSubstring = function(s) {
var obj = {},
arr = s.split(''),
start = 0,
length = 0;
for (var i = 0; i < arr.length; i++) {
if (obj.hasOwnProperty(arr[i])) {
start = obj[arr[i]] + 1;
}
obj[arr[i]] = i;
length = Math.max(length, i - start + 1)
}
return length;
};
LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters的更多相关文章
- LeetCode 第 3 题(Longest Substring Without Repeating Characters)
LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...
- (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...
- 【leetcode刷题笔记】Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- leetcode笔记:Longest Substring Without Repeating Characters
一. 题目描写叙述 Given a string, find the length of the longest substring without repeating characters. For ...
- 【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第三题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
Problem:Given a string, find the length of the longest substring without repeating characters. For e ...
- LeetCode(3)Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
随机推荐
- 大数据环境下mongoDB要加索引
mongodb在存储大数据时,对查询的字段需要添加索引,我测试的是阿里云30多万的数据量,不加索引查询已经到8秒,而添加索引之后是毫秒级! 为集合加索引 mongodb支持内嵌属性添加索引 db.ag ...
- RESTful 架构风格
在移动互联网的大潮下,『微服务』的概念也越来越被大家接受并应用于实践,日益增多的web service逐渐统一于RESTful 架构风格,如果开发者对RESTful 架构风格不甚了解,则开发出的所谓R ...
- Vue-cli脚手架 安装 并创建项目--命令
检查是否有 node - v 安装Vue-cli npm install -g vue-cli 安装好后,执行 vue list可以看到很多实用的模板,我这里实用的webpack 初始化模板 vue ...
- spring @Transaction事务回滚失败
今天客户提出一个新问题,出库一批商品,提示失败了,但是库存数量却减少了.看了一下代码一头雾水,我们的代码加了事物,且捕获异常. 经过调试代码发现就是两个原因导致的 第一.在当前方法的catch中处理了 ...
- 基于django中间件的编程思想
目录 前言 前期准备 importlib模块介绍 基于django中间件的编程思想 django中settings源码 配置文件的插拔式设计 基于django中间件的思想,实现功能配置 前言 在学习d ...
- ML.NET Model Builder 更新
ML.NET是面向.NET开发人员的跨平台机器学习框架,而Model Builder是Visual Studio中的UI工具,它使用自动机器学习(AutoML)轻松地允许您训练和使用自定义ML.NET ...
- QT Creator: The process could not be started!
如果往工程里面增加了uac.manifest 文件后,QT creator不通过管理员启动的话,若要debug程序的话,就会提示 “The process could not be started!” ...
- JavaWeb包含哪些内容
JavaWeb JavaWeb课程内容涉及:HTML5课程.CSS3.JavaScript.MySQL使用.JDBC连接池.Servlet.JSP.AJAX.jQuery.Bootstrap. 第一部 ...
- redis启动错误: Warning: no config file specified, using the default config. In order to specify a config
redis启动错误: Warning: no config file specified, using the default config. In order to specify a config ...
- Linux 部署 FastDFS
FastDFS 安装规划: 项目 信息 Group Name group1 FastDFS安装主目录 /usr/local/fastdfs-5.0.8 FastDFS work主目录 /usr/loc ...