Java [Leetcode 43]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.
解题思路:
设置数组记录单个位置相乘的结果,最后负责相加进位。
代码如下:
public class Solution {
public String multiply(String num1, String num2) {
int num1Length = num1.length();
int num2Length = num2.length();
int d1, d2;
int carry = 0;
int temp;
int[] caculate = new int[num1Length + num2Length];
StringBuilder sb = new StringBuilder();
for (int i = num1.length() - 1; i >= 0; i--) {
d1 = num1.charAt(i) - '0';
for (int j = num2.length() - 1; j >= 0; j--) {
d2 = num2.charAt(j) - '0';
caculate[i + j + 1] += d1 * d2;
}
}
for (int i = caculate.length - 1; i >= 0; i--) {
temp = (caculate[i] + carry) % 10;
carry = (caculate[i] + carry) / 10;
caculate[i] = temp;
}
for (int num : caculate)
sb.append(num);
while (sb.length() > 1 && sb.charAt(0) == '0') {
sb.deleteCharAt(0);
}
return sb.toString();
}
}
Java [Leetcode 43]Multiply Strings的更多相关文章
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- LeetCode(43. Multiply Strings)
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
- [leetcode]43. Multiply Strings高精度乘法
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...
- LeetCode 43 Multiply Strings(字符串相乘)
题目链接: https://leetcode.com/problems/multiply-strings/?tab=Description 求解大数相乘问题 按照上图所示,进行嵌套循环计算 ...
- leetcode 43. Multiply Strings(高精度乘法)
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- leetcode 43 Multiply Strings 大数相乘
感觉是大数相乘算法里面最能够描述.模拟演算过程的思路 class Solution { public String multiply(String num1, String num2) { if(nu ...
- [Leetcode][Python]43: Multiply Strings
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
随机推荐
- c语言指针用法
一.指针 int t 定义整型变量 int *p p为指向整型数据的指针变量 int a[n] 定义整型数组a,它有n个元素 int *p[n] 定义指针数组p,它由n个指向整形数据的指针元素组成 i ...
- 快捷设置IE代理小工具
时间:2015-02-06 起因: 公司新装了PLM系统,用这个系统必须使用指定IP段的IP才能访问.所以为了还能愉快的继续使用代理进行特定网站的访问,我们必须要频繁的去设置IE代理,这也太麻烦了吧. ...
- java指令集
0x00 nop 什么都不做 0x01 aconst_null 将null推送至栈顶 0x02 iconst_m1 将int型-1推送至栈顶 0x03 iconst_0 将int型0 ...
- 制作输入框(Input)
怎样判断是否应当使用输入框 输入框,就是用户可以自由输入文本的地方.当需要判断是否需要使用输入框时,可以遵循一条原则:凡是需要用户自主输入文本的地方,几乎都必须使用输入框. 输入框的常见用法:输入登录 ...
- TOPAPI 消息通知机制
接收用户订阅消息 public class UserSubMain { public static void main(String[] args ) throws ApiException { St ...
- mysql-community-server 5.7.16 设置密码
那是由于mysql-community-server 5.7的密码是一个默认的随机密码,这个初始密码,mysql又不告诉你,我们需要重设这个密码. service mysqld stop mysqld ...
- C# - Lambda 表达式
Lambda 表达式分为三个部分: 1 参数定义部分.参数是未类型化的,因此编译器可以根据上下文推断出他们的类型. 2 =>运算符,把Lambda表达式的参数与表达式体分开. 3 表达式体. d ...
- java.lang.NoSuchMethodError: javaxservlet.http.HttpServletRequest.isAsyncStarted()Z
鸣谢网址:http://stackoverflow.com/questions/25940571/java-lang-nosuchmethoderror-javaxservlet-http-https ...
- pthread_create用法
linux下用C开发多线程程序,Linux系统下的多线程遵循POSIX线程接口,称为pthread. #include <pthread.h> int pthread_create(pth ...
- auto_ptr,shared_ptr 智能指针的使用
Q: 那个auto_ptr是什么东东啊?为什么没有auto_array?A: 哦,auto_ptr是一个很简单的资源封装类,是在<memory>头文件中定义的.它使用“资源分配即初始化”技 ...