3. Longest Substring Without Repeating Characters
Medium

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) {
map<char,int> ext;
int id_now = ;
int len = s.length();
int ans = ,cnt = ;
while(id_now<len){
if(ext.find(s[id_now])!=ext.end()){
id_now = ext[s[id_now]]+;
ans = max(ans,cnt);
ext.clear();
cnt = ;
}
else{
ext[s[id_now]] = id_now;
id_now+=;
cnt+=;
}
}
ans = max(ans,cnt);
return ans;
}
};

#include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件
using std::string;
using std::wstring;

using namespace std;

string类的大部分函数:
begin 得到指向字符串开头的Iterator
end 得到指向字符串结尾的Iterator
rbegin 得到指向反向字符串开头的Iterator
rend 得到指向反向字符串结尾的Iterator
size 得到字符串的大小
length 和size函数功能相同
max_size 字符串可能的最大大小
capacity 在不重新分配内存的情况下,字符串可能的大小
empty 判断是否为空
operator[] 取第几个元素,相当于数组
c_str 取得C风格的const char* 字符串
data 取得字符串内容地址
operator= 赋值操作符
reserve 预留空间
swap 交换函数
insert 插入字符
append 追加字符
push_back 追加字符
operator+= += 操作符
erase 删除字符串
clear 清空字符容器中所有内容
resize 重新分配空间
assign 和赋值操作符一样
replace 替代
copy 字符串到空间
find 查找
rfind 反向查找
find_first_of 查找包含子串中的任何字符,返回第一个位置
find_first_not_of 查找不包含子串中的任何字符,返回第一个位置
find_last_of 查找包含子串中的任何字符,返回最后一个位置
find_last_not_of 查找不包含子串中的任何字符,返回最后一个位置
substr 得到字串
compare 比较字符串
operator+ 字符串链接
operator== 判断是否相等
operator!= 判断是否不等于
operator< 判断是否小于
operator>> 从输入流中读入字符串
operator<< 字符串写入输出流
getline 从输入流中读入一行

最新文章

  1. denounce函数:Javascript中如何应对高频触发事件
  2. springMVC 基于注解的controller
  3. eclipse中修改maven仓储
  4. hadoop:could only be replicated to 0 nodes, instead of 1
  5. Android 自定义CheckBox 样式
  6. Inside Microsoft SQL Server 2008: T-SQL Querying 读书笔记1
  7. vue项目构建与实战
  8. Markdown编辑器 简单使用
  9. jquery 导出Excel表格
  10. Linux系统格式化新磁盘并挂载分区
  11. Spring高级装配bean
  12. day 7-16 单表查询
  13. 原创科幻短篇《Bug》
  14. c语言中printf("%x",-1);为什么会输出-1的十六进制补码??
  15. Traefik Kubernetes 初试
  16. sc58x config
  17. QuantLib 金融计算
  18. java并发之原子性、可见性、有序性
  19. 如何在ASP.NET Core 2.0中使用Razor页面
  20. windows phone 8.1 让项目开启蓝牙genericAttributeProfile

热门文章

  1. Lesson 4 The double life of Alfred Bloggs
  2. BindWeb - Bind智能DNS管理系统介绍
  3. 最大连续和 Easy
  4. 手写spring事务框架, 揭秘AOP实现原理。
  5. B/S,C/S架构的区别
  6. MFC- 网络编程
  7. maven多模块tomcat启动报 NoClassDefFoundError:com/test/main/message
  8. Ajax加载数据后百度分享实例
  9. lmhosts - samba的NetBIOS主机列表文件
  10. 为docker配置国内镜像加速器