leetcode--js--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.
问题思路:
(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的更多相关文章
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- [LeetCode][Python]Longest Substring Without Repeating Characters
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- LeetCode之Longest Substring Without Repeating Characters
[题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...
- Leetcode 3. Longest Substring Without Repeating Characters (Medium)
Description Given a string, find the length of the longest substring without repeating characters. E ...
- [Leetcode Week1]Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
随机推荐
- java 储存机制
1.栈 statck 局部变量名称 2.堆 heap 带new的 3.方法区 method area .class
- python 面向对象的内置方法
要求:了解即可,能用最好 """ 1.print(obj), str(obj), %s % (obj), 都调用obj.__str__()方法,若类中没有找__repr_ ...
- python 快速创建字典 fromkes()
作用:快速创建字典 特点:共用value seq = ['google', 'ie', 'firefox'] # seq为可迭代对象(str, list, tuple, dict, set) dic ...
- CCPC-Wannafly Winter Camp Day 1
B. 密码学 题意: 告诉你关于字符串加密的方法,然后给你一些加密操作和加密后的字符串,让你求原来的串 思路: 知道被加密后的串与加密字符可以向前推出被加密之前的串,不断向前模拟即可 #include ...
- 【javaScript】js出现allocation size overflow以及字符串拼接优化
字符串拼接长一点了,就出现了allocation size overflow异常! 先创建缓冲字符串数组,最后将数组转化为字符串 <script type="text/javascri ...
- P1553 数字反转(升级版)
题目描述 给定一个数,请将该数各个位上数字反转得到一个新数. 这次与NOIp2011普及组第一题不同的是:这个数可以是小数,分数,百分数,整数.整数反转是将所有数位对调:小数反转是把整数部分的数反转, ...
- 机器学习环境配置系列五之keras2
keras一个大坑就是配置文件的问题,网上会给很多的误导,让我走了很多弯路. 1.安装keras2 conda install keras 2.环境配置 echo ‘{ "epsilon&q ...
- NOI2.5 1490:A Knight's Journey
描述 Background The knight is getting bored of seeing the same black and white squares again and again ...
- Vmware上安装Linux(centos7)图文教程
Vmware上安装Linux(centos7)图文教程 一.准备安装文件(vmware && centos7 镜像) 1.下载 vmware workstations :链接: ...
- 工具之awk
转自:http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html awk是一个强大的文本分析工具,相对于grep的查找,sed的编 ...