题目

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.

翻译

给定一个字符串,找到最长的子串的长度,这个子串不存在重复的字符。
例如:
输入”abcabcbb”, 符合条件的子串就是”abc”, 长度为3。

输入”bbbbb”, 符合条件的子串就是”b”, 长度为1

输入”pwwkew”, 符合条件的子串就是”wke”, 长度为3。提示:要求必须是子串,例如”pwke” 是一个子序列但不是一个子串。

分析

遍历一遍,记录符合条件的长度count。如果遇到相同的字符,则从左往右remove直到移除掉那个相同的字符。

Java版代码(时间复杂度O(n),空间复杂度O(n)):

public class Solution {public int lengthOfLongestSubstring(String s) {Map<Character, Integer> map = new HashMap<Character, Integer>();char[] charArr = s.toCharArray();int max = 0;int count = 0;for (int i = 0; i < charArr.length; i++) {Integer last = map.get(charArr[i]);if (last == null) {count++;if (count > max) {max = count;}} else {int start=i-count;for(int j=start;j<last;j++){map.remove(charArr[j]);count--;}}map.put(charArr[i], i);}return max;}
}

Leet Code OJ 3. Longest Substring Without Repeating Characters相关推荐

  1. [LeetCode]3.Longest Substring Without Repeating Characters

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

  2. leetcode(三)—— Longest Substring Without Repeating Characters(最长不重复子串 Python/C++)

    Longest Substring Without Repeating Characters | LeetCode OJ 使用 hash 判重问题首先想到的就是 hash(或者使用 map): 思路: ...

  3. LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)

    这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...

  4. 3.longest substring without repeating characters

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

  5. Longest Substring Without Repeating Characters(最长不重复子序列求解)

    问题描述: Given a string, find the length of the longest substring without repeating characters. Example ...

  6. [LeetCode] Longest Substring Without Repeating Characters

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

  7. LeetCode——Longest Substring Without Repeating Characters

    原问题 Given a string, find the length of the longest substring without repeating characters. Example 1 ...

  8. LeetCode:3. Longest Substring Without Repeating Characters

    https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ 内容描述: Give ...

  9. 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters

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

最新文章

  1. PS网页设计教程——30个优秀的PS网页设计教程的中文翻译教程
  2. nginx loaction
  3. .NET开发者省份分布排名
  4. jQuery的ajax提交表单
  5. mysql优化笔记,MySQL优化笔记
  6. 认真去做,我会做得很棒!
  7. DuiLib(一)——窗口及消息
  8. Windows Phone 7调用必应翻译服务
  9. 在win10 python3用pyhive连接hive
  10. 经典laravel后台管理系统
  11. sinkhorn algorithm
  12. 近两年最快上市房企,祥生控股究竟是在控制负债,还是饮鸩止渴?
  13. 瑞典计算机最好的大学排名,瑞典前十大学一览表
  14. php strtotime 2038 时间戳,PHP的strtotime计算2038年以上日期的时间戳错误
  15. 翌加:抖音小店可以绑定几个抖音号
  16. iOS_导航栏的navigationBar.hidden与navigationBarHidden的区别
  17. 梅森公式的推导和探究
  18. Ranger 简介与安装
  19. 别再说你不知道分布式事务了
  20. 国际亚马逊API 接口,获得AMAZON商品详情

热门文章

  1. android软件的data使用方法,实例讲解Android中SQLiteDatabase使用方法
  2. AJAX DELETE
  3. 贝叶斯学习及共轭先验
  4. 线段树求区间最大值RMQ(单点更新)
  5. Vi(Linux系统下的标准编辑器)学习笔记
  6. C++虚继承(一) --- vtordisp字段
  7. Gh0st源码学习(一)前期准备工作
  8. Linux下的基本指令
  9. MongoDB索引原理和具体使用
  10. Java多线程知识小抄集(一)