Longest Substring Without Repeating Characters
来源:互联网 发布:淘宝衣服平面拍照片 编辑:程序博客网 时间:2024/06/10 06:50
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given “abcabcbb”, the answer is “abc”, which the length is 3.
Given “bbbbb”, the answer is “b”, with the length of 1.
Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.
我的解决方法:使用数组保存对应字符上次出现的位置
#include<iostream>#include<string>class Solution {public: int lengthOfLongestSubstring(string s) { int index, cur_index = -1, max_lenghth = 0; int char_set[256];//保存上次字符出现的位置 memset( char_set, -1, sizeof( char_set ) ); for( index = 0; index < s.size(); index++ ) { if( char_set[ s[ index ] ] > cur_index ) //字符重复出现 cur_index = char_set[ s[ index ] ]; if( index - cur_index > max_lenghth ) max_lenghth = index - cur_index; char_set[ s[ index ] ] = index; } return max_lenghth; }};
leetcode上给出的9行代码的解决方案:
int lengthOfLongestSubstring(string s) { vector<int> dict(256, -1); int maxLen = 0, start = -1; for (int i = 0; i != s.length(); i++) { if (dict[s[i]] > start) start = dict[s[i]]; dict[s[i]] = i; maxLen = max(maxLen, i - start); } return maxLen;}
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
- 设计模式之观察者设计模式
- GObject
- float:left导致不能居中问题
- ViewPager过渡动画position的理解
- c_位运算及习题
- Longest Substring Without Repeating Characters
- Maven 开 发 规 范
- ADB命令乱码
- ExtJs大比拼JQuery:Dom文档操作
- [dfs序+主席树] BZOJ2809: [Apio2012]dispatching
- 【项目管理和构建】——Maven简介(一)
- 4.19
- android中的shape使用之android:angle
- 如何下载编译android kernel 3.4