LeetCode java Hashtable compile error


  • 3
    S

    I was trying to solve this problem in a recursive way and store results that are computed in a Hashtable. But it has compile error saying cannot find class Hashtable. But it works in eclipse.

    public class Solution {
         Hashtable<Integer,Integer> results = new Hashtable<Integer,Integer>();
        	
    	 public  int climbStairs(int n) {
    	        if(n<0)
    	           return 0;
    	         
    	        if(n<=1){
    	           results.put(n,1);
    	           return 1;
                  }
    	        
    	        Integer tmp=results.get(n);
    	        if( tmp== null){
    	           tmp = climbStairs(n-1)+climbStairs(n-2);
    	           results.put(n,tmp);
    	        }
    	        return tmp;
    	    }
    	
    }

  • 9
    S

    add the following command before your realCode:

    import java.util.Hashtable; 
    

    or try using HashMap


  • 0
    S

    It works. Never know I need to import a package by myself.


Log in to reply
 

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