【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 ...
随机推荐
- SCWS分词扩展在WINDOWS下的安装方法
安装之前先确认您是否拥有主机的安装权限,否则无法进行安装,安装步骤如下: 1. 根据您当前用的 PHP 版本,下载相应已编译好的 php_scws.dll 扩展库. 目前支持以下版本 [PHP-4 ...
- Autolayout学习(1)-了解Autoreszing
1. 为什么要有Autoreszing? 在Xcode6之前,如果定义了下面的一个布局,同时运行在不同尺寸设备下会显示不同的效果. (iPhone6-4.7inch) (iPhone5s-4inch) ...
- Struts2 自定义拦截器
自定义拦截器(权限管理),包含了对ajax和表单请求的拦截 package com.interceptor; import java.io.IOException; import java.io.Pr ...
- mysql 定义function rand
MySQL获取随机数 如何通过MySQL在某个数据区间获取随机数? MySQL本身提供一个叫rand的函数,返回的v范围为0 <= v < 1.0. 介绍此函数的MySQL文档也介绍道 ...
- linuxMint下安装ftp工具--filezilla
windows下ftp工具有好多,linux下推荐用filezilla 安装filezilla很简单,安装完后,使用方式和windows下面一样. 第一种方式: 直接去filezilla官网下载软件包 ...
- R--基本统计分析方法(包及函数)
摘要:目前经典的统计学分析方法主要有回归分析,Logistic回归,决策树,支持向量机,聚类分析,关联分析,主成分分析,对应分析,因子分析等,那么对于这些经典的分析方法在R中的使用主要有那些程序包及函 ...
- 希望各位博友能对我的自我介绍提出意见(要面试IBM的应用开发工程师,本科应届生一枚)
面试官你好,首先我非常高兴能参加今天的面试. 我叫XXX(我名字里面有光宗耀祖),也许父母希望我光宗耀祖吧,所以给我起这样的名字.我的家乡山西太原,本科就读于XX大学,专业是信息与计算科学. 我今天要 ...
- Backbone事件模块源码分析
事件模块Backbone.Events在Backbone中占有十分重要的位置,其他模块Model,Collection,View所有事件模块都依赖它.通过继承Events的方法来实现事件的管理,可以说 ...
- git checkout -b 的详细讲解
创建分支: $ git branch mybranch 切换分支: $ git checkout mybranch 创建并切换分支: $ git checkout -b mybranch 更新mast ...
- 【Junit】JUnit-4.12使用报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing错误
下载了最新的JUnit版本,是4.12,结果尝试使用发现总是报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing这样的错误, 上网查 ...