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

Examples:

  1. Given "abcabcbb", the answer is "abc", which the length is 3.
  2.  
  3. Given "bbbbb", the answer is "b", with the length of 1.
  4.  
  5. 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.

解法: 建立一个大小为256整形数组,用来记录每个字符上一次出现的位置。longest记录最长的子串长度,left记录当前子串的起始位置。对于每一个遍历到的字符,如果它曾经出现过(即位置值不为-1)并且left标识在位置值的左方(即left<位置值),则需将left标识移到当前字符之后;否则left无需变化。然后计算当前子串长度,如果大于longest则更新longest。最后记录当前字符的位置值。该算法时间复杂度为O(n),空间复杂度为O(1)。

  1. public class Solution {
  2. public int lengthOfLongestSubstring(String s) {
  3. int[] flags = new int[256]; // 最多有256个字符
  4. Arrays.fill(flags, -1); // 初始标示为-1
  5. int longest = 0;
  6. int left = -1; // 纪录当前最长子串的起始点
  7. for (int i = 0; i < s.length(); i++) {
  8. int index = s.charAt(i);
  9. // 如果当前字符已出现过并且在left的右侧,则将left移动到上次出现的位置之后
  10. if (flags[index] > left)
  11. left = flags[index];
  12. if (longest < (i - left))
  13. longest = i - left;
  14. flags[index] = i; // 纪录下当前字符的位置
  15. }
  16. return longest;
  17. }
  18. }

ps: 如果考虑用队列实现(即将当前字符添加到队列中,如果前面有出现过,则将出现过的字符及其之前的字符全部移除队列),由于每次都需要查找队列中是否有与当前字符相同的字符,增加了不必要的消耗,因此效率较低。

[LeetCode] 3. 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. LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告

    1.题目大意 Given an array of integers that is already sorted in ascending order, find two numbers such t ...

  2. canvas学习(四):高级属性

    一:阴影 示例:绘制一个带有阴影的正方形 var canvas = document.getElementById("myCanvas") var ctx = canvas.get ...

  3. 直接管理内存——new和delete

    一.运算符new 1. 使用new动态分配对象 在自由空间分配的内存是无名的,故new无法为其分配的对象命名,而是返回一个指向该对象的指针 int *pi = new int; //pi指向一个动态分 ...

  4. 初学c#(又要打代码了好难)

    因为我原来从没有学过C#,所以要重新看一个语言的基本语法,仔细阅读了老师的作业要求,发现第一个10分的作业如果要用c语言写我是可以完成的,于是定个小目标就是在周日前完成作业的第一步.今天我在菜鸟教程的 ...

  5. Calculator PartⅢ

    GitHub/object-oriented The title of the work 这次敲代码耗时相对较短,但是始终无法完成debug步骤,目前上传的代码可以通过编译,但运行即报停,问题调试为内 ...

  6. Calculator Part Ⅰ

    GitHub/object-oriented The title of the work 关于这次的作业,一开始我是觉得不难的,毕竟学长在已经提供了足够多的提示,实现步骤.需要那些方面的知识等等.但是 ...

  7. Serialable与Parcelable

    Serializable和Parcelable比较        Serializable的作用是为了保存对象的属性到本地文件.数据库.网络流.rmi以方便数据传输,当然这种传输可以是程序内的也可以是 ...

  8. Android蓝牙开发浅谈(转)

    http://www.eoeandroid.com/thread-18993-1-1.html 对于一般的软件开发人员来说,蓝牙是很少用到的,尤其是Android的蓝牙开发,国内的例子很少     A ...

  9. 如何彻底解决adb 5037端口被占用

    在进行安卓开发的时候是不是经常碰到adb端口被占用的情况? 解决这个问题的方法很简单,只需要设置一个系统环境变量就可以搞定. 设置方法: 增加系统环境变量变量名称:ADNROID_ADB_SERVER ...

  10. windows默认TEMP环境

    留着是为了等出问题的时候能找着改回来 Administrator 的用户变量 TEMP     %USERPROFILE%\AppData\Local\Temp TMP        %USERPRO ...