leetCode练习(43)
2016-09-27 11:02
274 查看
题目:Multiply Strings
难度:dedium
问题描述:
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.
解题思路:
用数组模拟乘法的计算过程。面对nums1*nums2,先计算所有的temp【i】=nums1*nums2【i】,然后将每个temp【i】左边填上合适数量的0之后再相加即可。
返回时注意开头的0要舍去。
具体代码如下:
难度:dedium
问题描述:
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.
解题思路:
用数组模拟乘法的计算过程。面对nums1*nums2,先计算所有的temp【i】=nums1*nums2【i】,然后将每个temp【i】左边填上合适数量的0之后再相加即可。
返回时注意开头的0要舍去。
具体代码如下:
public class Solution { public String multiply(String num1, String num2) { if(num1.equals("0")||num2.equals("0")) return "0"; char[] str1=new char[num1.length()]; char[] str2=new char[num2.length()]; int[][] temp=new int[num2.length()][num1.length()+1]; int i; String rs=""; for(i=0;i=0;i--){ for(int j=s[i].length-1;j>=0;j--){ temp[i][(len--)-(s.length-i)]=s[i][j]; } len=temp[0].length; } for(int i=temp[0].length-1;i>=0;i--){ sum=0; for(int j=0;j=0;i--){ res[i+1]=(str[i]*m+jinwei)%10; jinwei=(str[i]*m+jinwei)/10; } res[0]=jinwei; return res; } }
相关文章推荐
- leetcode练习(43)
- leetCode练习(22)
- leetCode练习(25)
- leetCode练习(31)
- LeetCode编程练习 - Search Insert Position学习心得
- LeetCode编程练习 - Best Time to Buy and Sell Stock II学习心得
- 常见算法练习leetcode
- Leetcode43: Merge Two Sorted Lists
- Leetcode练习<十一> 整数翻转
- LeetCode练习-singleNumber
- leetcode 43 Multiply Strings (字符串相乘)
- LeetCode练习:subset
- LeetCode-43. Multiply Strings (JAVA)大整数乘法
- 数据结构与算法 LeetCode编程练习--Search and Inserted in sorted array
- LeetCode 43: First Missing Positive
- LeetCode Online Judge 题目C# 练习 - Add Binary
- leetcode 算法练习 two-sum
- LeetCode Online Judge 题目C# 练习 - Combination Sum
- LeetCode Online Judge 题目C# 练习 - Container With Most Water
- LeetCode Online Judge 题目C# 练习 - Generate Parentheses