LeetCode 43. Multiply Strings_git

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;
	}
};