100.0% faster than everyone???


  • 0
    A

    Leetcode said this solution took 2 ms which was 100.0% faster than everyone the first time I ran it. Ran it again and it took 10 ms. A third time said 8 ms. I guess you can't really trust the runtime...

    public class Solution {
        public int[] twoSum(int[] nums, int target) {
            HashMap<Integer, Integer> map = new HashMap<>(nums.length);
            int[] result = null;
            int index = 0;
            while (result == null) {
                Integer operandIndex = map.get(target-nums[index]);
                if (operandIndex != null) {
                    result = new int[] { index, operandIndex };
                } else {
                    map.put(nums[index], index);
                    index++;
                }
            }
            
            return result;
        }
    }
    

Log in to reply
 

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