LeetCode 1281:Subtract the Product and Sum of Digits of an Integer

LeetCode:1281
標籤:Math
難度:Easy

 

public class LeetCode1281 {
	/*
	 * Easy
	 * 1281. Subtract the Product and Sum of Digits of an Integer
	 * Given an integer number n, return the difference between the product of its digits and the sum of its digits.
	 * 
	 * Example:
	 * Input: n = 234
	 * Output: 15 
	 * Explanation: 
	 * Product of digits = 2 * 3 * 4 = 24 
	 * Sum of digits = 2 + 3 + 4 = 9 
	 * Result = 24 - 9 = 15
	 */
    public static void main(String[] args) {
        int input = 234;
        int res = subtractProductAndSum(input);
        System.out.print("res: " + res);
		
    }
	
    public static int subtractProductAndSum(int n) {
    	int res = 0;
    	int multiplyRes = 0;
    	int addRes = 0;
    	char[] charArr = String.valueOf(n).toCharArray();
    	for(int i = 0 ; i < charArr.length ; i++) {
    		if(i == 0) {
    			multiplyRes = Integer.valueOf(String.valueOf(charArr[i]));
    			addRes = Integer.valueOf(String.valueOf(charArr[i]));
    		}else {
    			multiplyRes = multiplyRes * Integer.valueOf(String.valueOf(charArr[i]));
    			addRes += Integer.valueOf(String.valueOf(charArr[i]));
    		}
    	}
        res = multiplyRes - addRes;
        return res;
    }
}