【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 ...
随机推荐
- ES6箭头函数
1. 无参数 var sum = () => 1 + 2; // 等同于: var sum = function() { return 1 + 2; } 2. 返回单个值 var reflect ...
- android-获取当前屏幕尺寸信息
方法有两种一: DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMet ...
- linux 打造man中文帮助手册
博客转自:http://my.oschina.net/hbzhangmao/blog/354533 学IT的同学都知道, Linux是一个好东西, 但初学者往往会因为太多的命令觉得头疼, 更头疼的是所 ...
- php 选择排序法
private function demo(array $arr = array()){ $len = count($arr); if ($len == 1) return $arr; else fo ...
- Maven初级学习(二)Maven使用入门
序,学习配置pom.xml,利用maven生成eclipes项目. 一.编写POM POM Project Obejct Model,项目对象模型. 编写pom.xml,新建文件夹hello-worl ...
- ios如何普安短图片类型
很多时候需要知道服务器返回的图片是.png还是.jpg或者是.git, 两种方式 1,获取扩展名 //图片 NSString *image = @"4351141241.GIT&quo ...
- 怎样给WordPress分配更多的内存
WordPress如果内存不够,你在操作的时候,就会碰到像这样的问题”Allowed memory size of xxxxxx bytes exhausted”(允许的内存 xxxx 字节已经用光了 ...
- 软件测试-----Graph Coverage作业
/******************************************************* * Finds and prints n prime integers * Jeff ...
- javascript基础07
javascript基础07 1.节点 元素.childNodes : 属性 只读 属性 子节点列表集合 元素.childNodes 只包含子节点,不包含孙节点 DOM节点的类型有很多种,w3c标准有 ...
- 在Fedora 20 上安装Mysql并初始化root密码
[root@localhost ~]# yum -y install community-mysql-server #安装数据库 已加载插件:langpacks, refresh-packagekit ...