8ms simple C++ solution


  • 4
    L
    int m = num1.length();
            int n = num2.length();
            vector<int> results;
            for(int i=0; i < m+n; i++){
                results.push_back(0);
            }
            
            for(int i=0; i<m;i++){
                int v1=num1[m-1-i] -'0';
                for(int j=0; j <n; j++){
                    int v2 = num2[n-1-j] -'0';
                    results[i+j] += v1 * v2;
                }
            }
    
    		int carry =0;
    		for(int i=0; i < m+n; i++){
    			int value = results[i] + carry;
    			results[i] = value % 10;
    			carry = value/10;
    		}
    		
    		string re="";
    		bool findNoZero = false;
    		for(int i= m+n-1; i >=0; i--){
    			if(findNoZero){
    				re += (results[i] + '0');
    			}else{
    				if(results[i] != 0){
    					findNoZero = true;
    					re += (results[i] + '0');
    				}
    			}
    		}
    
    		if(re == "")
    			re = "0";
    		return re;

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.