Please critic my solution and help debugging it. Thank you.


  • 0
    X

    class Solution {

    /*     0     ,1  ,2, 3 ,4
    n->5, 10000,1000,100,10,1
    n->3, 100,10,1
    n->2, 10, 1
    n->1, 1
    where n = numstr.length
    
    int[] arr = new int[5];
    
    for(int i=5;i>=0;i--){
        int temp = 1;
        if(i>0){
            int index = i;
            while(index>0){
            temp *= 10;
            index--;
            }
        }
        arr[i] = temp;
        }
    Array.sort();
    
    */
    static HashMap<Character,Integer> map = new HashMap<Character,Integer>();
    
    public static String multiply(String num1, String num2) {
            map.put('0',0);
            map.put('1',1);
            map.put('2',2);
            map.put('3',3);
            map.put('4',4);
            map.put('5',5);
            map.put('6',6);
            map.put('7',7);
            map.put('8',8);
            map.put('9',9);
        
        return ""+getInteger(num1)*getInteger(num2);
    }
    
    
    private static int getInteger(String numstr){
        
        /*
        int [] pair = new int[]{};
        if(numstr.length()==3){
            pair = new int[]{100,10,1};
        }
        else if(numstr.length()==2){
            pair = new int[]{10,1};
        }
        else if(numstr.length()==1){
            pair = new int[]{1};
        }*/
        
        
        int[] arr = new int[numstr.length()];
        System.out.println(numstr.length());
        for(int i=arr.length-1;i>=0;i--){
        	int temp = 1;
        	int index = i;
        	if(i>0){
        	while(index>0){
        		temp*=10;
        		index--;
        	}
        	}
        
         arr[i] = temp;
        }
        Arrays.sort(arr);
        
        for(int i:arr){
        	System.out.println("num: "+i);
        }
        
        int sum = 0;
        
        for(int i=0;i<numstr.length();i++){
            sum  += map.get(numstr.charAt(i))*arr[i];
        }
        
        return sum;
    }
    

    }


  • 0
    X
    class Solution {
        
        /*     0     ,1  ,2, 3 ,4
        n->5, 10000,1000,100,10,1
        n->3, 100,10,1
        n->2, 10, 1
        n->1, 1
        */
        static HashMap<Character,Integer> map = new HashMap<Character,Integer>();
        
        /* use the character '0'->'9' as the key, and the Integer 0->9 as the matching value  */
        public static String multiply(String num1, String num2) {
                map.put('0',0);
                map.put('1',1);
                map.put('2',2);
                map.put('3',3);
                map.put('4',4);
                map.put('5',5);
                map.put('6',6);
                map.put('7',7);
                map.put('8',8);
                map.put('9',9);
            
            return ""+getInteger(num1)*getInteger(num2);
        }
        
        
        private static int getInteger(String numstr){
            /*
            Using the array to handles multiples of 1, and 10s, so the idea is if the 
            the length of numstr is only one then we know the integer we are trying to get is only a single digit. e.g 
            then the value at arr[0] would be 1,
            if the length is numstr is 2 then the arr values would be [1,10], if 3 then [1,10,100], 
            if 4 then [1,10,100,1000] and so forth ......
            
            then below for loop  and the nested while loop should be able to populate the arr
            */
            int[] arr = new int[numstr.length()];
            //System.out.println(numstr.length());
            for(int i=arr.length-1;i>=0;i--){
            	int temp = 1;
            	int index = i;
            	if(i>0){
            	while(index>0){
            		temp*=10;
            		index--;
            	}
            	}
            
             arr[i] = temp;
            }
            Arrays.sort(arr);
           
            /*
            for(int i:arr){
            	System.out.println("num: "+i);
            }
            */
            
            int sum = 0;
            
            /*going through each chracter in the numstr, and multiply it with the value in the arr where it contains -> {1,10,100,1000,10000}*/
            for(int i=0;i<numstr.length();i++){
                sum  += map.get(numstr.charAt(i))*arr[i];
            }
            
            return sum;
        }
    
    }
    
    
    It says that  20 / 311 test cases passed.
    it seem that the code has my answer reversed, where it is 
    "89" * "9", not "98" * "9"
    
    Input:
    "98"
    "9"
    Output:
    "801"
    Expected:
    "882"
    

Log in to reply
 

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