Given a array of integers, find factorial of each number. Integer can be in range 1 to 25


  • 0
    C

    look out for boundary conditions.


  • 0
    A
    package dynamic;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class factorial {
    	public static Map<Integer,Long> memo = new HashMap<Integer,Long>();
    	
    	public static long calculate_factorial(int i) {
    		if (i ==1) return 1;
    		if(memo.containsKey(i)) {
    			return memo.get(i);
    		}
    		long res = i*calculate_factorial(i-1);
    		memo.put(i, res);
    		return res;
    	}
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		int[] input = { 10, 5, 20, 23 };
    		for (int i = 0; i < input.length; i++) {
    			System.out.println(input[i] + ":" + calculate_factorial(input[i]));
    		}
    
    	}
    
    }
    

Log in to reply
 

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