【Leetcode 3】Longest Substring Without Repeating Characters0
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;
}
};
【Leetcode 3】Longest Substring Without Repeating Characters0的更多相关文章
- 【LeetCode OJ】Longest Substring Without Repeating Characters
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...
- 【LeetCode】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 exa ...
- 【leetcode】Longest Substring Without Repeating Characters (middle)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...
- 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)
Given a string, find the length of the longest substring without repeating characters. Example 1: ...
- 【Leetcode】【Medium】Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【leetcode刷题笔记】Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- leetcode题解 3. Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
随机推荐
- Spring security 5 Authorize Configuration
1. Spring Security 核心请求,认证配置类 WebSecurityConfigurerAdapter protected void configure(HttpSecurity htt ...
- 升级 HTTPS,价值何在?
HTTPS 实质上是一种面向安全信息通信的协议.从最终的数据解析的角度上看,HTTPS 与 HTTP 没有本质上的区别.对于接收端而言,SSL/TSL 将接收的数据包解密,将数据传给 HTTP 协议层 ...
- 根据判断数组不为空然后取他的值----数组不会为空---只能判断其size是否大于0
private List<Integer> classId=new ArrayList<Integer>(); business.getClassId()!=null 以上为错 ...
- BNUOJ 13358 Binary Apple Tree
Binary Apple Tree Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Orig ...
- 55. spring boot 服务配置和部署【从零开始学Spring Boot】
Spring Boot 其默认是集成web容器的,启动方式由像普通Java程序一样,main函数入口启动.其内置Tomcat容器或Jetty容器,具体由配置来决定(默认Tomcat).当然你也可以将项 ...
- 【MongoDB】2、安装MongoDB 2.6.1 on Unbuntu 14.04(学习流水账)
http://blog.csdn.net/stationxp/article/details/26077439 计划: 装一个虚机,ubuntu吧,14.04 Trusty Tahr. 安装Mongo ...
- Cx的治疗
题目背景 「Cx的故事」众所周知,Cx是一个宇宙大犇.由于Cx在空中花园失足摔下,导致他那蕴含着无穷智慧的大脑受到了严重的损伤,许多的脑神经断裂.于是,Cx的wife(有么?)决定请巴比伦最好的医师治 ...
- 28、Java并发性和多线程-剖析同步器
以下内容转自http://ifeve.com/anatomy-of-a-synchronizer/: 虽然许多同步器(如锁,信号量,阻塞队列等)功能上各不相同,但它们的内部设计上却差别不大.换句话说, ...
- gh-ost: triggerless online schema migrations:Blog by Shlomi Noach:
http://code.openark.org/blog/category/mysql https://rj03hou.github.io/mysql/gh-ost/
- Mybatis在Spring环境下的启动顺序
主要看三个类: mybatis-spring-1.2.2.jar包 -> org.mybatis.spring.SqlSessionFactoryBean mybatis-3.2.6.jar包 ...