Leetcode 3 Longest Substring Without Repeating Characters
来源:互联网 发布:carven淘宝旗舰店 编辑:程序博客网 时间:2024/05/17 09:34
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.
Subscribe to see which companies asked this question
class Solution {public: int lengthOfLongestSubstring(string s) { map<char,int> alpha2num;int maxLen = 0, stratIndex=0;for (int i = 0; i < s.size(); ++i){if(alpha2num.count(s[i])) {int loc = alpha2num[s[i]];if(loc+1 > stratIndex) {stratIndex = loc+1;}}alpha2num[s[i]] = i;if(i-stratIndex+1 > maxLen) {maxLen = i-stratIndex+1;}}return maxLen; }};
#include <iostream>#include <string>#include <cstdio>#include <map>using namespace std;class Solution {public:int lengthOfLongestSubstring(string s) {map<char,int> alpha2num;int maxLen = 0, stratIndex=0;for (int i = 0; i < s.size(); ++i){if(alpha2num.count(s[i])) {int loc = alpha2num[s[i]];if(loc+1 > stratIndex) {stratIndex = loc+1;}alpha2num[s[i]] = i;if(i-stratIndex+1 > maxLen) {maxLen = i-stratIndex+1;}} else {alpha2num[s[i]]=i;if(maxLen < i+1-stratIndex)maxLen = i+1-stratIndex;}}return maxLen;}};int main() {Solution *ss = new Solution();string s;cin >> s;int ans = ss->lengthOfLongestSubstring(s);cout << ans << endl;}
0 0
- LeetCode 3 - Longest Substring Without Repeating Characters
- LeetCode 3: Longest Substring Without Repeating Characters
- leetcode-3:Longest Substring Without Repeating Characters
- LeetCode(3) Longest Substring Without Repeating Characters
- leetcode 3LONGEST SUBSTRING WITHOUT REPEATING CHARACTERS
- Leetcode【3】:Longest Substring Without Repeating Characters
- [leetcode 3] Longest Substring Without Repeating Characters
- [Leetcode] 3 - Longest Substring Without Repeating Characters
- LeetCode 3:《Longest Substring Without Repeating Characters》
- [leetcode 3] Longest Substring Without Repeating Characters
- [leetcode] 3 Longest Substring Without Repeating Characters
- [Leetcode]3Longest Substring Without Repeating Characters
- leetcode 3 Longest Substring Without Repeating Characters
- LeetCode #3 Longest Substring Without Repeating Characters
- leetcode-3 Longest Substring Without Repeating Characters
- LeetCode 3 Longest Substring Without Repeating Characters
- leetcode 3: Longest Substring Without Repeating Characters
- LeetCode 3 Longest Substring Without Repeating Characters
- php 类的构造方法 __construct
- 现代密码学 实验
- 如何跳转到另一个activity并且关闭当前的activity
- Hadoop IO 特性详解(1)【数据完整性】
- 时间序列算法的改善
- Leetcode 3 Longest Substring Without Repeating Characters
- Android Wear 开发入门
- POJ 1936 All in All
- 解决利用模板装linux网卡eth1问题
- 如何在ubuntu14.04中安装IE8
- 黑马程序员--C语言自学笔记---10字符串、指针
- LintCode 快速幂
- SQL语句大全
- scala学习总结之二