Longest Substring Without Repeating Characters
来源:互联网 发布:iptables内网端口转发 编辑:程序博客网 时间:2024/06/11 20:16
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.
解题思路:用两个标志位来表示不重复字符串的头尾,每次按一个字符长度搜索时,若当前字符不重复则跟新当前字符串长度,否则将字符串长度与最长不重复字符串长度比较,跟新结果。并根据重复的字符串位置确定字符串的起始位置。
#include<iostream>#include<vector>#include<map>#include<string>using namespace std;int lengthOfLongestSubstring(string s) {int LengthLongestSubs = 0;int LengthTmp = 0;map<char, int>CharMap;int IdxBegin = 0;int IdxEnd = 0;for (; IdxEnd != s.size();++IdxEnd){auto Iter = CharMap.find(s[IdxEnd]);if (Iter == CharMap.end()){CharMap.insert(make_pair(s[IdxEnd], IdxEnd));LengthTmp = IdxEnd + 1 - IdxBegin;//确立不重复字符串的长度}else{LengthLongestSubs = LengthLongestSubs < LengthTmp ? LengthTmp: LengthLongestSubs;int tmp = IdxBegin;IdxBegin = Iter->second+1;//确立新的字符串起始位置for (int i = tmp; i != IdxBegin; ++i)CharMap.erase(s[i]);CharMap.insert(make_pair(s[IdxEnd], IdxEnd));//跟新map}}LengthLongestSubs = LengthLongestSubs < LengthTmp ? LengthTmp : LengthLongestSubs;//避免某些情况,LengthLongestSubs未跟新return LengthLongestSubs;}
0 0
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Linux 2.4.x 网络协议栈QoS模块(TC)的设计与实现
- 记录一个mac相关软件的网站
- google 原生态 抽屉式侧滑菜单 Android DrawerLayout 布局的使用介绍
- empty 与isset区别
- Codeforces Round #288 (Div. 2) C. Anya and Ghosts
- Longest Substring Without Repeating Characters
- 之定义Dialog的宽度
- 以lable实现的checkBox如何切换背景和显示隐藏关联区域
- makefile简介及编写之一
- java与 C++ 之间进行 SOCKET 通讯要点简要解析
- Ubuntu12.04下安装CUDA5.5.22
- Objective-C 代理的例子
- asp.net绝对与相对路径
- CComboBox