[3, Medium, C++] Longest Substring Without Repeating Characters
2016-01-10 05:55
429 查看
Problem:
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.
Analysis:
To solve this question, the program first caches a set of distinct characters of the string and runs from the first one to the
last one to check whether a repeated character of the set is met. If such character is met, remove all ones before the repeated character. The key point of this method is to locate the repeated one in the quickest way.The method here is a typical one.
We use a characteristic array whose index is the ascii value of a character and value is the index this character. For each character of the string, if the corresponding characteristic value of this character is greater or equal of the current sub-string,
it means that a repeated character is found. Then, update the local_max_length of the sub-string and new start-checking-point.
Sample Code:
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.
Analysis:
To solve this question, the program first caches a set of distinct characters of the string and runs from the first one to the
last one to check whether a repeated character of the set is met. If such character is met, remove all ones before the repeated character. The key point of this method is to locate the repeated one in the quickest way.The method here is a typical one.
We use a characteristic array whose index is the ascii value of a character and value is the index this character. For each character of the string, if the corresponding characteristic value of this character is greater or equal of the current sub-string,
it means that a repeated character is found. Then, update the local_max_length of the sub-string and new start-checking-point.
Sample Code:
int lengthOfLongestSubstring(string s) { if(s.size() <= 1) return s.size(); int max_length = 0; vector<int> char_seq_index(256, -1); int start_point = 0; int local_max_length = 0; for(int i = 0; i < s.size(); ++i) { if(char_seq_index[s[i]] >= start_point) { local_max_length -= char_seq_index[s[i]] + 1 - start_point; start_point = char_seq_index[s[i]] + 1; } char_seq_index[s[i]] = i; ++local_max_length; if(local_max_length > max_length) max_length = local_max_length; } return max_length; }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- 关于指针的一些事情
- android上改变listView的选中颜色
- String.intern
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Prototype源码浅析 String部分(二)
- Ruby中的String对象学习笔记
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- PostgreSQL ERROR: invalid escape string 解决办法
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析