4ms Java Solution using array as set Beats 100.00%


  • 0
    C
        public int lengthOfLongestSubstring(String s) {
            if(s == null || s.length() == 0)
                return 0;
            int maxLength = 0;
            int proLocation = 1;
            int aftLocation = 1;
            int[] c = new int['~' - ' ' + 1];
            for(int i = 0; i < s.length(); i++) {
                char a = s.charAt(i);
                if(c[a - ' '] >= proLocation) {
                    maxLength = maxLength < aftLocation - proLocation + 1 ? aftLocation - proLocation + 1 : maxLength;
                    proLocation = c[a - ' '] + 1;
                }
                c[a - ' '] = i + 1; 
                aftLocation = c[a - ' '];
            }
            maxLength = maxLength < aftLocation - proLocation + 1 ? aftLocation - proLocation + 1 : maxLength;
            return maxLength;
        }
    

Log in to reply
 

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