leetcode: Longest Substring Without Repeating Characters

http://oj.leetcode.com/problems/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.

思路

查表,反正就26个字母,出现过的做个标记就行了。

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s) {
 4         int len = s.length(), max = 0, i = 0;
 5         
 6         while (i < len) {
 7             int sub_len = 1, next = i + 1;
 8             vector<bool> marks(26, false);
 9             
10             marks[s[i] - 'a'] = true;
11             while (next < len) {
12                 if (marks[s[next] - 'a']) {
13                     break;
14                 }
15                 
16                 marks[s[next] - 'a'] = true;
17                 ++next;
18                 ++sub_len;
19             }
20             
21             if (sub_len > max) {
22                 max = sub_len;
23             }
24             
25             ++i;
26         }
27         
28         return max;
29     }
30 };

 

posted @ 2013-10-11 15:01  移山测试工作室黑灯老师  阅读(278)  评论(0编辑  收藏  举报
count website visits
Buy Computers