LeetCode算法入门- Multiply Strings -day18
LeetCode算法入门- Multiply Strings -day18
- 题目介绍
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.
Example 1:
Input: num1 = “2”, num2 = “3”
Output: “6”
Example 2:
Input: num1 = “123”, num2 = “456”
Output: “56088”
Note:
The length of both num1 and num2 is < 110.
Both num1 and num2 contain only digits 0-9.
Both num1 and num2 do not contain any leading zero, except the number 0 itself.
You must not use any built-in BigInteger library or convert the inputs to integer directly.
- 题目分析
解题思想在于这个图:
通过上述的推导我们可以发现num1[i] * num2[j]的结果将被保存到 [i + j, i + j + 1] 这两个索引的位置。其中它们的个位数保存在 (i + j + 1) 的位置,十位数保存在 (i + j) 的位置。所以我们可以定义一个数组 pos[m + n] ,它的长度为两个数组的长度。只是它们的计算不需要把它们给反转过来。每次我们有num1[i] * num2[j]的时候先取得 pos1 = i + j, pos2 = i + j + 1。这样得到的值是
sum = num1[i] * num2[j] + pos[pos2]。按照前面的计算规则,。pos[pos1] = pos[pos1] + sum/10; pos[pos2] = sum % 10;
- Java实现
class Solution {public String multiply(String num1, String num2) {int m = num1.length();int n = num2.length();//定义这个数组来存储最后结果的每一位int[] pos = new int[m+n];//从最右边开始,所以是m-1for(int i = m -1; i >= 0; i--){for(int j = n -1; j >= 0; j--){int mul = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');int p1 = i + j;int p2 = i + j + 1;//这里进行运算,最后需要用到的是数组每个下标的值,下面不是很理解int sum = mul + pos[p2];pos[p1] = pos[p1] + sum/10;pos[p2] = sum % 10;}}StringBuilder sb = new StringBuilder();for(int p : pos){//排除高位数为0的情况if(sb.length() == 0 && p == 0){continue;}else{sb.append(p);}}//排除长度为0的情况if(sb.length() == 0)return "0";else{return sb.toString();}}
}
LeetCode算法入门- Multiply Strings -day18相关推荐
- LeetCode算法入门- Compare Version Numbers -day14
LeetCode算法入门- Compare Version Numbers -day14 题目描述: Compare two version numbers version1 and version2 ...
- LeetCode算法入门- Longest Common Prefix -day13
LeetCode算法入门- Longest Common Prefix -day13 题目描述: Write a function to find the longest common prefix ...
- LeetCode算法入门- Implement strStr() -day22
LeetCode算法入门- Implement strStr() -day22 题目描述 Implement strStr(). Return the index of the first occur ...
- LeetCode算法入门- Remove Duplicates from Sorted Array -day21
LeetCode算法入门- Remove Duplicates from Sorted Array -day21 题目描述 Given a sorted array nums, remove the ...
- LeetCode算法入门- Remove Element -day20
LeetCode算法入门- Remove Element -day20 1. 题目描述 Given an array nums and a value val, remove all instance ...
- LeetCode算法入门- Search Insert Position -day19
LeetCode算法入门- Search Insert Position -day19 题目描述 Given a sorted array and a target value, return the ...
- LeetCode算法入门- Remove Nth Node From End of List -day17
LeetCode算法入门- Remove Nth Node From End of List -day17 题目解释: Given a linked list, remove the n-th nod ...
- LeetCode算法入门- Generate Parentheses -day16
LeetCode算法入门- Generate Parentheses -day16 题目描述 Given n pairs of parentheses, write a function to gen ...
- LeetCode算法入门- Merge Two Sorted Lists -day15
LeetCode算法入门- Merge Two Sorted Lists -day15 题目描述: Merge two sorted linked lists and return it as a n ...
最新文章
- 记录运行gpu错误及解决方案
- Jaspersoft Studio简介
- android给后台传递json,将服务中的JSON数据发送到Android中的UI
- 多个div并排居中显示
- asp.net表单提交方法:GET\POST介绍
- jdk 版本和内部版本对应_JDK 14 Rampdown:内部版本27
- 【读书笔记】《框架设计(第2版)CLR Via C#》中两个比较有趣的知识点
- PHP set_error_handler()函数的使用【转载】
- Java列表removeAll(Collection)示例
- Silverlight socket组件
- Linux-虚拟机使用:真机与虚拟机ping通
- matlab 科研绘图
- html输入后面消失,excel输入减号为什么会消失
- 【BurpSuite学习篇】四:Scanner 漏洞扫描模块
- 石器时代地图->魔力宝贝地图
- (个体户)注册公众平台步骤
- mysql8.0 1064_MySQL 8.0.x 用户授权报错 1064 (42000)
- 这就是裸金属服务器?
- Verilog:【7】超详细WaveDrom教程,时序图绘制利器,看这一篇就够了。
- DXO 评测华为p20pro说的halo effect是什么
热门文章
- Java BigDecimal toString()方法与示例
- 时间转换竟多出1年!Java开发中的20个坑你遇到过几个?
- Redis 快速搭建与使用
- 我被面试官给虐懵了,竟然是因为我不懂Spring中的@Configuration
- Oracle 练习题 20131017
- PyQt5 QTreeWidget更改item项前的展开折叠三角图标
- android.support.v7 fragme,打造最强RecyclerView侧滑菜单,长按拖拽Item,滑动删除Item
- python自己做个定时器_python 创建一个自己的类计时器
- 复习----使用链表实现栈(后进先出)及迭代
- python3.6.8卸载_CentOS7下安装python3.6.8的教程详解