国产探花免费观看_亚洲丰满少妇自慰呻吟_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;	}};


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沙湾县| 岳池县| 定安县| 宁化县| 翼城县| 子洲县| 阳春市| 昌平区| 镇坪县| 岗巴县| 新野县| 封开县| 彰化市| 青浦区| 故城县| 合作市| 兴文县| 鞍山市| 屯昌县| 苗栗县| 永新县| 日土县| 内丘县| 舟山市| 墨竹工卡县| 西丰县| 丹巴县| 千阳县| 元朗区| 开原市| 南开区| 武宁县| 玉环县| 海盐县| 宜兰市| 高雄市| 长宁县| 长治县| 建平县| 海安县| 溆浦县|