leetcode 43. Multiply Strings
2016-09-13 21:52
417 查看
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.
输入的都是正数
输入的字符串超出了Integer
不许使用BigInteger
大数相乘
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.
输入的都是正数
输入的字符串超出了Integer
不许使用BigInteger
public String multiply(String num1, String num2) { int[] result = new int[num1.length() + num2.length()]; if(num1.equals("0") || num2.equals("0")) return "0"; num1 = new StringBuilder(num1).reverse().toString(); num2 = new StringBuilder(num2).reverse().toString(); for(int i = 0;i < num1.length();i++){ for(int j = 0;j < num2.length();j++){ result[i+j] += (num1.charAt(i)-'0') * (num2.charAt(j)-'0') % 10; result[i+j+1] += (num1.charAt(i)-'0') * (num2.charAt(j)-'0') / 10; } } for(int i = 0;i < num1.length() + num2.length()-1;i++){ result[i+1] += result[i] / 10; result[i] = result[i] % 10; } StringBuffer sb = new StringBuffer(); for(int i:result) sb.append((char)('0'+i)); if(result[num1.length() + num2.length()-1] == 0){ return new StringBuilder(sb.toString()).reverse().toString().substring(1); } else{ return new StringBuilder(sb.toString()).reverse().toString().substring(0); } }
相关文章推荐
- php中实现矩阵的碰撞检测
- leetcode-python 第十二周
- 编译器的工作过程
- Java中thread类与Runnable接口的区别
- 初探appium之元素定位(1)
- Android动画学习笔记-Android Animation
- js数组的方法
- 黑盒测试——错误推测法
- 智能语音录制程序
- 《javascript设计模式与开放实践》学习(二)Function.prototype.bind
- Volley框架网络访问String,JsonObject,JsonArray
- 01 戴尔混合云DHCS今生与前世
- GoLang-2(变量)
- python——nagios监控nginx日志 request_time
- python——nagios监控nginx日志 request_time
- curl模拟请求、登陆以及带验证码登陆
- 数控进入数控显示实体四轴四面Cube应用软件开发
- 【学习笔记】快速平方根倒数算法
- Android_project目录及文件详解
- cf 712E Memory and Casinos