LeetCode算法入门- Multiply Strings -day18

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();}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/558851.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux下查看版本及系统信息

一、查看Linux发行版本 [rootlocalhost ~]# cat /etc/issue CentOS release 6.8 (Final) Kernel \r on an \m[rootlocalhost ~]# cat /etc/redhat-release CentOS release 6.8 (Final)二、查看Linux内核信息 [rootlocalhost ~]# uname -a Linux localhost.localdomain 2.6.32…

LeetCode算法入门- Search Insert Position -day19

LeetCode算法入门- Search Insert Position -day19 题目描述 Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array.…

Linux——VMware虚拟机安装CentOS步骤

一、下载CentOS.iso镜像 最地道的下载方式就是通过官网&#xff0c;大多数的网上连接会直接抛出网易、华为的镜像连接&#xff0c;实际上这些连接都可以在官网找到&#xff1a; 官网地址&#xff08;可直接百度搜索CentOS&#xff09;&#xff1a;https://www.centos.org/ 1…

LeetCode算法入门- Remove Element -day20

LeetCode算法入门- Remove Element -day20 1. 题目描述 Given an array nums and a value val, remove all instances of that value in-place and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array i…

Spring Boot——Redis安装配置与应用整合

引言 Spring Boot默认以ConcurrentHashMap作为缓存容器&#xff0c;但默认的缓存容器在简单的场景使用还是可以的&#xff0c;而作为NoSQL的代表&#xff0c;Redis可以做内存数据库、消息中间件都是不错的&#xff0c;而且有RedisDesktopManager作为可视化管理工具&#xff0c…

利用Aria2高速下载网盘文件

利用Aria2高速下载网盘文件 方法步骤&#xff1a; 下载文件 解压arial2&#xff0c;运行aria2启动.VBS添加插件&#xff0c;解压BaiduExporter-master.zip在Google浏览器扩展程序中chrome://extensions加载已经解压的扩展程序 选择BaiduExporter进行添加即可&#xff0c;打开…

MySQL——JSON_REPLACE()函数修改JSON属性值

引言 由于对mysql的函数并不了解&#xff0c;之前遇到了一个场景&#xff1a; mysql表中有一个字段res_content 是一个由longtext类型&#xff08;可以理解为一个更长的varchar&#xff09;保存的巨大的JSON对象&#xff0c;但是&#xff0c;由于录入的疏忽&#xff0c;导致这…

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

LeetCode算法入门- Remove Duplicates from Sorted Array -day21 题目描述 Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do …

Spring Boot整合Redis——自定义RedisSerializer

引言 spring boot简单引入redis依赖&#xff0c;并使用RedisTemplate进行对象存储时&#xff0c;需要使存储对象实现Serializable接口&#xff0c;这样才能够成功将对象进行序列化。 RedisTemplate默认使用的序列化机制是JdkSerializationRedisSerializer&#xff0c;但实际开…

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

LeetCode算法入门- Implement strStr() -day22 题目描述 Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack “hello”, needle “ll” Output: 2 Example …

交易系统如何确保账簿100%准确

转自廖雪峰老师的《交易系统如何确保账簿100%准确》 这篇文章阐述了一个交易系统中对账功能的关键&#xff0c;即&#xff1a;时刻保证资产负债表总额始终为 0。 交易系统中&#xff0c;对账是一个大问题。对账处理不好&#xff0c;不但需要花费大量的人力去处理账簿&#xff…

通俗易懂的SpringBoot教程---day1---Springboot入门教程介绍

通俗易懂的SpringBoot教程—day1—教程介绍 教程介绍&#xff1a; 初级教程&#xff1a; 一、 Spring Boot入门 二、 Spring Boot配置 三、 Spring Boot与日志 四、 Spring Boot与Web开发 五、 Spring Boot与Docker&#xff1a;Docker容器 六、 Spring Boot与数据访问&#x…

重装win11 23H2系统步骤及错误总结

1.分出大约13GB 的启动盘&#xff0c;下载win11 mul映像并解压进去 &#xff08;记得下载电脑相应的网卡驱动&#xff01;&#xff08;只要等下进入桌面要用&#xff09;&#xff09; 2.利用easybcd&#xff0c; 到add选项&#xff0c;点击Win1PE&#xff0c;命名启动项&…

Java 8中获取参数名称

本文转自廖雪峰老师的&#xff1a;《在Java 8中获取参数名称》 在Java 8之前的版本&#xff0c;代码编译为class文件后&#xff0c;方法参数的类型是固定的&#xff0c;但参数名称却丢失了&#xff0c;这和动态语言严重依赖参数名称形成了鲜明对比。现在&#xff0c;Java 8开始…

通俗易懂的SpringBoot教程---day2---Springboot配置文件

通俗易懂的SpringBoot教程—day2—Springboot配置文件 1、配置文件 SpringBoot使用一个全局的配置文件&#xff0c;配置文件名是固定的&#xff1b; •application.properties •application.yml 配置文件的作用&#xff1a;修改SpringBoot自动配置的默认值&#xff1b;Spring…

英语中数字表达总结

引言 对于英语中千、万、亿的表达法&#xff0c;始终是我英语路上的一个痛点&#xff0c;今天就来好好总结一下这东西的规律。 一句话总结 阅读和理解层面&#xff08;99%的使用情况&#xff09;&#xff1a;thousand后面跟 3 个 0 &#xff0c;million后面跟 6 个 0 &#…

Kibana 的安装(Windows版本)新手入门

Kibana 的安装&#xff08;Windows版本&#xff09;新手入门 参考博文&#xff1a;https://blog.csdn.net/weixin_34727238/article/details/81200071 目录 什么是Kibana? Kibana 6.3.1安装条件 JDK的安装 node的安装 Elasticsearch的安装 Kibana 的安装 什么是Kibana?…

价值50万年薪的Java面试题

《Java面试题全集&#xff08;上&#xff09;》 《Java面试题全集&#xff08;中&#xff09;》 《Java面试题全集&#xff08;下&#xff09;》 《关于Java并发编程的总结和思考》 《面试编程题拾遗&#xff08;01&#xff09; --- 不用算术运算符完成两个数求和》 《面试…

Could not resolve host: 'localhost 报错解决办法

Could not resolve host: localhost 报错解决办法 面向Windows的&#xff1a; 零基础的我一直卡在这一步骤下&#xff1a; 首先要先在Windows安装curl&#xff1a;安装方式参考&#xff1a;https://blog.csdn.net/weixin_41986096/article/details/86646365 按照完之后&…

当面试官问我————为什么String是final的?

面试官&#xff1a;你好&#xff0c;能看得清下面这张图吗&#xff1f; 我&#xff1a;可以的。 面试官&#xff1a;恩&#xff0c;好的。呃&#xff0c;你能不能说一说为什么String要用final修饰&#xff1f; 我&#xff1a;final意味着不能被继承或者被重写&#xff0c;Str…