LeetCode OJ ---- Longest Substring Without Repeating Characters
来源:互联网 发布:vb picturebox 清空 编辑:程序博客网 时间:2024/06/06 07:48
题目描述
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.
code(c++)
/*法一:*/class Solution {public: int lengthOfLongestSubstring(string s) { unordered_map<char, int> subString; int max_subLength = 0; int start = 0; int length; for (int i = 0; i < s.size(); ++i){ unordered_map<char, int>::iterator it = subString.find(s[i]); if (it != subString.end()){ if (it->second >= start) start = it->second + 1; it->second = i; } else subString.insert(make_pair(s[i], i)); length = i - start + 1; max_subLength = max_subLength > length ? max_subLength : length; } return max_subLength; }};/*法二:*/class Solution {public: int lengthOfLongestSubstring(string s) { const size_t letterNum = 95; //ASCII可打印字符个数 int subString[letterNum]; //记录对应元素在s中的位置 fill(subString, subString+letterNum, -1); size_t start = 0; size_t maxLength = 0; size_t length; for(size_t i = 0; i < s.size(); ++i){ if(subString[s[i] - ' '] != -1 && subString[s[i] - ' '] >= start) start = subString[s[i] - ' '] + 1; subString[s[i] - ' '] = i; length = subString[s[i] - ' '] - start + 1; maxLength = maxLength > length ? maxLength : length; } return maxLength; }};
note:
- 对于方法二,需要知道ASCII中可打印字符的个数以及第一个可打印字符。ASCII共有128个字符,其中可打印字符有95个,控制字符有33个,可打印字符中第一个元素为空格,其ASCII值为32
0 0
- LeetCode OJ:Longest Substring Without Repeating Characters
- LeetCode OJ Longest Substring Without Repeating Characters
- LeetCode OJ ---- Longest Substring Without Repeating Characters
- [LeetCode OJ]Longest Substring Without Repeating Characters
- LeetCode OJ - Longest Substring Without Repeating Characters (-2*)
- [LeetCode OJ][003]Longest Substring Without Repeating Characters
- Leetcode OJ #3 Longest Substring Without Repeating Characters
- LeetCode OJ 之 Longest Substring Without Repeating Characters 解题报告
- LeetCode OJ 3 Longest Substring Without Repeating Characters 小结
- LeetCode OJ 03 Longest Substring Without Repeating Characters
- 【LeetCode OJ 003】Longest Substring Without Repeating Characters
- LeetCode OJ : 3 Longest Substring Without Repeating Characters
- <LeetCode OJ> 3. Longest Substring Without Repeating Characters
- LeetCode OJ 3.Longest Substring Without Repeating Characters
- LeetCode OJ 3. Longest Substring Without Repeating Characters
- LeetCode OJ(3.Longest Substring Without Repeating Characters)
- 3. Longest Substring Without Repeating Characters——LeetCode OJ
- LeetCode: Longest Substring Without Repeating Characters
- struts2中的jsp值传到后台action接收的三种方法
- 面试之数据库算法http://www.newsmth.net/nForum/#!article/Algorithm/37574?p=1
- 计算机网络17--Email应用
- [踩过的坑]spring事务为什么不回滚!
- RxJava过滤操作符 filter
- LeetCode OJ ---- Longest Substring Without Repeating Characters
- OpenGL ES纹理映射坐标对应
- 拿到一份开源项目,快速定位感兴趣的功能代码
- PLSQL如何调试存储过程
- android studio中导入xUtils和xUtils3开源库
- 文章标题
- Android项目:手机安全卫士(3)—— 主界面布局
- 企商云惠--我的起点
- UVA 1626 Brackets sequence dp:经典&&类似于三角剖分