코딩테스트/Leetcode

Leetcode - [Medium]3. Longest Substring Without Repeating Characters

aiemag 2021. 3. 7. 23:52
반응형

leetcode.com/problems/longest-substring-without-repeating-characters/

 

Longest Substring Without Repeating Characters - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 
 
int lengthOfLongestSubstring(char * s){
    
    int ctable[255= {0, };
    
    char cmax[255];
    int max_len = 0;
    
    char ctmp[255];
    int tmp_len = 0;
    
    int idx;
    int o_idx = 0;
    char* o = s;
    
    while(*s){        
        idx = *- ' ';
        
        if(ctable[idx]==0){            
            ctmp[tmp_len++= *s;
            ctable[idx] = 1;
        }else{
            if(max_len < tmp_len){
                max_len = 0;
                for(int i=0 ; i<tmp_len ; i++){
                    cmax[max_len++= ctmp[i];
                }
                
            }
                        
            for(int i=0 ; i<tmp_len ; i++){
                ctable[ctmp[i] - ' '= 0;
            }
            tmp_len = 0;
            
            s = (o + (++o_idx));            
            continue;            
        }        
        
        s++;
    }
    
    if(max_len < tmp_len){
        max_len = 0;
        for(int i=0 ; i<tmp_len ; i++){
            cmax[max_len++= ctmp[i];
        }
 
    }
    
    return max_len;
}
cs

 

반응형