Why my one pass O(n) Java solution ran about 40ms, due to the variant test cases???


  • 0
    L

    If so, I think it's unfair.

    public class Solution {
        public int lengthOfLongestSubstring(String s) {
            char[] sc = s.toCharArray();
            int[] hash = new int[256];
            
            int max = 0, i = 0, j = 0;
            for (; j < sc.length; j++) {
                if (hash[(int)sc[j]] != 0) {
                    max = Math.max(max, j-i);
                    i = Math.max(hash[(int)sc[j]], i);
                }
                hash[(int)sc[j]] = j+1;
            }
            return Math.max(max, j-i);
        }
    }
    

  • 0

    Why it ran 40ms? Very strange question. That's simply the result of your code, the judge computer, and the test cases. As always.

    Not sure what you mean with "variant test cases".


  • 0
    L

    @StefanPochmann Because the best runtime of Java submissions is less than 5ms. I have ran several solutions who declaimed that they can finish in 10ms, but all the result are more or less than 45ms. So I am not sure that the 4ms solution's test cases are the same as my 40ms solution's.


Log in to reply
 

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