My simple solution in C#


  • 0
    P
            /// <summary>
            /// Method to multiply two large integer values. 
            /// This accepts the arguments in the form of strings 
            /// and return the product in string format only.
            /// </summary>
            /// <param name="num1">first value to multiply</param>
            /// <param name="num2">second value to multiply</param>
            /// <returns>product of two large given values</returns>
            public string Multiply(string num1, string num2)
            {
                if (num1 == "0" || num2 == "0") return "0";
    
                int num1_len = num1.Length;
                int num2_len = num2.Length;
    
                // it will initialize the result with all zeros 
                // with the length as sum of both given number length.
                char[] result = new char[num1_len + num2_len];
                for (int k = 0; k < result.Length; k++)
                {
                    result[k] = '0';
                }
    
                int remainder = 0;
                int placeSpecifier = result.Length - 1;
                // starting from the right most index.
                int initialize = result.Length - 1;
    
                for (int i = num1_len - 1; i >= 0; i--)
                {
                    int multiplier = int.Parse(num1[i].ToString());
    
                    for (int j = num2_len - 1; j >= 0; j--)
                    {
                        int multiplyBy = int.Parse(num2[j].ToString());
    
                        int product = multiplier * multiplyBy + remainder
                                     + int.Parse(result[placeSpecifier].ToString());
                        remainder = 0;
    
                        int unitAns = product % 10;
                        // carry forward
                        if (product > 9)
                        {
                            remainder = product / 10;
                        }
    
                        result[placeSpecifier] = Convert.ToChar(unitAns.ToString());
                        placeSpecifier--;
                    }
    
                    if (remainder > 0)
                    {
                        result[placeSpecifier] = Convert.ToChar(remainder.ToString());
                        remainder = 0;
                    }
    
                    initialize--;
                    placeSpecifier = initialize;
                }
    
                if (remainder > 0)
                {
                    result[placeSpecifier] = Convert.ToChar(remainder.ToString());
                    remainder = 0;
                }
    
                for (int i = 0; i < result.Length; i++)
                {
                    if (result[i] == '0') 
                        result[i] = ' ';
                    else
                        break;
                }
    
                return new string(result).Trim();
            }
    

Log in to reply
 

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