Clean c++ code by using hash


  • 2
    L
    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            int hash[256];
            int maxlen = 0;
            int len = 0;
            int stp = 0;
            memset(hash,-1,256*sizeof(int));
            for(int i=0; i<s.size(); i++) {
                if(hash[s[i]]>=stp) {
                    int pos = hash[s[i]];
                    len = i - stp;
                    if (len > maxlen)
                        maxlen = len;
                    stp = pos+1;
                }
                hash[s[i]] = i;
            }
            len = s.size() - stp;
            return maxlen>len? maxlen: len;
        }
    };

  • 0
    N

    hi,what doe "stp"mean?


  • 0
    J

    @nathan I guess stp means "start Point" or "Step" :)


Log in to reply
 

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