题目

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.

题解

题意就是给你两个字符串型的数字,给这两个数字做乘法。

如果直接转换成Integer做乘法就会溢出。

所以要一步一步来。

下面讲解引用自(http://leetcodenotes.wordpress.com/2013/10/20/leetcode-multiply-strings-%E5%A4%A7%E6%95%B4%E6%95%B0%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B9%98%E6%B3%95/comment-page-1/#comment-122),非常精巧的写法:

这个题第二遍做code就写的挺完美的,高兴~~几个要点:

  • 直接乘会溢出,所以每次都要两个single digit相乘,最大81,不会溢出。
  • 比如385 * 97, 就是个位=5 * 7,十位=8 * 7 + 5 * 9 ,百位=3 * 7 + 8 * 9 …
    可以每一位用一个Int表示,存在一个int[]里面。
  • 这个数组最大长度是num1.len + num2.len,比如99 * 99,最大不会超过10000,所以4位就够了。
  • 这种个位在后面的,不好做(10的0次方,可惜对应位的数组index不是0而是n-1),
    所以干脆先把string reverse了代码就清晰好多。
  • 最后结果前面的0要清掉。

代码如下:

 1     num1 = new StringBuilder(num1).reverse().toString();
 2     num2 = new StringBuilder(num2).reverse().toString();
 3     // even 99 * 99 is < 10000, so maximaly 4 digits
 4     int[] d = new int[num1.length() + num2.length()];
 5     for (int i = 0; i < num1.length(); i++) {
 6         int a = num1.charAt(i) - '0';
 7         for (int j = 0; j < num2.length(); j++) {
 8             int b = num2.charAt(j) - '0';
 9             d[i + j] += a * b;
         }
     }
     StringBuilder sb = new StringBuilder();
     for (int i = 0; i < d.length; i++) {
         int digit = d[i] % 10;
         int carry = d[i] / 10;
         sb.insert(0, digit);
         if (i < d.length - 1)
             d[i + 1] += carry;
         }
     //trim starting zeros
     while (sb.length() > 0 && sb.charAt(0) == '0') {
         sb.deleteCharAt(0);
     }
     return sb.length() == 0 ? "0" : sb.toString();
 }

Multiply Strings leetcode java的更多相关文章

  1. Multiply Strings [LeetCode]

    Problem Description http://oj.leetcode.com/problems/multiply-strings/ Basic idea is to multiply two ...

  2. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

  3. LeetCode 43. 字符串相乘(Multiply Strings)

    43. 字符串相乘 43. Multiply Strings 题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. ...

  4. leetcode面试准备:Multiply Strings

    1 题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...

  5. [Leetcode][Python]43: Multiply Strings

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...

  6. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  7. LeetCode: Multiply Strings 解题报告

    Multiply StringsGiven two numbers represented as strings, return multiplication of the numbers as a ...

  8. 【leetcode】Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

  9. 【LeetCode练习题】Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

随机推荐

  1. lupgu P3950 部落冲突

    题目链接 luogu P3950 部落冲突 题解 树剖线段树可以 lct还行 代码 #include<cstdio> #include<algorithm> inline in ...

  2. 【转载】C语言 构建参数个数不固定函数

    深入浅出可变参数函数的使用技巧本文主要介绍可变参数的函数使用,然后分析它的原理,程序员自己如何对它们实现和封装,最后是可能会出现的问题和避免措施. VA函数(variable argument fun ...

  3. Slickflow.NET 开源工作流引擎高级开发(四) -- 硬核编码:代码式快速构建流程图

    前言:通过设计器交互来创建流程图是比较常见的方式,这种方式是比较方便业务人员对流程的操作.然而,在需要流程模板,或者技术开发阶段以及一些自动化流程的处理过程中,使用代码快速创建流程图也是一种非常有必要 ...

  4. 青客宝团队Consul内部分享ppt

    青客宝团队Consul内部分享ppt   https://mp.weixin.qq.com/s?src=3&timestamp=1503647705&ver=1&signatu ...

  5. ARM-JTAG-SWD-schematic

  6. ThinkPHP 模型方法 getField() 和 select() 使用技巧

    getField() 使用技巧 getField() 方法是 ThinkPHP 中用来获取字段值的方法,区别于 select() 和 find() 方法,通常仅用于获取个别字段的值.但是事实上并没有那 ...

  7. PHP 服务端 和 APP 客户端 实现 RSA+AES 双向加密解密

    目的:服务端和移动端双向加密解密 共有七个文件 其中包括三个类文件 lib_aes.php aes对称加密解密类 server_rsa_crypt.php 服务端RSA公钥私钥非对称加密解密类 cli ...

  8. TCP编程的迷惑

    server : ip -- 192.168.96.132 client: ip--192.168.96.131 在服务端,accept函数的其中一个入参是listen-socket,会返回一个新的c ...

  9. JavaScript进阶系列01,函数的声明,函数参数,函数闭包

    本篇主要体验JavaScript函数的声明.函数参数以及函数闭包. □ 函数的声明 ※ 声明全局函数 通常这样声明函数: function doSth() { alert("可以在任何时候调 ...

  10. Delphi的自动编译软件Want

    Delphi自动编译环境的搭建及使用 什么是Want Want是一套Windows下用于编译Delphi源代码的工具.Want的名称是Windows Ant的意思. Ant是Java下最著名的自动编译 ...