Encountering runtime error while it's fine on my laptop


  • 0
    H

    It's fine on my laptop (with Apple LLVM version 7.0.2) but encountered runtime error here. Following is the code,

    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            if(s.size()==0) return 0;
    
            int start=0,end=0,max=1,cur=1;
            int temp=0,tmpPos=-1;
            int subStrPos[256];
            //-1 for "not in substr"
            for(int i=0;i<256;i++){
                subStrPos[i]=-1;
            }
            subStrPos[int(s[start])]=0;
            
            int size=s.size();
            while(end!=size){
                end++;
                if(end==size) break;
                
                //end pointer move backward until find repeating char
                while(end!=size && subStrPos[int(s[end])]==-1){
                    temp=int(s[end]);
                    tmpPos=subStrPos[temp];
                    subStrPos[temp]=end;
                    end++;
                }
                
                cur=end-start;
                if(cur>max)max=cur;
    
                //adjust adjust "start" and let "end" move to the next (repeating) char
                if(end!=size && subStrPos[int(s[end])]>=0){
                    tmpPos=subStrPos[s[end]];
                    for(int i=start;i<subStrPos[end];i++){
                        subStrPos[i]=-1;
                    }
                    cur-=tmpPos+1-start;
                    start=tmpPos+1;
                    subStrPos[s[end]]=end;
                }
            }
            return max;
            
        }
    };
    

    Attached is the code I run on my machine, which is basically the same, and the case it failed on. On my computer, it gave the same answer as an accepted version of my code (using map instead of array) on this case, but had runtime error on the platform.

    0_1477506534538_length.cpp
    0_1477506670703_subStrRunCase

    Any advise?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.