leetcode 43 Multiply Strings 大数相乘
感觉是大数相乘算法里面最能够描述、模拟演算过程的思路
class Solution {
public String multiply(String num1, String num2) {
if(num1.charAt(0) == '0' || num2.charAt(0) == '0'){
return "0";
}
int len1 = num1.length();
int len2 = num2.length();
int len = len1+len2;
int[] arr = new int[len];
for(int i=len1-1;i>=0;i--){
for(int j=len2-1;j>=0;j--){
arr[i+j+1] += (num1.charAt(i)-'0')*(num2.charAt(j)-'0');
}
}
for(int i=len-1;i>0;i--){
if(arr[i]>=10){
arr[i-1] += arr[i]/10;
arr[i]%=10;
}
}
int i=0;
if(arr[i] == 0) i++;
String res = "";
while(i<len){
res = res+arr[i++];
}
return res;
}
}
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(字符串相乘)
题目链接: https://leetcode.com/problems/multiply-strings/?tab=Description 求解大数相乘问题 按照上图所示,进行嵌套循环计算 ...
- 43. Multiply Strings 字符串相乘
1. 原始题目 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2&qu ...
- Java [Leetcode 43]Multiply Strings
题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...
- [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)
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
- Multiply Strings 大数相乘 java
先贴上代码 public String multiply(String num1, String num2) { String str = ""; StringBuffer sb ...
- 43. Multiply Strings字符串相乘
网址:https://leetcode.com/problems/multiply-strings/submissions/ 参考:https://leetcode.com/problems/mult ...
随机推荐
- Python的异步编程[0] -> 协程[0] -> 协程和 async / await
协程 / Coroutine 目录 生产者消费者模型 从生成器到异步协程– async/await 协程是在一个线程执行过程中可以在一个子程序的预定或者随机位置中断,然后转而执行别的子程序,在适当的时 ...
- vc ini配置文件读写
ini文件(即Initialization file),这种类型的文件中通常存放的是一个程序的初始化信息.ini文件由若干个节(Section)组成,每个Section由若干键(Key)组成,每个Ke ...
- 设计高效SQL: 一种视觉的方法
行; 这听起来很直观,但最有效的方法是什么?你可能有如下选择:行,其中有50行你必须剔除行,其中有450行你必须剔除行中剔除50行听起来比从500行中剔除450行更高效,但是请记住:聚簇,或者说,数据 ...
- DNS无响应
无语,运行某某大品牌的杀毒软件后,无法上网,window检查后是DNS服务器无响应. 开始>运行>输入"netsh winsock reset" 然后重启电脑. pi ...
- UVA GCD - Extreme (II)
discription Given the value of N, you will have to find the value of G. The definition of G is given ...
- [LOJ6437]PKUSC
旋转多边形是没有前途的,我们考虑旋转敌人,那么答案就是所有人的可行区间长度之和除以$2\pi$ 首先对每个敌人找到那些旋转后会落到多边形上的角度,实际上就是圆和一些线段求交,解方程即可,注意判一下落在 ...
- 【贪心】【线性基】bzoj2460 [BeiJing2011]元素 / bzoj3105 [cqoi2013]新Nim游戏
p2460: #include<cstdio> #include<algorithm> using namespace std; #define N 1001 typedef ...
- 1.4(Mybatis学习笔记)关联映射
一.一对一 mybatis处理一对一主要通过<resultMap>中的<association>元素来处理. <association>元素主要使用方方式有两种: ...
- cocoapods更新
使用sudo gem install cocoapods更新提示: ERROR: While executing gem ... (Errno::EPERM) Operation not permit ...
- iOS:MBProgressHUD的基本使用
下载地址:https://github.com/jdg/MBProgressHUD/ //方式1.直接在View上show HUD = [[MBProgressHUD showHUDAddedTo:s ...