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 ...
随机推荐
- Socket实现简易聊天室,Client,Server
package seday08; import java.io.BufferedWriter;import java.io.OutputStream;import java.io.OutputStre ...
- js 淡入淡出的tab选项卡
代码如下 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...
- 配置基于服务器认证的Dynamics 365 Customer Engagement和SharePoint Online集成
我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...
- 团队开发的代码管理(VS)
1.文档 代码需要一个文档说明代码的基本情况,使用的组件,代码逻辑层等等 2.源代码冲突(Git) 首先需要尽可能避免冲突,公共的工具基类尽可能不动,如果需要修改也交给专人修改不能谁都上去修改 项目按 ...
- jQuery仿京东首页广告图片切换图片轮播
1.效果图如下: 2.源码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charse ...
- supervisor 工具使用
最近项目要使用supervisor 来管理程序,简单查了查,发现比较容易使用: 中文博客查了查,发现很多人都写出了教程,我这边就懒得写了,找了几个能看懂的记录如下: https://www.cnblo ...
- Linux系统学习 十三、VSFTP服务—相关文件
常见的FTP服务器程序 IIS.Serv-U (windwards中) wu-ftpd(淘汰了).Proftpd (Linux中) vsftpd(Very Secure ...
- 005.SQLServer AlwaysOn可用性组高可用简介
一 AlwaysOn 可用性组 1.1 AlwaysOn 可用性组概述 AlwaysOn 可用性组功能是一个提供替代数据库镜像的企业级方案的高可用性和灾难恢复解决方案.SQL Server 2012 ...
- nfs存储服务器
1.nfs的基础简介 1.1:什么是nfs? 它的主要功能是通过网络让不同的机器系统之间可以彼此共享文件和目录.NFS服务器可以允许NFS客户端将远端NFS服务器端的共享目录挂载到本地的NFS客户端中 ...
- vue template标签
在普通的html里面: template标签默认有个 display:none; 属性,并且其里面的内容是不可见的. 在vue里面: template标签类似一个隐藏的div,在审查元素的时候是找不到 ...