LeetCode_OJ【43】Multiply Strings
来源:互联网 发布:淘宝大v认证运营故事 编辑:程序博客网 时间:2024/04/27 14:11
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.
Subscribe to see which companies asked this question
首先将两个String转换为int数组,然后一位一位模拟乘法就可以了。
下面是本题的JAVA实现。
public class Solution { public String multiply(String num1, String num2) { int[] s1 = new int[num1.length()]; int[] s2 = new int[num2.length()]; int[] res = new int[num1.length() + num2.length()]; for(int i = 0 ; i < num1.length() ;i++){ s1[i] = num1.charAt(num1.length() -1 -i) - '0'; } for(int i = 0 ; i < num2.length() ;i++){ s2[i] = num2.charAt(num2.length() -1 -i) - '0'; } int middle = 0 ; for(int i = 0 ; i < num1.length() + num2.length() ; i ++){ for(int j = 0 ; j <= i ; j++){ if(j < num1.length() && i-j < num2.length()){ middle += s1[j]*s2[i-j] ; } } res[i] = middle % 10; middle = middle / 10; } String str = ""; for(int i = 0,flag = 1 ; i < res.length ; i ++){ if(flag == 1 && res[res.length-1 -i] == 0) continue; else{ flag = 0; str += res[res.length-1 -i]; } } return str.equals("") ? "0" : str; }}
0 0
- LeetCode_OJ【43】Multiply Strings
- LeetCode(43) Multiply Strings
- [leetcode 43] Multiply Strings
- [LeetCode 43]Multiply Strings
- leetcode || 43、Multiply Strings
- leetcode-43 Multiply Strings
- Multiply Strings leetcode 43
- leetcode 43: Multiply Strings
- Leetcode 43 Multiply Strings
- 43Multiply 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
- [Leetcode]#43 Multiply Strings
- 43 Multiply Strings
- Css
- ubuntu14.0日志消息内容存放的位置
- mysql显示行号
- 浅析地理数据模型发展
- Alcatraz的安装和使用
- LeetCode_OJ【43】Multiply Strings
- ThinkpadE450开启Intel VT
- Java应用服务器
- c#源代码—演示类的继承性,类的多态性
- apache2.4 和php 开启gzip功能
- 电信SMGP协议,基于开源的jar文件smgpapi20100113.jar进行实现
- 判断一个产品是否是一个可配置产品的子产品
- Android Log 工具类
- 程序测试概论与单元测试详解