LC 3. Longest Substring Without Repeating Characters
题目描述
Given a string, find the length of the longest substring without repeating characters.
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is"abc", with the length of 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is"b", with the length of 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: 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.
参考答案
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_map<char,int> map;
int res = ;
int start = -; // 存储开始的位置,如果完全没有重复的字符,那么开始位置永远是-1
for(int i = ;i<s.length();i++){
if(map.find(s[i]) != map.end()){
start = max(start,map[s[i]]);
// 如果通过find 找到了一个重复的
// 那么就把start给替换成 上一个重复字符的index
}
map[s[i]] = i; // map[char] = index;
res = max(res,i-start); // 获得最大值
}
return res;
}
};
Given a string, find the length of the longest substring without repeating characters.
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is"abc", with the length of 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is"b", with the length of 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: 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.
LC 3. Longest Substring Without Repeating Characters的更多相关文章
- LN : leetcode 3 Longest Substring Without Repeating Characters
lc 3 Longest Substring Without Repeating Characters 3 Longest Substring Without Repeating Characters ...
- 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 example, ...
- Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 3. Longest Substring Without Repeating Characters(c++) 15ms
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
- Longest Substring Without Repeating Characters(C语言实现)
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- leetcode: longest substring without repeating characters
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- [LeetCode_3] Longest Substring Without Repeating Characters
LeetCode: 3. Longest Substring Without Repeating Characters class Solution { public: int lengthOfLon ...
随机推荐
- 用Python匹配HTML tag的时候,<.>和<.?>有什么区别?
答:术语叫贪婪匹配( <.> )和非贪婪匹配(<.?> ) 例如: test <.*> : test <.*?> :
- scrapy框架自定制命令
写好自己的爬虫项目之后,可以自己定制爬虫运行的命令. 一.单爬虫 在项目的根目录下新建一个py文件,如命名为start.py,写入如下代码: from scrapy.cmdline import ex ...
- 《Glibc内存管理》笔记DAY3
目录 边界标记法 内容来源 边界标记法 /* conversion from malloc headers to user pointers, and back */ #define chunk2me ...
- Go --- 七牛云 上传文件 & Token demo
package main import ( "bytes" "crypto/hmac" "crypto/sha1" "encodi ...
- arcgis 地理坐标系 699个,投影坐标系是4767
import arcpy # Get the list of spatial references and print it. srs = arcpy.ListSpatialReferences(sp ...
- 服务挂后Dump日志
JAVA_HOME=/usr/java OUTPUT_HOME=~/output DEPLOY_HOME=`dirname $0` HOST_NAME=`hostname` DUMP_PIDS=`ps ...
- Qt代码配色VS2015风格
通过本文的方法可以将VS2015的深色主题界面应用到Qt上,对于喜欢VS代码风格配色的人应该会比较有用 效果图: 1. 设置IDE主题 为了配合vs深色的代码编辑背景,将Qt的主题也换成深色版本 2 ...
- python代码-leetcode1 两数相加
1.两个循环 class Solution: def twoSum(self, nums, target): n=len(nums) for i in range(n): for j in range ...
- 解决一个 MySQL 服务器进程 CPU 占用 100%解决一个 MySQL 服务器进程 CPU 占用 100%的技术笔记》[转]
转载地址:http://bbs.chinaunix.net/archiver/tid-1823500.html 解决一个 MySQL 服务器进程 CPU 占用 100%解决一个 MySQL 服务器进程 ...
- VCTravel
#pragma once #include <osgViewer/Viewer> #include <osgViewer/ViewerEventHandlers> #inclu ...