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 ...
随机推荐
- springBoot学习(一):初学Thymeleaf
这一部分的代码是基于大神的代码,只是原本的代码是有错的,只自己记录一下自己更改之后的代码和自己的理解. 使用Spring Initzal创建项目,最后代码结构如下,我对Spring及其相关之事还是全然 ...
- Hadoop 安装(本地、伪分布、分布式模式)
本地模式 环境介绍 一共三台测试机 master 192.168.4.91 slave1 192.168.4.45 slave2 192.168.4.96 操作系统配置 1.Centos7 ...
- java创建数组几种方式
最近得多学学基础了,基础还是很重要的- int[] temp=new int[6]; int[] temp={1,2,3,4}; int[] temp= new int[]{1,2,3,4,5}; ...
- ORBSLAM2单目初始化过程
ORBSLAM2单目初始化过程 转自博客:https://blog.csdn.net/zhubaohua_bupt/article/details/78560966 ORB单目模式的初始化过程可以分为 ...
- fidder监控请求响应时间和请求IP
1.增加监控请求的详情时间 在CustomRules.js的class Handlers中增加 //添加请求的响应时间 public static BindUIColumn("Time T ...
- jQuery Ajax calls and the Html.AntiForgeryToken()
jQuery Ajax calls and the Html.AntiForgeryToken() https://stackoverflow.com/a/4074289/3782855 I use ...
- RVS PA-1800 功放参数
RVS PA-1800大功率功放技术参数: 文章来源:外星人来地球 欢迎关注,有问题一起学习欢迎留言.评论
- redis安装与介绍
安装 一般推荐次新版的最后一个发行版.https://redis.io/download 先安装gcc, yum install gcc tar xzf redis-3.2.3.tar.gz cd r ...
- SWT界面刷新
参考文章: https://segmentfault.com/q/1010000002956350 在UI线程sleep等待,是不会刷新界面的. outPrint.setText("其他 ...
- XListView实现上拉加载下拉刷新
package com.loaderman.androiddemo; import android.content.Context; import android.util.AttributeSet; ...