LeetCode刷题实战(43):Multiply Strings

题目描述:

43Multiply Strings28.7%Medium

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:

  1. The length of both num1 and num2 is < 110.
  2. Both num1 and num2 contain only digits 0-9.
  3. Both num1 and num2 do not contain any leading zero, except the number 0 itself.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

C语言解法:


char* multiply(char* num1, char* num2){if(*num1=='0' || *num2=='0')return "0";int len_num1 = strlen(num1);int len_num2 = strlen(num2);int len = len_num1 + len_num2;int *arr = (int*)malloc(sizeof(int)*len); //存储计算结果;memset(arr, 0, sizeof(int)*len); //重要;for(int i=len_num1-1; i > -1; i--)for(int j=len_num2-1; j > -1; j--)arr[i+j+1] += (num1[i]-'0')*(num2[j]-'0'); //计算每一位结果;for(int i=len-1; i > 0; i--) {arr[i-1] += arr[i]/10;arr[i] %= 10;}char *s = (char*)malloc(sizeof(char)*(len+1)); //数字转换为字符串;int index = 0;int i = 0;if(arr[i]==0) i++;while(i < len)s[index++] = arr[i++]+'0';s[index] = '\0';return s;
}

运行结果:

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

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

相关文章

【2018山东省赛 - A】Anagram(贪心,费用流,KM算法)

题干&#xff1a; Problem Description Orz has two strings of the same length: A and B. Now she wants to transform A into an anagram of B (which means, a rearrangement of B) by changing some of its letters. The only operation the girl can make is to “incr…

【2018ACM山东省赛 - B】Bullet(二分 + 二分图匹配,匈牙利算法,卡常)

题干&#xff1a; Problem Description In GGO, a world dominated by gun and steel, players are fighting for the honor of being the strongest gunmen. Player Shino is a sniper, and her aimed shot kills one monster at a time. Now she is in an nnn \times nnn m…

13.Data Leakage

本教程是ML系列的一部分。在此步骤中&#xff0c;你将学习什么是data leakage及如何预防它。 What is Data Leakage 数据泄漏是数据科学家需要理解的最重要问题之一。 如果您不知道如何防止它&#xff0c;则会频繁出现泄漏&#xff0c;并且会以最微妙和危险的方式破坏您的模…

0.Overview----Machine Learning

本文为Kaggle Learn的Machine Learning课程的中文翻译&#xff0c;原文链接为&#xff1a;https://www.kaggle.com/learn/machine-learning 1.How Models Work The first step if youre new to machine learning 2.Explore Your Data Load data and set up your environment …

【2018ACM山东省赛 - C】Cities(最小生成树变形优化,贪心思维)

题干&#xff1a; Problem Description There are nnn cities in Byteland, and the ithi_{th}ith​ city has a value aia_iai​. The cost of building a bidirectional road between two cities is the sum of their values. Please calculate the minimum cost of connec…

项目总结1:微信扫码自动识别设备类型并跳转到相应的应用下载页面(apk或App Store)之解决方案

问题分析&#xff1a;普通页面一般无法调用微信的扫一扫接口&#xff0c;从而否定通过微信扫一扫功能给我们判断当前扫码的设备类型。 解决方案&#xff1a;通过应用下载页面自身来获取当前访问的客户端设备类型&#xff08;iPhone、Android、iPad&#xff09;&#xff0c;然后…

《TCP/IP详解》学习笔记(六):UDP 协议

UDP 简要介绍 UDP 是传输层协议&#xff0c;和 TCP 协议处于一个分层中&#xff0c;但是与 TCP 协议不同&#xff0c;UDP 协议并不提供超时重传&#xff0c;出错重传等功能&#xff0c;也就是说其是不可靠的协议。 UDP 协议头 1UDP 端口号 由于很多软件需要用到 UDP 协议&am…

【2018ACM山东省赛 - E】Sequence(树状数组,思维,优化)

题干&#xff1a; We define an element aia_iai​ in a sequence "good", if and only if there exists a j(1≤j<i)j(1\le j < i)j(1≤j<i) such that aj<aia_j < a_iaj​<ai​. Given a permutation ppp of integers from 111 to nnn. Remove …

Apollo自动驾驶入门课程第②讲 — 高精地图

目录 1. 高精地图与传统地图 2. 高精地图与定位、感知规划的关系 2.1 高精地图用于定位 2.2 高精地图用于感知 2.3 高精地图用于规划 3. Apollo高精度地图与构建 3.1 Apollo高精地图 3.2 Apollo高精地图的构建 本文转自微信公众号&#xff1a; Apollo开发者社区 原创&a…

项目总结2:ionic3开发跨平台App如何设置和替换应用图标及启动图

前言&#xff1a; 和原生开发一样&#xff0c;ionic官方提供的设置方式也很简单&#xff0c;只不过多了一个步骤&#xff1a;基于ionic命令的方式自动修改全局的配置文件config.xml。 设置或替换应用图标和应用启动图&#xff1a; 把UI提供的图标拿过来改成特定的名称"i…

【2018ACM山东省赛 - G】Games(Nim博弈 + dp)

题干&#xff1a; Problem Description Alice and Bob are playing a stone game. There are nnn piles of stones. In each turn, a player can remove some stones from a pile (the number must be positive and not greater than the number of remaining stones in the …

LeetCode刷题实战(13):Roman to Integer

题目描述&#xff1a; 13 Roman to Integer 49.5%Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D …

项目总结3:ionic3开发的App在启动过程中部分手机出现白屏或黑屏问题之终极解决方案

问题场景&#xff1a;采用ionic3开发的App&#xff0c;当项目比较大的时候&#xff0c;会出现部分真机设备在启动的过程中有白屏或黑屏的情况。 原因预测&#xff1a;个别手机&#xff0c;尤其是安卓手机的性能比较差&#xff0c;App在启动后进入首页或登录页前的初始化工作还…

1.Intro to Deep Learning and Computer Vision

Intro 这是Kaggle深度学习教育课程的第一课。 在本课程结束后&#xff0c;您将了解卷积。 卷积是计算机视觉&#xff08;以及许多其他应用程序&#xff09;中深度学习模型的基本构建块。 之后&#xff0c;我们将很快开始使用世界一流的深度学习模型。 Lesson [1] from IPy…

【POJ - 2253】Frogger(floyd,或 部分瓶颈生成树的最大边)

题干&#xff1a; 湖中有n块石头&#xff0c;编号从1到n&#xff0c;有两只青蛙&#xff0c;Bob在1号石头上&#xff0c;Alice在2号石头上&#xff0c;Bob想去看望Alice&#xff0c;但由于水很脏&#xff0c;他想避免游泳&#xff0c;于是跳着去找她。但是Alice的石头超出了他…

SpringMVC常用的视图接口分类及实现类

SpringMVC中常用的视图接口分类及对应的实现类&#xff1a; URL资源视图&#xff1a;InternalResourceView、JstlView 文档视图&#xff1a;AbstractExcelView、AbstractPdfView 报表视图&#xff1a;ConfigurableJsperReportsView等JasperReports报表技术的视图 JSON视图&…

2.Building Models from Convolutions

Intro 这是深度学习课程的第2课 在本课程结束时&#xff0c;您将了解如何将卷积结合起来&#xff0c;以实现计算机视觉中的超人成就。 Lesson [1] from IPython.display import YouTubeVideo YouTubeVideo(ToBPiUlLFEY, width800, height450) Keep Going 现在你理解了模…

*【POJ - 2796】 Feel Good (前缀和优化+单调栈维护)

题干&#xff1a; Feel Good Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 12409 Accepted: 3484Case Time Limit: 1000MS Special Judge Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedi…

SpringMVC配置视图的直接映射view-controller命名空间

通常情况下&#xff0c;如果直接通过url来访问具体的视图会报404错误&#xff0c;这个时候最容易想到的解决办法是通过转发或重定向机制&#xff0c;也就说走一遍目标控制器方法拦截一次。但是最好的方法是配置视图的直接映射关系。 <mvc: view-controller path"url访…

【nyoj-456】 邮票分你一半 (dp,0-1背包的中点问题)

题干&#xff1a; 邮票分你一半 时间限制&#xff1a;1000 ms | 内存限制&#xff1a;65535 KB 难度&#xff1a;3 描述 小珂最近收集了些邮票&#xff0c;他想把其中的一些给他的好朋友小明。每张邮票上都有分值&#xff0c;他们想把这些邮票分成两份&#xff0c;并且使…