Longest Substring Without Repeating Characters
来源:互联网 发布:公务员可以开淘宝店吗 编辑:程序博客网 时间:2024/05/17 09:05
Longest Substring Without Repeating Characters
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.
将元素遍历,并将结果保存在vector里,每次遍历新元素时去vector查找是否存在该元素,如果不存在就将该元素添加,如果存在,查找出存在的元素位置k,并将此时的vector的长度与maxlen作比较,去最大值。然后将vector的头元素到k全部删除掉,并将k添加进vector。
int lengthOfLongestSubstring(string s) { vector<char> ans; int max_len = 0; //记录最大长度 for(int i = 0; i < s.size(); i++){ vector<char>::iterator it = find(ans.begin(), ans.end(), s[i]); if(it != ans.end()){ //如果有重复的元素 max_len = (ans.size() > max_len) ? ans.size() : max_len; ans.erase(ans.begin(), ++it); //删除掉重复的元素及重复元素之前的元素 } ans.push_back(s[i]); //无论重复与否,都要把它添加进去 } //这里需要再做一次判断,因为最后一个子串的if语句不会执行,所以需要在做一次判断。 max_len = (ans.size() > max_len) ? ans.size() : max_len; return max_len; }
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
- hdu-4747-Mex-线段树区域更新
- Scripts:显示数据库中的控制文件dba_controlfiles.sql
- ACM-MST(最小生成树)之Agri-Net——poj1258
- ibatis like查询防sql注入
- 每日算法之十九:Valid Parentheses
- Longest Substring Without Repeating Characters
- iOS中block类型大全
- 01.ViewPager使用Demo
- 2014任重道远的SEO行程
- iOS键盘设置相关
- 铜梁安居古镇建西部第一芦苇荡 打造5A级景区
- 软件项目管理六要素
- 两个顺序栈实现顺序队列功能
- Scripts:查询数据库中参数文件的信息(在重新建库或者克隆数据库时经常使用)dba_cr_init.sql