博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode:Longest Substring Without Repeating Characters
阅读量:6234 次
发布时间:2019-06-22

本文共 2504 字,大约阅读时间需要 8 分钟。

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

分析:题意为给一个字符串,找出最长的没有重复字符的子串。

思路:用一个数组来记录目前字符串中出现过的字符(出现过记为1,未出现记为0),start表示满足条件的字符串的起始位置,i每次往前移动一次,遇到一个新的字符元素,判断当前的数组中是否已经存在这个字母,如果存在,移动i指针,直到排除之前出现过的这个元素。

代码:

class Solution {public:   int lengthOfLongestSubstring(string s) {    vector
sign(256, 0); int maxstr=0, start=0; for(int i=0;i

其它解法:

Solution 1(Original): using a set to maintain a window (bounded by l and r) only contains distinct chars. When there is a new character, adds to set, and move r forward; when it's a character that already exists, move l forward right next to the previous inserted character as s[r]. This is my original solution, but may have some overheads when erasing elements from set. Just a different perspective.

class Solution {    public:        int lengthOfLongestSubstring(string s) {            if(s.size()<1) return s.size();            int l=0, r=0, len=1;            unordered_set
window; while(r

Solution 2: The basic idea is, keep a hashmap which stores the characters in string as keys and their positions as values, and keep two pointers which define the potential max substring(window using my word). move the right pointer to scan through the string , and meanwhile update the hashmap. If the character is already in the hashmap, then move the left pointer to the right of the same character last found. Note that the two pointers can only move forward.

To understand l=max(l,window[s[r]]+1) is the key here:

If position of last found char same as s[r] is beyond l, which means the window will have two same chars s[r] now, so we need to move forward l to shrink the window. Otherwise, l stays the same. That's all what says. Your feedback or any thought is welcome. I really learnt a lot from all you genius.

Example 1 "tmmzuxt"

s[l] is the 2nd "m", s[r] is the last "t", and the last found t is not in the current window, no need to update l.

Example 2 "mmzuxtabt"

s[l] is the 2nd m, s[r] is the last "t", and the last found "t" is in the current window, so move lforward to "a". 

class Solution {public:    int lengthOfLongestSubstring(string s) {        int l=0, r=0, len=0;        unordered_map
window; while(r

  

转载于:https://www.cnblogs.com/carsonzhu/p/5282877.html

你可能感兴趣的文章
邮件客户端导入邮件通讯录地址薄
查看>>
java中异常抛出后代码还会继续执行吗
查看>>
oracle 学习摘记
查看>>
IOS代码布局(一) UIView
查看>>
如何解决开机出现Missing operating system的故障
查看>>
Android AudioPolicyService服务启动过程
查看>>
SVG的a链接
查看>>
MSSQL查找前一天,前一月,前一年的数据,对比当前时间记录查找超过一年,一月,一天的数据...
查看>>
基于三星I9250演示自己弄的Miracast功能-手机对手机
查看>>
【转】MOCK测试
查看>>
pyhon——进程线程、与协程基础概述
查看>>
Centos 7配置LAMP
查看>>
重载与重写
查看>>
SQLite学习笔记(十二)&&虚拟机指令
查看>>
UVALive 4221 Walk in the Park 扫描线
查看>>
在vc中使用xtremetoolkit界面库-----安装及环境配置
查看>>
[Redux] Extracting Presentational Components -- Footer, FilterLink
查看>>
将数据写入TXT文件中,file_put_contents与fwrite
查看>>
Win 2008 r2 远程桌面多用户登陆,一用户多登陆配置
查看>>
ANTLR4权威參考手冊(一)
查看>>