https://oj.leetcode.com/problems/multiply-strings/

用字符串实现大数乘法,细节题,细节很多

class Solution {
public:
string multiply(string num1, string num2) {
if(num1.empty() || num1.size() == || num2.empty() || num2.size() == )
return "";
//
if(num1 == "" || num2 == "")
return ""; //keep num2 the smaller
if(num1.size() < num2.size())
{
//exchange
string t = num1; num1 = num2; num2 = t;
}
string ans = "";
ans.resize(num1.size()); //initialize
for(size_t i = ; i < num1.size(); i++)
ans[i] = ''; int pos = ;
int times = ;
int sum = ; for(int index2 = num2.size() - ; index2 >= ; index2--)
{
pos = ans.size() - - times;
times++;
int carry = ;
for(int index1 = num1.size() - ; index1 >=; index1--)
{
sum = carry + (num1[index1] - '')*(num2[index2] - '') + ans[pos] - '';
carry = sum/;
ans[pos] = sum% + '';
pos--;
// ans has been the beginning while index1 not, and ans has been the beginning and here are carry
if(pos == - &&(carry > || index1 != ))
{
string t = "";
t[] = carry + '';
ans = t + ans;
carry = ;
pos = ;
}
}
}
return ans;
}
};

LeetCode OJ--Multiply Strings **的更多相关文章

  1. 【leetcode】Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

  2. [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)

    转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...

  3. LeetCode 043 Multiply Strings

    题目要求:Multiply Strings Given two numbers represented as strings, return multiplication of the numbers ...

  4. leetcode:Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  5. [LeetCode] 43. Multiply Strings 字符串相乘

    Given two non-negative integers num1 and num2represented as strings, return the product of num1 and  ...

  6. LeetCode(43. Multiply Strings)

    题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...

  7. Java for LeetCode 043 Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  8. 【leetcode】Multiply Strings(middle)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  9. Java [Leetcode 43]Multiply Strings

    题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...

  10. leetcode:Multiply Strings(字符串的乘法)【面试算法题】

    题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...

随机推荐

  1. 学习pytho第l六天 常用字符串用法

    name='my name is dream' print(name.capitalize())#首字母大写 print(name.count(‘’a‘’))#判断字符串里有多少个a print(na ...

  2. Essential C++ 3.1 节的代码练习——指针方式

    // // PointerToValue.cpp // Working // // Created by Hawkins, Dakota Y on 6/3/16. // Copyright 2016 ...

  3. COGS:313. [POI2001] 和平委员会

    313. [POI2001] 和平委员会 ★★☆   输入文件:spo.in   输出文件:spo.out   评测插件时间限制:1 s   内存限制:128 MB 题目描述 根据宪法,Bytelan ...

  4. Redis实现之数据库(一)

    服务器中的数据库 Redis服务器将所有数据库都保存在服务器状态redis.h/redisServer结构体的db数组中,db数组的每个项都是一个redis.h/redisDb结构体,每个redisD ...

  5. 在Foxmail中添加阿里云企业邮箱账号

    1.安装完成Foxmail之后,新建账号 输入阿里云邮箱地址和密码,点击创建 接受服务器类型你可以选择POP3或者IMAP,在这里我选择的是POP3 点击创建,大功告成. 为什么要写这篇文章呢? 因为 ...

  6. privoxy+ss5实现 HTTP 代理协议转socks5代理

    一.系统准备资源         二.ss5安装部署 1.SOCK5代理服务器部署环境准备 IP:10.0.0.100 官网: http://ss5.sourceforge.net/ 下载 yum - ...

  7. Android资源限定符

    Android系统会根据设备参数,自动选择最佳资源配置方案. Android中常见的资源限定符: 屏幕特征 限定符 描述 大小 small 提供给小屏幕设备的资源 normal 提供给中等屏幕设备的资 ...

  8. netcfg.exe

    netcfg.exe 编辑 本词条缺少信息栏.名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧!   目录 1 简介 2 可能出现问题 简介编辑 netcfg.exe是Kaspersky的 ...

  9. IOS开发---菜鸟学习之路--(六)-UITableView几个方法的使用说明

    对于UITableView的基础使用我这边就不做重复介绍了 我重点就来介绍下如何实现大部分新闻的界面.也就是第一条记录显示大图片下面加一段文字说明 然后剩下来的内容全部显示为文字图片的格式 其实要做到 ...

  10. 33、secret

    CemFjee9rueuoeeQhgkxCee9kee7nO+8jOW6lOeUqO+8jOaVsOaNruW6k+acjeWKoeeahOWFs+mUruS/oeaBr++8iOWmgiDmnI3l ...