Accepted Java Solution


  • 0
    M
    public String multiply(String num1, String num2) {
    
        if(num1.equals("0") || num2.equals("0")){
            return "0";
        }
        List<Integer> list=new ArrayList<>();
        for(int i=num2.length()-1;i>=0;i--){
            int bit=num2.length()-i;
            for(int j=num1.length()-1;j>=0;j--){
                int mpln=(num2.charAt(i)-'0')*(num1.charAt(j)-'0');
                if(list.size()<bit){
                    list.add(mpln);
                }
                else{
                    list.set(bit-1,list.get(bit-1)+mpln);
                }
                bit++;
            }
        }
    
        int size=list.size();
        for(int i=0;i<size;i++){
            if(list.get(i)>9){
            	int add_on=(int)Math.floor(list.get(i)/10);
                list.set(i,list.get(i)%10);
                if(list.size()<i+2){
                    list.add(add_on);
                }
                else{
                    list.set(i+1,list.get(i+1)+add_on);
                }
            }
        }
        StringBuffer sb=new StringBuffer();
        for(int i=list.size()-1;i>=0;i--){
            sb.append((char)(list.get(i)+48));
        }
        return sb.toString();
    }

Log in to reply
 

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