zoukankan      html  css  js  c++  java
  • 43. Multiply Strings java solutions

    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.
    • Converting the input string to integer is NOT allowed.
    • You should NOT use internal library such as BigInteger.
     1 public class Solution {
     2     public String multiply(String num1, String num2) {
     3         StringBuilder ans = new StringBuilder();
     4         int[] tmp = new int[num1.length() + num2.length()];
     5         for(int i = num1.length()-1; i>=0;i--){
     6             for(int j = num2.length()-1; j>=0;j--){
     7                 tmp[i+j+1] += (num1.charAt(i) - '0')*(num2.charAt(j) - '0');
     8             }
     9         }
    10         
    11         for(int i = tmp.length-1; i >= 1;i--){
    12             tmp[i-1] += tmp[i]/10;
    13             tmp[i] %= 10;
    14         }
    15         
    16         int left = 0;
    17         while(left < tmp.length-1 && tmp[left] == 0)left++;// 这里需要考虑一个 num1 和 num2 有一个为0 的情况。
    18         for(;left < tmp.length; left++){
    19             ans.append(tmp[left]);
    20         }
    21         return ans.toString();
    22     }
    23 }

  • 相关阅读:
    Dom修改元素样式
    URL百分号编码
    accesskey附上一些实例
    dom实例
    dom 创建时间
    关系运算符
    赋值运算符
    js图片随机切换
    js自增图片切换
    transform-origin盒子旋转位置
  • 原文地址:https://www.cnblogs.com/guoguolan/p/5624177.html
Copyright © 2011-2022 走看看