Why did this cause Time Limit Exceed


  • 0
    S

    This question is not very hard , but when I was doing this I always got time limit exceed error, below is my code:

    private Map<Integer, Integer> map = new HashMap<>();
        // Add the number to an internal data structure.
    	public void add(int number) {
    	    map.put(number, map.containsKey(number) ? map.get(number) + 1 : 1);
    	}
    
        // Find if there exists any pair of numbers which sum is equal to the value.
    	public boolean find(int value) {
    	    for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
    	        int i = entry.getKey();
    	        int j = value - i;
    	        if((i == j && entry.getValue() > 1) || (i != j && map.containsKey(j))) {
    	            return true;
    	        }
    	    }
    	    return false;
    	}
    

    After looking for other similar code, I found the only difference between us is that I declared Map<Integer, Integer> map = new HashMap<>(); instead of HashMap<Integer, Integer> map = new HashMap<>();.

    Can anyone tell me the reason? And please don't give a down vote even if you don't like my question, thx.


Log in to reply
 

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