LeetCode Multiply Strings

IQA图像质量评价 数据集介绍(LIVE、TID2013、CSIQ、LIVEC、KonIQ-10K) LIVE、TID2013、CSIQ、LIVEC、KonIQ-10K 数据集介绍 阅读详情

原题链接在这里:https://leetcode.com/problems/multiply-strings/

Method 1: 通过这道题,学到了BigInteger的用法, 他的constructor 可以直接从string建立BigInteger, 但要注意它的乘法API是 bi1.multiply(bi2).

若是中间需要考虑溢出,还有bi1.intValue(), bi1.longValue()等API.

Method 2: 若num1.length() = m, num2.length() = n, 那么结果的长度为m+n-1(没有进位),或者是m+n(有进位)。没有进位时, 结果对应的i位等于 (num1的1位 * num2的i位 + num1的2位 * num2的i-1位 + ... + num1的i位 * num2的1位)%10. 当然 前提是不能有溢出。为了方便计算与理解,可以先反转两个string,最后计算的结果的most significant位在最后,所 以需要反转回来。

AC Java:

import java.math.*;
public class Solution {
    public String multiply(String num1, String num2) {
        /*Method 1
        if(num1 == null || num2 == null || num1.length() ==0 || num2.length() == 0)
            return "";
        BigInteger bi1 = new BigInteger(num1);
        BigInteger bi2 = new BigInteger(num2);
        
        BigInteger res = bi1.multiply(bi2);
        return res.toString();
        */
        
        //Method 2
        if(num1 == null || num2 == null || num1.length() ==0 || num2.length() == 0)
            return "";
        if(num1.charAt(0) == '0'||num2.charAt(0) == '0'){
            return "0"; //error
        }
        
        int len1 = num1.length();
        int len2 = num2.length();
        num1 = new StringBuilder(num1).reverse().toString(); //error
        num2 = new StringBuilder(num2).reverse().toString();
        StringBuilder res = new StringBuilder();
        
        int carryDig = 0;
        for(int i = 0; i < len1+len2-1; i++){
            int mul = 0;
            for(int j = 0; j<=i; j++){
                if(j<len1 && (i-j)<len2){
                    mul += (int)(num1.charAt(j)-'0') * (int)(num2.charAt(i-j)-'0');
                }   
            }
            mul += carryDig;
            res.append(mul%10);
            carryDig = mul/10;
        }
        
        if(carryDig != 0){
            res.append(carryDig);
        }
        
        return res.reverse().toString();
    }
}


squidpy 教程 2:利用 Scanpy 和 Squidpy 进行空间数据的可视化分析 本教程详细介绍了如何利用Squidpy和Scanpy对空间转录组数据进行可视化分析。通过将Scanpy处理得到的聚类结果映射回原始组织图像,并结合基因表达与空间邻域信息,帮助研究者直观揭示细胞类型分布、基因表达模式及空间相互作用,从而深入挖掘空间组学数据的生物学意义。 阅读详情

相关推荐

信捷 XDH Ethercat 正负极限配置详解与实战技巧

本文详细解析了信捷XDH系列PLC通过EtherCAT总线配置伺服电机正负硬极限的完整流程与实战技巧。内容涵盖硬件开关选型安装、EtherCAT IO信号映射、软件参数设置(包括关键的极性配置)、调试验证方法,以及硬极限与软极限的协同策略。重点阐述了如何避免常见配置错误,确保运动控制系统的最后一道物理安全防线可靠有效,为工程师提供从理论到实践的全面指导。

wdx012345的博客 601

LeetCode力扣之 Multiply Strings

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.Note:The length of both num1 and num2 is &lt; 110.Both num1 and num2 contains only digits 0-9....

LeeWei 219

Nvidia Tesla A100 驱动安装方法

Nvidia Tesla A100 驱动安装方法 遇到的问题:以前安装tesla系列、geforce系列等gpu 驱动的时候,只需执行nvidiaxxxx.run驱动包或者安装nvidiaxxxx.rpm包即可,但遇到新的GPU Nvidia Tesla A100的时候,安装完驱动似乎不起作用,找了半天资料,原来需要以下步骤才可以使用;

qq_24343177的博客 2万+

LeetCode 刷题记录 43. Multiply Strings

题目: 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” Exampl...

dldldl1994的博客 235

(超全面,超基础)Kriging插值推导理论笔记,算法,普通克里金

最近老师让我再重新推导一下克里金插值的方法如何求出预测结果,于是我又无数次地打开了大神的推导原理过程链接????:克里金(Kriging)插值的原理与公式推导 作者:xg1990,但是不可避免地在推导过程中,数学知识基础薄弱的我卡住在一些公式推导的步骤中。 所以就想基于上面大神的推导过程,将里面的推导写得更具体些,当然觉得毫不吃力可以进行推导的大神们可以不看我的注释,直接点开链接看就好。 我看到csdn中很多关于克里金推导的博客里面的内容直接截屏或者复制了上面链接里大神发表的内容,也不注

Olivel1997的博客 1万+

永久关闭Windows更新的5种方法

很多家用电脑,如果系统自动更新的话,会变得越来越卡顿,且硬件型号兼容也并不完美。那么我们该如何彻底关闭Win11的自动更新呢?以下准备了5种方法,您可以根据自身实际情况选择合适的方法!

qq_43652793的博客 1万+

LeetCode | # 43. Multiply Strings

43. Multiply Strings Description 描述:https://leetcode.com/problems/multiply-strings/description/ 题意:将字符串表示的两个数相乘计算结果,不可调用大整数包或直接转为整数相乘。 Solution 1: (Java) class Solution { public String multiply(St...

Karmen199x的博客 275

【近源攻击】badusb制作

BadUSB是利用伪造HID设备执行攻击载荷的一种攻击方式。HID(Human InterfaceDevice)设备通常指的就是键盘鼠标等与人交互的设备,用户插入BadUSB,就会自动执行预置在固件中的恶意代码。BadUsb插入后,会模拟键盘鼠标对电脑进行操作,通过这些操作打开电脑的命令终端,并执行一条命令,这条命令将从指定网址下载其他代码并于后台静默运行。这些代码功能包括:窃取信息、反弹shell、发送邮件等,从而实现控制目标机或者窃取信息的目的。

qinjilll的博客 2546

[leetcode javascript解题]Multiply Strings

leetcode 第43题“Multiply Strings”描述如下: 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.

zzxboy1的博客 647

SpringBoot实现快速导入EXCEL

MaterialMonitorController.java @Log(title = "物料监测", businessType = BusinessType.IMPORT) @RequiresPermissions("system:materialMonitor:import") @PostMapping("/importData") @ResponseBody ...

LEN程序员的一生 4817

【主题建模】一种基于深度学习的主题建模方法:BERTopic(理论篇)

在我的博客中已经写了很多关于主题建模的内容,当你准备了解 BERTopic 时,默认你已经知道了 LSA、pLSA、NFM、LDA 等传统的主题建模方法。关于主题建模的前置知识我在这里不做赘述,感兴趣的同学可以看看我前几篇博客。学习 BERTopic 需要一定的机器学习基础,让我们一起开始吧!

Data · Model · Agent · Repeat 9892

LeetCode 力扣 43. 字符串相乘 multiply-strings

43. 字符串相乘 题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。 样例 示例 1: 输入: num1 = "2", num2 = "3" 输出: "6" 示例 2: 输入: num1 = "123", num2 = "456" 输出: "56088" 说明: num1 和 num2 的长度小于110。 num1 和 num2 只包含数字 0-9。 num1 和 num2 均不以零开头,除非是数字 0 本身。

motongxue的博客 354

Plant Simulation与ABB机器人虚实结合

Plant Simulation与ABB机器人虚实结合

santayana的专栏 1869

LeetCode: 43. Multiply Strings

LeetCode: 43. Multiply Strings

杨领well的专栏 978

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Leetcode 43. Multiply Strings (python+cpp)

Leetcode 43. Multiply Strings题目解法: 题目 解法: class Solution: def multiply(self, num1: str, num2: str) -> str: if num1=='0' or num2=='0': return '0' product = [0]*(len(num1)+len(num2)) pos = len(product)-1

qq_37821701的博客 366

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

Leetcode: Multiply Strings

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. 方法一:模拟手算乘法 string addTwoString(strin

ZkvIA的博客 8025

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

Leetcode: 43. Multiply Strings

Leetcode: 43. Multiply Strings 题目难度中等,想到分别遍历num1、num2中的数值,但是具体细节没有理清。参考:https://discuss.leetcode.com/topic/30508/easiest-java-solution-with-graph-explanation?page=1 public...

哎呀妈呀脑壳疼脑壳疼 194

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

hongjie_lin 264

leetcode 43. Multiply Strings

leetcode 43. Multiply Strings 字符串相乘  不简单啊。。。 参考了 String 、StringBuffer、StringBuilder 之间的区别 public class Solution { public static void main(String[] arg){ String a = "999"; String b = "9

独钓寒江雪 416

【大数相乘】LeetCode 43. Multiply Strings

LeetCode 43. Multiply Strings Solution1:我的答案 作为一个hard题,提交一次就过真是让我hin开心啊!!! 就是方法有点笨。。 class Solution { public: string multiply(string num1, string num2) { if (num1 == &amp;quot;0&amp;quot; || num2 == &amp;quot;0&amp;quot;...

Allenlzcoder的博客 464

LeetCode 43. Multiply Strings (Java版; Medium)

welcome to my blog LeetCode Top Interview Questions 43. Multiply Strings (Java版; Medium) 题目描述 Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2,...

littlehaes的博客 243

Leetcode1-100: 43. Multiply Strings

Leetcode1-100: 43. Multiply Strings问题描述解题思路代码实现分析后记 问题描述 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

Y123iwantit的博客 250

leetcode-multiply strings

这道题就是大数运算。 lexi's的想法很好,在操作之前先把num1和num2给逆置,方便操作。 Tenos的文章通过一张图把计算过程直观的展示出来。 1 class Solution { 2 public: 3 string multiply(string num1, string num2) { 4 int m = n...

dongkai0918的博客 185

leetcode multiply-strings

题干: Given two non-negative integersnum1andnum2represented as strings, return the product ofnum1andnum2, also represented as a string. Example 1: Input: num1 = "2", num2 = "3" Output: "...

dibanfu3470的博客 200

Leetcode 43 Multiply Strings

Leetcode 43 Multiply Strings 题目描述 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",...

qq_43271202的博客 245

[LeetCode] Multiply Strings

[Problem] 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. [Analysis] 大数乘法,不要忘了最后一个进位,注意删除前

1.02^365的成长裂变 3612
上一篇: Java: arr==null vs arr.length==0
下一篇: Interview Return Products of All Other Elements in Array
Dylan_Java_NYC
博客等级 码龄11年 1粉丝 134原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值