面试题(7)之 leetcode-003
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
- 示例1:
- 输入: "abcabcbb"
- 输出: 3
- 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
- 示例2:
- 输入: "bbbbb"
- 输出: 1
- 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
- 示例3:
- 输入: "pwwkew"
- 输出: 3
- 解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
- 请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
- /**
- * @param {string} s
- * @return {number}
- */
- var lengthOfLongestSubstring = function (s) {
- let string = ''; // 定义一个空字符串存放无重复子串,只是一个中间过程量,不会真正存储最长的无重复子串
- let len = 0; // 定义存放无重复子串的长度
- for(let i = 0; i < s.length; i++) {
- const singleString = s.charAt(i);
- const singleIndex = string.indexOf(singleString);
- if(singleIndex === -1) {
- string += singleString
- len = len < string.length ? string.length : len
- } else {
- string = string.substr(singleIndex + 1) + singleString
- }
- }
- return len
- };
- const string = "wkqweqwerasdfxcvrsdfswkew"
- let a = lengthOfLongestSubstring(string)
- console.log(a);
面试题(7)之 leetcode-003的更多相关文章
- LeetCode #003# Longest Substring Without Repeating Characters(js描述)
索引 思路1:分治策略 思路2:Brute Force - O(n^3) 思路3:动态规划? O(n^2)版,错解之一:420 ms O(n^2)版,错解之二:388 ms O(n)版,思路转变: 1 ...
- [Leetcode]003. Longest Substring Without Repeating Characters
https://leetcode.com/problems/longest-substring-without-repeating-characters/ public class Solution ...
- leetcode 003
3. Longest Substring Repeating Character Difficulty:Medium The link: https://leetcode.com/problems/l ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [leetCode][003] Intersection of Two Linked Lists
[题目]: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- Longest Substring Without Repeating Characters ---- LeetCode 003
Given a string, find the length of the longest substring without repeating characters. For example, ...
- C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介
目录 为什么要刷LeetCode 刷LeetCode有哪些好处? LeetCode vs 传统的 OJ LeetCode刷题时的心态建设 C#如何刷遍LeetCode 选项1: VS本地Debug + ...
- LeetCode 要记得一些小trick
最近搞了几场编程比赛,面试题或者是LeetCode周赛.每次都不能做完,发现时间不够用. 看了别人的代码才知道,同样实现相同的功能,可能别人只需要用一个恰当的函数,就会比自己少些不少代码,争得了时间. ...
- leetcode 刷500道题,笔试/面试稳过吗?谈一谈这些年来算法的学习
想要学习算法.应付笔试或者应付面试手撕算法题,相信大部分人都会去刷 Leetcode,有读者问?如果我在 leetcode 坚持刷它个 500 道题,以后笔试/面试稳吗? 这里我说下我的个人看法,我认 ...
- 使用 JS 输出螺旋矩阵
关于螺旋矩阵 这是我曾经遇到过的面试题,在 LeetCode 上找到了题目的原型,难度中等.题目描述如下: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中 ...
随机推荐
- C++编程学习(十二) STL
一.简介 标准模板库STL,是一组模板类和函数.提供了: 1.容器.用于存储信息. 2.迭代器.用于访问容器中的信息. 3.算法.操作容器内容. 1.容器 STL有两种类型的容器类: (1)顺序容器 ...
- python里的def 方法中->代表什么意思?
功能注释 函数注释是关于用户定义函数使用的类型的完全可选元数据信息(请参阅PEP 3107和 PEP 484了解更多信息). 注释__annotations__ 作为字典存储在函数的属性中,对函数的任 ...
- string.xml中的空格
<string name="userName"> 用 户 名</string>
- mysql导入导出无权限
error:The MySQL server is running with the --secure-file-priv option so it cannot execute this state ...
- python用于web题里写解密脚本
题源自bugku里的WEB3 选择选项让他停止,F12后出现如下代码,一看数字就知道是ASC: 复制出来,写pyhton脚本如下,在编译器里跑一下 s='KEY{J2sa42ahJK-HS11III} ...
- vue-cli 局域网可访问配置
vue-cli 使用官方脚手架搭建以后,本地的config配置已经没有了,默认打开localhost,无法ip访问 只要修改项目目录配置和防火墙配置 1.在项目根目录下面加一个文件vue.config ...
- P1044 火星数字
P1044 火星数字 转跳点:
- 20170305深圳Meetup Rails中CSS,JS引用关系分析
新手上路,若有错误请及时提醒 Rails中CSS,JS引用关系分析 一.Rails静态文件存放位置 二.Rails中CSS引用方式 三.Rails中JS引用方式与CSS类似 四.上面都是默认引用app ...
- c++输出哈夫曼编码
#include<iostream> #include<string> using namespace std; struct huffTree { int parent; i ...
- 官网英文版学习——RabbitMQ学习笔记(二)RabbitMQ安装
一.安装RabbitMQ的依赖Erlang 要进行RabbitMQ学习,首先需要进行RabbitMQ服务的安装,安装我们可以根据官网指导进行http://www.rabbitmq.com/downlo ...