码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode Longest Substring Without Repeating Characters

时间:2014-12-30 17:16:14      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

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.

Show Tags









题意:求最长的没有重复字母的子串

思路:双指针的方法,一个指向当前子串的头(左边),一个指向尾(右边),显然如果尾指针的这个字符出现过的话,那么头指针就要跳到相应的位置了,遍历一遍找最大的

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int Max = 0;
        int cur = -1;
        int vis[256];
        memset(vis, -1, sizeof(vis));
        
        for (int i = 0; i < s.length(); i++) {
            if (vis[s[i]] > cur) 
                cur = vis[s[i]];
            Max = max(Max, i - cur);
            vis[s[i]] = i;
        }
        
        return Max; 
    }
};


LeetCode Longest Substring Without Repeating Characters

标签:

原文地址:http://blog.csdn.net/u011345136/article/details/42266941

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!