LeetCode 3 Longest Substring Without Repeating Characters 区间,想法 难度:1
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的更多相关文章
- 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 ...
随机推荐
- 第二,C语言示例
#include<stdio.h> int main (void) /*WTF*/ { int num; num=1; printf(" I am ...
- 关于欧几里得算法求最大公约数,即OJ1029的参考解法
#include <stdio.h> int main(int argc, char *argv[]) { int a,b,c; scanf("%d %d",& ...
- Cheatsheet: 2016 11.01 ~ 11.30
Web Getting Started With Vapor: A Swift Web Framework Front-end vs Back-end vs Network Performance S ...
- Cheatsheet: 2016 06.01 ~ 6.30
Other Swift for the Java guy: Part 1 – Getting Started Building a better code review process Creatin ...
- Java开发中经典的小实例-(swich(){case:参数break;default: break;})
import java.util.Scanner;public class Test6 { public static void main(String[] args) { // ...
- (转载)MongoingDB常用操作
mongo –path db.AddUser(username,password) 添加用户 db.auth(usrename,password) 设置数据库连接验证 db.cloneDat ...
- hdu 4920 Matrix multiplication bitset优化常数
Matrix multiplication Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...
- 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 ...
- mac上的git环境配置
git生成ssh key for mac 打开终端 cd ~/.ssh 看有没有文件 没有的时候 有有ssh key是这样的多了id_rsa(私钥)和 id_rsa.pub(公钥) 没有的话创建 ss ...
- Google Maps地图投影全解析(3):WKT形式表示
update20090601:EPSG对该投影的编号设定为EPSG:3857,对应的WKT也发生了变化,下文不再修改,相对来说格式都是那样,可以到http://www.epsg-registry.or ...