LeetCode 43. Multiply Strings(乘法)
来源:互联网 发布:金融类软件测试 编辑:程序博客网 时间:2024/05/23 17:05
原题网址:https://leetcode.com/problems/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.
- Converting the input string to integer is NOT allowed.
- You should NOT use internal library such as BigInteger.
方法:模拟乘法运算。
public class Solution { public String multiply(String num1, String num2) { int[] n1 = new int[num1.length()]; int[] n2 = new int[num2.length()]; int[] m = new int[num1.length() + num2.length()]; for(int i=0; i<n1.length; i++) { n1[n1.length-1-i] = num1.charAt(i)-'0'; } for(int i=0; i<n2.length; i++) { n2[n2.length-1-i] = num2.charAt(i)-'0'; } for(int i=0; i<n1.length; i++) { int carry = 0; for(int j=0; j<n2.length; j++) { int mul = m[i+j] + n1[i]*n2[j] + carry; m[i+j] = mul % 10; carry = mul / 10; } if (carry > 0) m[i+n2.length] += carry; } boolean notZero = false; StringBuilder sb = new StringBuilder(); for(int i=m.length-1; i>=0; i--) { if (m[i] > 0) notZero = true; if (notZero) sb.append(m[i]); } if (!notZero) sb.append(0); return sb.toString(); }}
0 0
- LeetCode 43. Multiply Strings(乘法)
- LeetCode | 43. Multiply Strings(大整数乘法)
- LeetCode 43 Multiply Strings(高精度乘法)
- LeetCode-------43. Multiply Strings(字符串的乘法运算)
- LeetCode-43. Multiply Strings (JAVA)大整数乘法
- leetcode:Multiply Strings(字符串的乘法)【面试算法题】
- [LeetCode]43.Multiply Strings
- LeetCode --- 43. Multiply Strings
- [Leetcode] 43. Multiply Strings
- [leetcode] 43.Multiply Strings
- 43. Multiply Strings LeetCode
- leetcode 43. Multiply Strings
- LeetCode 43. Multiply Strings
- LeetCode 43. Multiply Strings
- LeetCode 43. Multiply Strings
- [leetcode] 43. Multiply Strings
- Leetcode 43. Multiply Strings
- (Leetcode)43. Multiply Strings
- 电子商务网站商品推荐案例分析
- C++第六次实验—循环(实验)
- /dev/shm应该设置多大Oracle 实例启动时才能不报ORA-00845
- CodeForces 509B Painting Pebbles
- 0.1 Spark Streaming 实现思路与模块概述
- LeetCode 43. Multiply Strings(乘法)
- AfinalHttp的AjaxBack简单封装
- Java 连接hbase 进行简单操作
- C++第6次实验——项目一输出星号图
- 简谈快速排序
- 对象检测之行人检测(1)
- Effective cpp 读书笔记6
- 欢迎使用CSDN-markdown编辑器
- jQuery Ajax与prototypeJS Ajax数据获取的方式