https://leetcode.com/problems/longest-substring-without-repeating-characters/

思路:从某点结束所能取到的最早开头是到目前出现倒数第二次的字符的最末位置后一位

如图:(S代表起点,T代表终点)

abcabcab

**S*T

假设现在在4位,向前找,a出现的倒数第二次在0位,b出现的在1位,所以可以从2位开始取

class Solution {
public:
int lengthOfLongestSubstring(string s) {
int ans = 0;
typedef pair<int,int> P;
priority_queue<P> que;
int vis[256];memset(vis,-1,sizeof vis);
int used[256];memset(used,-1,sizeof used);
for(int i = 0;i < s.length();i++){
used[s[i]] = vis[s[i]];
vis[s[i]] = i;
que.push(P(used[s[i]],s[i]));
while(!que.empty() && used[que.top().second]!= que.top().first)que.pop();
if(!que.empty())ans = max(ans,i - que.top().first);
else {ans = max(ans,i + 1);}
}
return ans;
}
};

  

LeetCode 3 Longest Substring Without Repeating Characters 区间,想法 难度:1的更多相关文章

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

随机推荐

  1. 如果你恨一个程序员 忽悠他去做iOS开发(戏谑篇)

    .state { text-align: right; height: 16px; line-height: 16px; color: #999; padding-top: 5px; overflow ...

  2. 【转】CSS:table-cell详解

    table-cell这个家伙在国外的网站中偶有露头,天朝由于IE6.7这两个货泛滥成灾,难有发挥,那么,这个家伙到底能干些什么呢?先让我们来研究下table,那些年曾经使用的table布局为何如此辉煌 ...

  3. 安装composer

    按照composer官网的指导,运行下列命令:curl -sS https://getcomposer.org/installer | php长时间无反应.手动安装1.下载installer# wge ...

  4. js设计模式总结-单例模式

    单例模式 解决的问题 保证实例只有一个,避免多个实现,从全局来看,这个实例的状态是唯一的. 实现原理 设置一个变量来记录实例,通过检测该变量是否为空来决定是否创建实例 非透明单例 所谓非透明就是用户在 ...

  5. 使用时间戳和sequence生成主键的function

    create or replace function fn_getKeyreturn varchar2is  k varchar2(30);begin  select to_char(sysdate, ...

  6. 关闭dialog(lhgdialog)

    W.$.dialog({id:'dyj'}).close(); 成功 $("#id").dialog('close');frameElement.api.close();

  7. android 画虚线、实线,画圆角矩形,一半圆角

    1.画虚线,实线: 建立dotted_line_gray.xml文件放在drawable文件夹下面. android:shape="line" 可以修改你想要的形状 <?xm ...

  8. 撤销git reset soft head操作

    一不小心在eclipse的git库中执行了Reset Soft(HEAD ONLY)操作,不料界面中竟然没有找到撤销方法(于是心中五味俱全,经过一番折腾,无果还是回归Git本身),最终通过命令行,很快 ...

  9. PackageManager源码分析

    在android 4.4源码上进行的分析. 一.PackageManager如何产生的? 我们平时在代码中使用的context.getPackageManager() 那么这个PackageManag ...

  10. laraver mongo 查询操作

    1,mongo 不支持特殊where条件(&,|) 2,mongo 可以连接mysql的表查询,但不支持连表的where查询