【leetcode】Multiply Strings
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.
- class Solution {
- public:
- string multiply(string num1, string num2) {
- if(num1==""||num2=="") return "";
- while(num1[]=='') num1.erase();
- while(num2[]=='') num2.erase();
- int n1=num1.size();
- int n2=num2.size();
- int *numArr1=new int[n1];
- int *numArr2=new int[n2];
- int *result=new int[n1+n2];
- memset(result,,sizeof(int)*(n1+n2));
- for(int i=;i<n1;i++) numArr1[i]=num1[i]-'';
- for(int i=;i<n2;i++) numArr2[i]=num2[i]-'';
- for(int i=;i<n1;i++)
- {
- for(int j=;j<n2;j++)
- {
- result[i+j+]+=numArr1[i]*numArr2[j];
- }
- }
- int carry=;
- for(int i=n1+n2-;i>=;i--)
- {
- result[i]+=carry;
- if(result[i]>=)
- {
- carry=result[i]/;
- result[i]=result[i]%;
- }
- else
- {
- carry=;
- }
- }
- string resultStr;
- int k=;
- while(result[k]==) k++;
- for(int i=k;i<n1+n2;i++)
- {
- resultStr.push_back(result[i]+'');
- }
- return resultStr;
- }
- };
【leetcode】Multiply Strings的更多相关文章
- 【leetcode】Multiply Strings(middle)
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【Leetcode】【Medium】Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【leetcode】Isomorphic Strings
题目简述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...
- 【leetcode】Isomorphic Strings(easy)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 【LeetCode43】 Multiply Strings
题目描述: 解题思路: java代码: public class LeetCode43 { public static void main(String[] args) { String num1=& ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
随机推荐
- CentOS 6.5安装配置LNMP服务器(Nginx+PHP+MySQL)
CentOS 6.5安装配置LNMP服务器(Nginx+PHP+MySQL) 一.准备篇: /etc/init.d/iptables stop #关闭防火墙 关闭SELINUX vi /etc/sel ...
- springmvc请求参数获取的几种方法
1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交. /** * 1.直接把表单的参数写在Controller相应的方法的形参中 * @pa ...
- C# Monitoring-network
http://www.codeproject.com/Articles/6259/Monitoring-network-speed
- 史上最全的maven pom.xml文件教程详解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 【转】Kafka实战-Flume到Kafka
Kafka实战-Flume到Kafka Kafka 2015-07-03 08:46:24 发布 您的评价: 0.0 收藏 2收藏 1.概述 前面给大家介绍了整个Kafka ...
- Linux查看系统信息的一些命令及查看已安装软件包的命令
转自:http://cheneyph.iteye.com/blog/824746 系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看 ...
- 控制器View的生命周期及相关函数是什么?你在开发中是如何用的?
* 1.首先判断控制器是否有视图,如果没有就调用loadView方法创建:通过storyboard或者代码: * 2.随后调用viewDidLoad,可以进行下一步的初始化操作:只会被调用一次: * ...
- Access应用笔记<三>
在搭了一个数据库后,买了一本<让excel飞>,里面有提及access 经过研究之后,发现access+excel的结合确实能解决我的大部分难题,并且可以做得更好. 比较好的方法是, ac ...
- memcache安装
windows下访问 http://pecl.php.net/package/memcache/3.0.8/windows 下载对应版本memcache的dll文件添加到php目录ext下 PHP.i ...
- HDU HDU1558 Segment set(并查集+判断线段相交)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...