leetcode 23: Longest Substring Without Repeating Characters

本文介绍了一种算法,用于找出给定字符串中最长的不包含重复字符的子串长度。通过三种不同的实现方式(C++, Java 和 C++),详细展示了如何使用滑动窗口和哈希表等数据结构来高效解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

mistakes I made:1. forget record start point. I resetted longest to 0 after accountting a repeatted char which is wrong. It should start from the next char following the first char of two repeatted chars.

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if( s.size() == 0) return 0;
        
        #define MAX_LEN 256
        char flag[MAX_LEN];
        reset(flag, MAX_LEN);
        
        int max = 0;
        int longest = 0;
        int start = 0;
        
        for(int i=0; i<s.size(); i++) {
            if( flag[ s[i] ] == -1 ){
                longest++;
            } else {
                max = max < longest ? longest : max;
                
                for(int j=start; j<i; j++){
                    flag[ s[j] ] = -1;
                    if( s[j] == s[i] ) {
                        start = j + 1;
                        break;
                    }  
                }
                longest = i - start + 1;
                
            }
            
            flag[ s[i] ] = 1;
            
        }
        max = max < longest ? longest : max;
        return max;
    }
    
private:
    void reset(char a[], int n){
        for(int i=0; i<n; i++) {
            a[i] = -1;
        }
    } 

};


 

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        // Start typing your Java solution below
        // DO NOT write main() function
        int max = Integer.MIN_VALUE;
        int start = 0;
        HashMap<Character,Integer> map = new HashMap<Character, Integer>();
        
        for(int i=0; i<s.length(); i++) {
            char c = s.charAt(i);
            if( map.containsKey(c)){
                for(int j=start;j<i;j++) {
                    if(s.charAt(j)==c) break;
                    map.remove(s.charAt(j));
                }
                start = map.get(c)+1;
                map.put(c,i);
            } else {
                map.put(c,i);
                if( i-start+1 > max ) max = i-start+1;
            }
        }
        return max;
    }
}

c++: 
class Solution {
public:


    int lengthOfLongestSubstring(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        unordered_set<char> myset;
        int sz = s.size();
        
        int cur = 0;
        int max = 0;
        int start = 0;
        
        for(int i=0; i<sz; i++) {
            char c = s[i];
            
            if( myset.find(c) == myset.end() ) {
                myset.insert(c);
                cur++;
                max = max>cur ? max : cur;
            } else {
                int j=start;
                while( s[j]!=c) {
                    myset.erase( s[j] );
                    ++j;
                }
                start = j+1;
                cur = i-start+1;
            }
        }
        return max;
    }
};


转载于:https://www.cnblogs.com/xishibean/archive/2013/01/09/2951400.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值