LeetCode算法入门- Multiply Strings -day18

  1. 题目介绍

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.

  1. 题目分析
    解题思想在于这个图:

通过上述的推导我们可以发现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;

  1. 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相关推荐

  1. LeetCode算法入门- Compare Version Numbers -day14

    LeetCode算法入门- Compare Version Numbers -day14 题目描述: Compare two version numbers version1 and version2 ...

  2. LeetCode算法入门- Longest Common Prefix -day13

    LeetCode算法入门- Longest Common Prefix -day13 题目描述: Write a function to find the longest common prefix ...

  3. LeetCode算法入门- Implement strStr() -day22

    LeetCode算法入门- Implement strStr() -day22 题目描述 Implement strStr(). Return the index of the first occur ...

  4. LeetCode算法入门- Remove Duplicates from Sorted Array -day21

    LeetCode算法入门- Remove Duplicates from Sorted Array -day21 题目描述 Given a sorted array nums, remove the ...

  5. LeetCode算法入门- Remove Element -day20

    LeetCode算法入门- Remove Element -day20 1. 题目描述 Given an array nums and a value val, remove all instance ...

  6. LeetCode算法入门- Search Insert Position -day19

    LeetCode算法入门- Search Insert Position -day19 题目描述 Given a sorted array and a target value, return the ...

  7. 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 ...

  8. LeetCode算法入门- Generate Parentheses -day16

    LeetCode算法入门- Generate Parentheses -day16 题目描述 Given n pairs of parentheses, write a function to gen ...

  9. LeetCode算法入门- Merge Two Sorted Lists -day15

    LeetCode算法入门- Merge Two Sorted Lists -day15 题目描述: Merge two sorted linked lists and return it as a n ...

最新文章

  1. 记录运行gpu错误及解决方案
  2. Jaspersoft Studio简介
  3. android给后台传递json,将服务中的JSON数据发送到Android中的UI
  4. 多个div并排居中显示
  5. asp.net表单提交方法:GET\POST介绍
  6. jdk 版本和内部版本对应_JDK 14 Rampdown:内部版本27
  7. 【读书笔记】《框架设计(第2版)CLR Via C#》中两个比较有趣的知识点
  8. PHP set_error_handler()函数的使用【转载】
  9. Java列表removeAll(Collection)示例
  10. Silverlight socket组件
  11. Linux-虚拟机使用:真机与虚拟机ping通
  12. matlab 科研绘图
  13. html输入后面消失,excel输入减号为什么会消失
  14. 【BurpSuite学习篇】四:Scanner 漏洞扫描模块
  15. 石器时代地图->魔力宝贝地图
  16. (个体户)注册公众平台步骤
  17. mysql8.0 1064_MySQL 8.0.x 用户授权报错 1064 (42000)
  18. 这就是裸金属服务器?
  19. Verilog:【7】超详细WaveDrom教程,时序图绘制利器,看这一篇就够了。
  20. DXO 评测华为p20pro说的halo effect是什么

热门文章

  1. Java BigDecimal toString()方法与示例
  2. 时间转换竟多出1年!Java开发中的20个坑你遇到过几个?
  3. Redis 快速搭建与使用
  4. 我被面试官给虐懵了,竟然是因为我不懂Spring中的@Configuration
  5. Oracle 练习题 20131017
  6. PyQt5 QTreeWidget更改item项前的展开折叠三角图标
  7. android.support.v7 fragme,打造最强RecyclerView侧滑菜单,长按拖拽Item,滑动删除Item
  8. python自己做个定时器_python 创建一个自己的类计时器
  9. 复习----使用链表实现栈(后进先出)及迭代
  10. python3.6.8卸载_CentOS7下安装python3.6.8的教程详解