LeetCode43 Multiply Strings

来源:互联网 发布:php怎么开启exec函数 编辑:程序博客网 时间:2024/06/06 09:33

详细见:leetcode.com/problems/multiply-strings


Java Solution: github

package leetcode;public class P043_MultiplyStrings {public static void main(String[] args) {//System.out.println(new Solution().multiply("99", "9"));System.out.println(new Solution().multiply("99", "0"));}/* *  一个简单的大数乘法 * 8 ms * 79.50%  */static class Solution {    public String multiply(String num1, String num2) {    int len1 = 0, len2 = 0;    if (num1 == null || (len1 = num1.length()) == 0 ||     num1 == null || (len2 = num2.length()) == 0 )    return String.valueOf(0);    int[] ans = new int[len1 + len2 + 1];    for (int i = len1 - 1; i > -1; i --)    for (int j = len2 - 1; j > -1; j --)    ans[i + 2 + j] += (num1.charAt(i) - '0') * (num2.charAt(j) - '0');    int carry = 0;    for (int i = len1 + len2; i > -1; i --) {    int temp = ans[i] + carry;    ans[i] = temp % 10;    carry = temp / 10;    }    int i = 0;    char[] cs = null;    for (carry = -1; i != ans.length; i ++)    if (carry == -1) {    if (ans[i] != 0) {    cs = new char[ans.length - i];    carry = i;    cs[0] = (char)(ans[i] + '0');    }    } else    cs[i - carry] = (char)(ans[i] + '0');        return cs == null ? "0" : new String(cs);    }}}


C Solution: github

/*    url: leetcode.com/problems/multiply-strings/    AC 6ms 57.50%*/#include <stdio.h>#include <stdlib.h>#include <string.h>//linked list starttypedef char T;typedef struct ln sln;typedef struct ln * pln;typedef struct ll sll;typedef struct ll * pll;//list nodestruct ln {    T val;    pln nxt;};//linked liststruct ll {    pln first;    pln last;    int size;};void ll_add_first(pll l, T v) {    pln t = (pln) malloc(sizeof(sln));    t->val = v;    t->nxt = NULL;    if (l->size == 0) {        l->first = t;        l->last = t;        l->size = 1;        return;    }    t->nxt = l->first;    l->first = t;    l->size ++;}void ll_add_last(pll l, T v) {    pln t = (pln) malloc(sizeof(sln));    t->val = v;    t->nxt = NULL;    if (l->size == 0) {        l->first = t;        l->last = t;        l->size = 1;        return;    }    l->last->nxt = t;    l->last = t;    l->size ++;}void ll_remove_first(pll l) {    pln t = NULL;    if (l->first == NULL) return;    if (l->first == l->last) {        free(l->first);        l->first = NULL;        l->last = NULL;        l->size = 0;        return;    }    t = l->first->nxt;    free(l->first);    l->first = t;    l->size --;}void ll_construct_from_array(pll l, T* arr, int arr_size) {    int i = 0;    for (i = 0; i < arr_size; i ++) {        ll_add_last(l, *(arr + i));    }}T* ll_convert_to_array_free_l(pll l) {    T* arr = NULL;    int arr_index = 0;    pln travel = NULL;    pln t1 = NULL, t2 = NULL;    if (l == NULL || l->size == 0) {        if (l != NULL) free(l);        return NULL;    }    arr = (T*) malloc(sizeof(T) * l->size);    travel = l->first;    while (travel != NULL) {        *(arr + arr_index ++) = travel->val;        travel = travel->nxt;    }    t1 = l->first;    while (t1 != NULL) {        t2 = t1->nxt;        free(t1);        t1 = t2;    }    free(l);    return arr;}void ll_print(pll l) {    pln t = l == NULL ? NULL : l->first;    while (t != NULL) {        printf("%d ", t->val);        t = t->nxt;    }    printf("\r\n");}void ll_free_all(pll l) {    pln t1 = l->first, t2 = NULL;    while (t1 != NULL) {        t2 = t1->nxt;        free(t1);        t1 = t2;    }    free(l);}//linked list endchar* multiply(char* n1, char* n2) {    int n1n = strlen(n1), n2n = strlen(n2);    int* rec = (int*) malloc(sizeof(int) * (n1n + n2n - 1));    int i = 0, i1 = 0, i2 = 0, val = 0;    pll l = (pll) malloc(sizeof(sll));    l->first = NULL;    l->last = NULL;    l->size = 0;    for (i = 0; i < n1n + n2n - 1; i ++)        rec[i] = 0;    for (i1 = 0; i1 < n1n; i1 ++) {        for (i2 = 0; i2 < n2n; i2 ++) {            rec[i1 + i2] += (*(n1 + i1) - '0') * (*(n2 + i2) - '0');        }    }    for (val = 0, i = n1n + n2n - 2; i > -1; i --) {        val += rec[i];        ll_add_first(l, (char)('0' + val % 10));        val = val / 10;    }    while (val != 0) {        ll_add_first(l, (char)('0' + val % 10));        val = val / 10;    }    while (l->first != l->last && l->first->val == '0')        ll_remove_first(l);    ll_add_last(l, '\0');    free(rec);    return ll_convert_to_array_free_l(l);}int main() {    char* n1 = "99999";    char* n2 = "0";    char* ans = multiply(n1, n2);    printf("answer is %s\r\n", ans);    free(ans);}


Python Solution: github

#coding=utf-8'''    url: leetcode.com/problems/multiply-strings    @author:     zxwtry    @email:      zxwtry@qq.com    @date:       2017年4月7日    @details:    Solution: 366ms 61.53%'''class Solution(object):    def multiply(self, n1, n2):        """        :type n1: str        :type n2: str        :rtype: str        """        n1n = 0 if n1 == None else len(n1)        n2n = 0 if n2 == None else len(n2)        if n1n == 0 or n2n == 0:            return "0"        a, val, l = [], 0, [0] * (n1n + n2n)        for i1 in range(n1n-1, -1 , -1):            for i2 in range(n2n-1, -1, -1):                l[n1n-1-i1+n2n-1-i2] += int(n1[i1])*int(n2[i2])        for i in range(0, n1n+n2n):            val += l[i]            a.append(str(val%10))            val //= 10        while val != 0:            a.append(str(val%10))            val //= 10        while len(a) != 1 and a[len(a) - 1] == '0':            a.pop()        a.reverse()        return "".join(a)if __name__ == "__main__":    print(Solution().multiply("789", "999999"))        


0 0