问题描述:给定两个字符串,返回他们的乘积。

public class MultiplyStrings
{
public String multiply(String num1, String num2)
{
String n1 = new StringBuilder(num1).reverse().toString();
String n2 = new StringBuilder(num2).reverse().toString(); int[] d = new int[num1.length()+num2.length()]; //multiply each digit and sum at the corresponding positions
for(int i=0; i<n1.length(); i++){
for(int j=0; j<n2.length(); j++){
d[i+j] += (n1.charAt(i)-'0') * (n2.charAt(j)-'0');
}
} StringBuilder sb = new StringBuilder(); //calculate each digit
for(int i=0; i<d.length; i++)
{
int mod = d[i]%10;
int carry = d[i]/10;
if(i+1<d.length)
{
d[i+1] += carry;
}
sb.insert(0, mod);
} //remove front 0's
while(sb.charAt(0) == '0' && sb.length()> 1){
sb.deleteCharAt(0);
} return sb.toString();
}
}

Multiply Strings,字符串相乘的更多相关文章

  1. [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)

    转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...

  2. Multiply Strings 字符串相乘

    http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...

  3. [LeetCode] Multiply Strings 字符串相乘

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  4. [LeetCode] 43. Multiply Strings 字符串相乘

    Given two non-negative integers num1 and num2represented as strings, return the product of num1 and  ...

  5. 43. Multiply Strings 字符串相乘

    1. 原始题目 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2&qu ...

  6. 043 Multiply Strings 字符串相乘

    给定两个以字符串表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积.注意:    num1 和 num2 的长度均小于110.    num1 和 num2 均只包含数字 0 ...

  7. Leetcode43. Multiply Strings字符串相乘(大数相乘)

    给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", num ...

  8. 43. Multiply Strings字符串相乘

    网址:https://leetcode.com/problems/multiply-strings/submissions/ 参考:https://leetcode.com/problems/mult ...

  9. [Leetcode] Multiply strings 字符串对应数字相乘

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  10. 【LeetCode每天一题】Multiply Strings(字符串乘法)

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...

随机推荐

  1. 你的项目应当使用Spring Boot吗?(译文)

    注:这是一篇译文,参考链接:https://www.e4developer.com/2018/09/24/should-you-use-spring-boot-in-your-project/ Spr ...

  2. LeetCode-Integer Breaks

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  3. 基于GTID的主从架构异常处理流程

    通常情况下我们主库的binlog只保留7天,如果从库故障超过7天以上的数据没有同步的话,那么主从架构就会异常,需要重新搭建主从架构. 本文就简单说明下如何通过mysqldump主库的数据恢复从库的主从 ...

  4. AutoArchive settings explained

    AutoArchive settings explained Applies To: Outlook 2010 More... Less AutoArchive helps manage the sp ...

  5. Ad Exchange

    品友互动-基于大数据技术的人工智能决策平台 http://www.ipinyou.com.cn/about?flag=milestones

  6. elastic search 查询语句

    部署了半个月,分析一下数据: 需要提前知道的是,tpot中,每天的数据存一个index,然后每个index里面有不同的type,每条请求一个document 共24万条请求: 查看整个集群所有数据 以 ...

  7. MongoDB-5: 查询(游标操作、游标信息)

    一.简介 db.collection.find()可以实现根据条件查询和指定使用投影运算符返回的字段省略此参数返回匹配文档中的所有字段.并返回到匹配文档的游标,可以随意修改查询限制.跳跃.和排序顺序的 ...

  8. Handler 与 Toast

    Toast或者Dialog中都有一个Handler的成员变量,所以如果不是在主线程中使用Toast或Dialog,则需要在使用Toast或者Dialog的线程中初始化Looper. Looper.pr ...

  9. 解决Object() takes on paramenters的问题

    先贴上今天的代码: 解决:是由于父类中的_init_()写成了_int_(),由于粗心导致,下次注意.

  10. beego——URL构建

    如果可以匹配URl,那么beego也可以生成URL吗?当然可以. UrlFor()函数就是用于构建执行函数的URL的.它把对应控制器和函数名结合的字符串作为第一个参数,其余参数对应URL中的变量.未知 ...