[LeetCode] 3. 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.
解法: 建立一个大小为256整形数组,用来记录每个字符上一次出现的位置。longest记录最长的子串长度,left记录当前子串的起始位置。对于每一个遍历到的字符,如果它曾经出现过(即位置值不为-1)并且left标识在位置值的左方(即left<位置值),则需将left标识移到当前字符之后;否则left无需变化。然后计算当前子串长度,如果大于longest则更新longest。最后记录当前字符的位置值。该算法时间复杂度为O(n),空间复杂度为O(1)。
- public class Solution {
- public int lengthOfLongestSubstring(String s) {
- int[] flags = new int[256]; // 最多有256个字符
- Arrays.fill(flags, -1); // 初始标示为-1
- int longest = 0;
- int left = -1; // 纪录当前最长子串的起始点
- for (int i = 0; i < s.length(); i++) {
- int index = s.charAt(i);
- // 如果当前字符已出现过并且在left的右侧,则将left移动到上次出现的位置之后
- if (flags[index] > left)
- left = flags[index];
- if (longest < (i - left))
- longest = i - left;
- flags[index] = i; // 纪录下当前字符的位置
- }
- return longest;
- }
- }
ps: 如果考虑用队列实现(即将当前字符添加到队列中,如果前面有出现过,则将出现过的字符及其之前的字符全部移除队列),由于每次都需要查找队列中是否有与当前字符相同的字符,增加了不必要的消耗,因此效率较低。
[LeetCode] 3. 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 ...
随机推荐
- 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 ...
- canvas学习(四):高级属性
一:阴影 示例:绘制一个带有阴影的正方形 var canvas = document.getElementById("myCanvas") var ctx = canvas.get ...
- 直接管理内存——new和delete
一.运算符new 1. 使用new动态分配对象 在自由空间分配的内存是无名的,故new无法为其分配的对象命名,而是返回一个指向该对象的指针 int *pi = new int; //pi指向一个动态分 ...
- 初学c#(又要打代码了好难)
因为我原来从没有学过C#,所以要重新看一个语言的基本语法,仔细阅读了老师的作业要求,发现第一个10分的作业如果要用c语言写我是可以完成的,于是定个小目标就是在周日前完成作业的第一步.今天我在菜鸟教程的 ...
- Calculator PartⅢ
GitHub/object-oriented The title of the work 这次敲代码耗时相对较短,但是始终无法完成debug步骤,目前上传的代码可以通过编译,但运行即报停,问题调试为内 ...
- Calculator Part Ⅰ
GitHub/object-oriented The title of the work 关于这次的作业,一开始我是觉得不难的,毕竟学长在已经提供了足够多的提示,实现步骤.需要那些方面的知识等等.但是 ...
- Serialable与Parcelable
Serializable和Parcelable比较 Serializable的作用是为了保存对象的属性到本地文件.数据库.网络流.rmi以方便数据传输,当然这种传输可以是程序内的也可以是 ...
- Android蓝牙开发浅谈(转)
http://www.eoeandroid.com/thread-18993-1-1.html 对于一般的软件开发人员来说,蓝牙是很少用到的,尤其是Android的蓝牙开发,国内的例子很少 A ...
- 如何彻底解决adb 5037端口被占用
在进行安卓开发的时候是不是经常碰到adb端口被占用的情况? 解决这个问题的方法很简单,只需要设置一个系统环境变量就可以搞定. 设置方法: 增加系统环境变量变量名称:ADNROID_ADB_SERVER ...
- windows默认TEMP环境
留着是为了等出问题的时候能找着改回来 Administrator 的用户变量 TEMP %USERPROFILE%\AppData\Local\Temp TMP %USERPRO ...