zoukankan      html  css  js  c++  java
  • LeetCode: Multiply Strings 解题报告

    Multiply Strings
    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.

    SOLUTION 1:

    参考自http://blog.csdn.net/fightforyourdream/article/details/17370495

    相当优雅的算法,主页君稍微改进,把翻转String这一步拿掉了。比起一般的做法,这个算法很容易就BUG FREE.

    思路:
    1 建立数组,双层循环遍历两个string,把单位的乘积累加到数组相应的位置
    2 处理进位并输出
    3 注意前导零的corner case

     1 public class Solution {
     2     public String multiply(String num1, String num2) {
     3         if (num1 == null || num2 == null) {
     4             return null;
     5         }
     6         
     7         int len1 = num1.length();
     8         int len2 = num2.length();
     9         
    10         int[] product = new int[len1 + len2];
    11         
    12         // 计算相应位置的product.
    13         for (int i = 0; i < len1; i++) {
    14             for (int j = 0; j < len2; j++) {
    15                 // 注意,这里要使用+=以不断累加乘积
    16                 product[i + j] += (num1.charAt(len1 - 1 - i) - '0') * (num2.charAt(len2 - 1 - j) - '0');
    17             }
    18         }
    19         
    20         StringBuilder ret = new StringBuilder();
    21         
    22         int carry = 0;
    23         // 计算进位
    24         for (int i = 0; i < len1 + len2; i++) {
    25             product[i] = product[i] + carry;
    26             int digit = product[i] % 10;
    27             carry = product[i] / 10;
    28             ret.insert(0, digit);
    29         }
    30         
    31         // 去掉前导0
    32         while (ret.length() > 1 && ret.charAt(0) == '0') {
    33             ret.deleteCharAt(0);
    34         }
    35         
    36         return ret.toString();
    37     }
    38 }
    View Code

    2015.1.20 redo 

     1 public String multiply(String num1, String num2) {
     2         // 18:24
     3         if (num1 == null || num2 == null) {
     4             return null;
     5         }
     6         
     7         int len1 = num1.length();
     8         int len2 = num2.length();
     9         int[] sum = new int[len1 + len2];
    10         
    11         // sum all of the mutiply result.
    12         for (int i = 0; i < len1; i++) {
    13             for (int j = 0; j < len2; j++) {
    14                 sum[i + j] += (num1.charAt(len1 - 1 - i) - '0') * (num2.charAt(len2 - 1 - j) - '0');
    15             }
    16         }
    17         
    18         int carry = 0;
    19         for (int i = 0; i < len1 + len2; i++) {
    20             sum[i] = sum[i] + carry;
    21             
    22             // Bug1: this line should be processed first.
    23             carry = sum[i] / 10;
    24             sum[i] %= 10;
    25         }
    26         
    27         StringBuilder sb = new StringBuilder();
    28         for (int i = 0; i < len1 + len2; i++) {
    29             sb.insert(0, sum[i] + "");
    30         }
    31         
    32         // delete the leading "0"
    33         while (sb.charAt(0) == '0' && sb.length() != 1) {
    34             sb.deleteCharAt(0);
    35         }
    36         
    37         return sb.toString();
    38     }
    View Code

    请至主页群GITHUB:

    https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/Multiply.java

  • 相关阅读:
    POJ 1797 Heavy Transportation (Dijkstra算法变形)
    HDU 2883 kebab (最大流)
    HDU 3338 Kakuro Extension (最大流)
    简单的敏捷工具更受敏捷开发团队青睐
    让敏捷工具在敏捷开发中发挥高效作用
    看板工具和Scrum工具选择之惑!
    敏捷开发如何在创业公司实施
    八个垂手可得的Scrum敏捷开发工具
    那些我们常用的scrum工具、敏捷开发工具
    Scrum/Sprint敏捷开发方法.
  • 原文地址:https://www.cnblogs.com/yuzhangcmu/p/4116211.html
Copyright © 2011-2022 走看看