LeetCode每日一题:multiply strings

2017-06-23  本文已影响29人  yoshino

问题描述

Given two numbers represented as strings, return multiplication of the numbers as a string.
Note: The numbers can be arbitrarily large and are non-negative.

问题分析

这题简直就是为java设计的,用C++的话还要考虑转化和进位,在java只需要用BigDecimal进行大数精确运算即可。

代码实现

public String multiply(String num1, String num2) {
        BigDecimal n1 = new BigDecimal(num1);
        BigDecimal n2 = new BigDecimal(num2);
        return n1.multiply(n2).toString();
    }
上一篇下一篇

猜你喜欢

热点阅读