国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

LeetCode 43. Multiply Strings

2019-11-08 02:48:25
字體:
來源:轉載
供稿:網友

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

Note:

The length of both num1 and num2 is < 110.Both num1 and num2 contains only digits 0-9.Both num1 and num2 does not contain any leading zero.You must not use any built-in BigInteger library or convert the inputs to integer directly.answer:

class Solution {public:	string multiply(string num1, string num2) {		int allLen = num1.length() + num2.length();		string all(allLen,'0'); //initial		if(num1.length() > num2.length()){			string temp = num1;			num1 = num2;			num1 = temp;		}		for(int i = num1.length() - 1; i >= 0; i --){			int carry = 0,mul = 0,final = 0,index = 0;			int j = 0;			for(j = num2.length() - 1; j >= 0; j --){				mul = (num2[j] - '0') * (num1[i] - '0');				final = mul + (all[i + j + 1] - '0') + carry;				all[i + j + 1] = final % 10 + '0';				carry = final / 10;			}			while(carry != 0){				final = (all[i + index] - '0') + carry;				all[i + index] = final % 10 + '0';				carry = final / 10;				index --;			}		}		int index = 0;		while(all[index] - '0' ==0) index ++;		all = all.substr(index,allLen - index);		if(all.length() == 0) return "0";		return all;	}};


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沾化县| 温州市| 大港区| 阜城县| 额尔古纳市| 荆门市| 怀化市| 江油市| 伊金霍洛旗| 张掖市| 桐梓县| 益阳市| 武乡县| 泰兴市| 莎车县| 夏津县| 平阳县| 哈密市| 南充市| 东光县| 错那县| 阿拉善右旗| 普格县| 大邑县| 同心县| 万源市| 鄂尔多斯市| 息烽县| 土默特左旗| 柞水县| 文水县| 霍邱县| 三明市| 永宁县| 神农架林区| 沅江市| 马公市| 公安县| 壤塘县| 临邑县| 五莲县|