C# solution


  • 0
    H
    public string Multiply(string num1, string num2) {
            int[] r = new int [num1.Length + num2.Length];
            
            for(int i = 0; i < num1.Length; i++) {
                int d1 = num1[num1.Length - i - 1] - '0';
                int c = 0, j = 0;
                for(j = 0; j < num2.Length; j++) {
                    int d2 = num2[num2.Length - j - 1] - '0';
                    int d = d1 * d2 + c + r[i + j];
                    r[i + j] = d % 10;
                    c = d / 10;
                }
                r[i + j] += c;
            }
            
            Array.Reverse(r);
            StringBuilder sb = new StringBuilder();
            int k = 0;
            while(k < r.Length && r[k] == 0) k++;
            for(; k < r.Length; k++) sb.Append((char)('0' + r[k]));
            if (sb.Length == 0) sb.Append("0");
            
            return sb.ToString();
        }
    

Log in to reply
 

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