题目

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

Example 1:

Input: “abcabcbb”

Output: 3

Explanation: The answer is “abc”, with the length of 3.

Example 2:

Input: “bbbbb”

Output: 1

Explanation: The answer is “b”, with the length of 1.

Example 3:

Input: “pwwkew”

Output: 3

Explanation: 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.

思路——Hash大法好

双指针移动。

Hash表查询字符是否出现。

时间复杂度O(L)O(L)O(L),空间复杂度O(L)O(L)O(L)

结果

Runtime: 27 ms, faster than 70.11% of Java online submissions for Longest Substring Without Repeating Characters.

Memory Usage: 37.5 MB, less than 100.00% of Java online submissions for Longest Substring Without Repeating Characters.

源码

class Solution {
public int lengthOfLongestSubstring(String s) {
Set<Character> set = new HashSet<Character>();
int ans = 0;
int l, r = 0;
Character c;
int len = s.length();
for (l = 0; l < len; ++l) {
while (r < len) {
c = s.charAt(r);
if (set.contains(c)) {
ans = Math.max(ans,r-l);
break;
} else {
set.add(c);
++r;
}
}
if (r == len) {
ans = Math.max(ans, r - l);
break;
}
set.remove(s.charAt(l));
}
return ans;
}
}

LeetCode longest substring without repeating characters 题解 Hash表的更多相关文章

  1. [LeetCode]Longest Substring Without Repeating Characters题解

    Longest Substring Without Repeating Characters: Given a string, find the length of the longest subst ...

  2. C++ leetcode Longest Substring Without Repeating Characters

    要开学了,不开森.键盘声音有点大,担心会吵到舍友.今年要当个可爱的技术宅呀~ 题目:Given a string, find the length of the longest substring w ...

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

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  4. leetcode: longest substring without repeating characters

    July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...

  5. [Leetcode] Longest Substring Without Repeating Characters (C++)

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

  6. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

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

  7. Leetcode:Longest Substring Without Repeating Characters 解题报告

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

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

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  9. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

随机推荐

  1. 面试官:你说你熟悉jvm?那你讲一下并发的可达性分析

    这是why技术的第35篇原创文章 上面这张图是我还是北漂的时候,在鼓楼附近的胡同里面拍的. 那天刚刚下完雨,路过这个地方的时候,一瞬间就被这五颜六色的门板和自行车给吸引了,于是拍下了这张图片.看到这张 ...

  2. Vue中的$Bus使用

    Vue中的$Bus使用 将Bus单独抽离成一个文件 Bus.js import Vue from 'vue'; let Bus = new Vue(); export default Bus; 创建两 ...

  3. java工作流系统表单自动 获取数据

    关键词:工作流快速开发平台  工作流流设计  业务流程管理   asp.net 开源工作流  bpm工作流系统  java工作流主流框架  自定义工作流引擎 表单设计器  流程设计器 什么是数据自动获 ...

  4. ts中的接口

    // 接口:接口是一种定义行为和规范,在程序设计中接口起到限制和规范的作用.接口定义某一 // 一批类所需要遵循的规范,接口不关系这些类的内部实现,之规定这些类必须提供某些方法 /* 1.对批量方法进 ...

  5. js删除对象数组

    若用remove删除某个对象数组,使用for循环遍历数组中的每个对象进行删除,则必须从数组的最后一个元素倒序删除,否则每次删除都只能删除数组的一半元素,因为把索引为0的子节点删除后那么很自然的原来索引 ...

  6. mssql sqlserver 如何将一个日期数据转换为"年份-月份"的格式呢?

    摘要: 下文讲述在sqlserver数据库中,将日期数据转换为指定格式的方法分享,如下所示: 实验环境:sqlserver 2008 R2 实现思路: 实现方法1: 使用year函数和month函数获 ...

  7. git本地创建多个分支互不干扰

    git本地创建多个分支,互不干扰. 情景:在做某个需求a时,先需要修改紧急bug b:发版时发的是远程dev的代码.   方式一(推荐): (1)本地已有分支dev,写了需求a,先commit,即将工 ...

  8. MySQL日志文件和InnoDB引擎文件简介

    MySQL和InnoDB的关系不在这里介绍了.但是大家都知道其中相关的文件很多,类型很多.看文件名就有点分布清楚了.所以在这里简单介绍下他们的文件. 我们直接看文件列表以及在后面直接加注释.做笔记. ...

  9. MySQL和MariaDB安全初始化

    通过yum安装mysql(5.x)后往往需要进行一些安全类的初始化设置: 安装完数据库后执行mysql_secure_installation命令,会出现安全相关的交互界面. 按提示操作.

  10. C语言简单编译预处理-笔记

    预处理指令 所谓预处理,是指,在编译之前对程序源码进行初步的转换,产生新的源代码提供给编译器.预处理时主要完成:包含其他源文件,定义宏,根据条件决定编译时是否包含某些代码(>防止重复包含某些文件 ...