欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

Leetcode: Longest Substring Without Repeating Characters,leetcoderepeating

来源: javaer 分享于  点击 47064 次 点评:241

Leetcode: Longest Substring Without Repeating Characters,leetcoderepeating


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.

Idea: The idea of this problem is to check the longest substring (no repeating) for each position, keep track the max length in this process. We need a int character to store the visited information of each character.

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        if(s.size() <= 1) return s.size();
        int max_len = 0;
        int i=0;
        while (i<s.size()) {
            int j=i;
            vector<int> visited(256, 0);
            while (j < s.size()) {
                if(visited[s[j]] == 0){
                    visited[s[j]] = 1;
                    ++j;
                }
                else
                {
                    break;
                }
            }
            max_len = max(max_len, j-i);
            while(s[i] != s[j]) {
                ++i;
            }
            ++i;
        }
        return max_len;
    }
};



相关文章

    暂无相关文章

用户点评