问题描述:

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.

问题思路:

(1)审清题目:最长不重复的子串

(2)将第i个字符放在数组arr中,判断下一个字符有没有在arr中出现,如果出现,则重新从i-len+1处开始。

(3)需要记录下每次循环的arr的最大长度

code:

var lengthOfLongestSubstring = function(s) {
var arr=[];
var str = s.split('');
var max = 0;
var len = 0; //arr[]数组的长度
for(var i=0;i<str.length;i++){
if(arr.indexOf(str[i])==-1){
arr.push(str[i]);
len = len + 1;
max = max > len ? max : len;
}else{
i = i - len + 1;
arr.splice(0, len, str[i]);
len = 1;
}
}
return max;
};

leetcode--js--Longest Substring Without Repeating Characters的更多相关文章

  1. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

  2. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  3. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  4. [LeetCode][Python]Longest Substring Without Repeating Characters

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  5. LeetCode之Longest Substring Without Repeating Characters

    [题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...

  6. Leetcode 3. Longest Substring Without Repeating Characters (Medium)

    Description Given a string, find the length of the longest substring without repeating characters. E ...

  7. [Leetcode Week1]Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...

  8. [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  9. LeetCode[3] Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...

  10. 【leetcode】Longest Substring Without Repeating Characters

    题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

随机推荐

  1. JsonResponse和HttpResponse

    1.联系 JsonResponse继承HttpResponse 2.区别 JsonResponse 数据类型装自动换成json字符串并相应到前端,传到前端的是数据类型而非json字符串 HttpRes ...

  2. 9.JavaSE之运算符

    Java语言支持如下运算符operator:优先级() 算数运算符 :+ ,- ,* ,/ ,% ,++ ,-- 赋值运算符 := 关系运算符 :> ,< ,>= ,<= ,= ...

  3. [bzoj5507] [洛谷P5305] [gzoi2019]旧词

    Descriptioin 浮生有梦三千场 穷尽千里诗酒荒 徒把理想倾倒 不如早还乡 温一壶风尘的酒 独饮往事迢迢 举杯轻思量 泪如潮青丝留他方 --乌糟兽/愚青<旧词> 你已经解决了五个问 ...

  4. set集合迭代

    1.迭代遍历 Set<String> set = new HashSet<String>(); Iterator<String> it = set.iterator ...

  5. 玩转Django2.0---Django笔记建站基础十(二)(常用的Web应用程序)

    10.3 CSRF防护 CSRF(跨站请求伪造)也成为One Click Attack或者Session Riding,通常缩写为CSRF或者XSRF,是一种对网站的恶意利用,窃取网站的用户信息来制作 ...

  6. 深入浅出MyBatis技术原理与实战

    第1 章 MyBatis 简介..................................................................................... ...

  7. Java容器解析系列(13) WeakHashMap详解

    关于WeakHashMap其实没有太多可说的,其与HashMap大致相同,区别就在于: 对每个key的引用方式为弱引用; 关于java4种引用方式,参考java Reference 网上很多说 弱引用 ...

  8. SSM 配置文件 分析

    spring 配置文件(主要整合的是spring 和 mybatis 的配置文件) 问题: 两者之间没有整合在一起的时候是怎么样的 spring配置文件:    Spring配置文件是用于指导Spri ...

  9. java容器(一) Collection类框架图解

  10. CAD制图系列一之绘图、标注、修改、视图

    笔记内容: 缩放.平移.键盘操作 绘图:直线.矩形 修改:删除.修剪.延时 标注:线型.对齐.半径.折弯.直径.角度 知识点 鼠标中键上下滚动 平移:先全部选中,然后点击中间的空格,随便移动 重点:空 ...