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 ...
随机推荐
- Linux(一)
1.简单命令 1.1 ls指令 语法1:#ls [路径] 表示列出指定路径下的文件夹和文件的名字,如果路径没有指定则列出当前路径下的(lis ...
- vue-better-scroll实现移动端下拉加载组件
1.下载安装better-scroll npm i -S better-scroll 1.1安装完成之后,打开pacaage.json文件查看,是否有(better-scroll) "dep ...
- window 操作
忘记密码:https://jingyan.baidu.com/article/066074d6762a59c3c21cb0f9.html
- redis cluster集群动态伸缩--删除主从节点
目标:从集群中剔除一组主从(5007,5008) 经过上一节增加5007,5008主从服务节点后,目前集群的情况是这样的: b3363a81c3c59d57143cd3323481259c044e66 ...
- AJAX-CORS 跨域
1.CORS就是一套AJAX跨域问题的解决方案. 2.CORS的原理: CORS定义一种跨域访问的机制,可以让AJAX实现跨域访问. 3.CORS浏览器支持情况: Chrome 3+ Firefox ...
- DEBUG的基本命令的使用[MASM]
DEBUG的基本命令的使用 DEBUG是专门为汇编语言设计的一种调试工具,它通过步进,设置断点等方式为汇编语言程序员提供了非常有效的调试手段. DEBUG的命令都是一个字母,后跟一个或多个参数:字母 ...
- 你的首个golang语言详细入门教程 | your first golang tutorial
本文首发于个人博客https://kezunlin.me/post/a0fb7f06/,欢迎阅读最新内容! your first golang tutorial go tutorial version ...
- 2019年腾讯最新Java工程师面试题
一.单选题(共21题,每题5分) 1在正则表达式当中下面那一个字符集表示非空格字符 A.[:graph:] B.[:digit:] C.[:space:] D.[:alpha:] 参考答案:A 答 ...
- Java - 变量常量数据类型
标识符命名规范 可以有字母数字下划线和美元符组成, hello abc 不能以数字开头 123abc 严格区分大小写 void Void 不能是java的关键字和保留字 class 标识符必须是见名知 ...
- Linux安装docker-compose
下载:curl -L https://get.daocloud.io/docker/compose/releases/download/1.16.1/docker-compose-`uname -s` ...