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. 第二,C语言示例

    #include<stdio.h> int main (void)                 /*WTF*/ { int num; num=1; printf(" I am ...

  2. 关于欧几里得算法求最大公约数,即OJ1029的参考解法

    #include <stdio.h> int main(int argc, char *argv[]) { int a,b,c; scanf("%d %d",& ...

  3. Cheatsheet: 2016 11.01 ~ 11.30

    Web Getting Started With Vapor: A Swift Web Framework Front-end vs Back-end vs Network Performance S ...

  4. Cheatsheet: 2016 06.01 ~ 6.30

    Other Swift for the Java guy: Part 1 – Getting Started Building a better code review process Creatin ...

  5. Java开发中经典的小实例-(swich(){case:参数break;default: break;})

    import java.util.Scanner;public class Test6 {    public static void main(String[] args) {        // ...

  6. (转载)MongoingDB常用操作

    mongo –path db.AddUser(username,password)  添加用户 db.auth(usrename,password)     设置数据库连接验证 db.cloneDat ...

  7. hdu 4920 Matrix multiplication bitset优化常数

    Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  8. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) E. Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  9. mac上的git环境配置

    git生成ssh key for mac 打开终端 cd ~/.ssh 看有没有文件 没有的时候 有有ssh key是这样的多了id_rsa(私钥)和 id_rsa.pub(公钥) 没有的话创建 ss ...

  10. Google Maps地图投影全解析(3):WKT形式表示

    update20090601:EPSG对该投影的编号设定为EPSG:3857,对应的WKT也发生了变化,下文不再修改,相对来说格式都是那样,可以到http://www.epsg-registry.or ...