Leetcode03---Longest Substring Without Repeating Characters
Description:
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.
给定一个字符串,求字符串中最长不重复的字串长度。
又是一道没有数据范围的题目,这里字符串长可以超过3w,所以暴力不可搞。最后用很巧妙的运用起点和重点位置的 顺序选择,和对字符先后出现位置的统计,来求得结果。
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int maxLen = 0, start = -1;
map<char, int>mapChar;
for (int i = 0; s[i]; i ++) {
if (mapChar.count(s[i])) {//这里学了一招,map的count方法可以统计对应类型出现的次数,不用再dt的初始化了
start = max(start, mapChar[s[i]]);
}
mapChar[s[i]] = i;
maxLen = max(maxLen, i - start);
}
return maxLen;
}
};
Leetcode03---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 examp ...
- 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 ...
- Longest Substring Without Repeating Characters (c#)
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- HDU 1176 DP
题目大意: 在0~10这11个点上面接饼 , 每秒最多往左或往移动一格,或者保持原地不动 令dp[i][j]表示在第 i 秒在 第 j 个点上最多能得到的饼的数量 dp[i][j] = max(dp[ ...
- [Usaco2015 dec]Max Flow
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 204 Solved: 129[Submit][Status][Discuss] Descriptio ...
- B. Code For 1 分治
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- Power of Matrix 等比数列求和 矩阵版!
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...
- 听SEO大神夜息分享
今天偶然听说了百度站长平台,又偶然在上面发现了夜息大神的分享(http://zhanzhang.baidu.com/college/videoinfo?id=871). 之前对于SEO的了解只限于减少 ...
- Linux运行级别研究(转)
Linux系统中的运行级别 7种运行级别 运行级别(Runlevel)指的是Unix或者Linux等类Unix操作系统的运行模式,不同的运行模式下系统的功能也有所有不同.Linux 系统下通常分为7种 ...
- Spring MVC集成thymeleaf时提示:defined in ServletContext resource [/WEB-INF/SrpingMVCTest-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException
错误提示: defined in ServletContext resource [/WEB-INF/SrpingMVCTest-servlet.xml]: Instantiation of bean ...
- 安全简单解决MVC 提示 检测到有潜在危险的 Request.Form 值.
一般使用富文本编辑器的时候.提交的表单中包含HTML字符,就会出现此错误提示. 使用 ValidateInput(false) 特性标签并不能解决此问题. 网上前篇一律的回答是修改Web.Config ...
- nginx: 添加文件下载目录
修改nginx.conf,添加如下行: location /file/ { alias /usr/share/nginx/html/file/; add_header Content-di ...
- [Javascript] JavaScript赋值时的传值与传址
JavaScript中有两种不同数据类型的值,分别是基本数据类型与引用数据类型 基本数据类型包含5类,分别是:Number.String.Boolean.Null.Undefined 引用数据类型包含 ...