1. 程式人生 > 其它 >LeetCode:399.Evaluate Division 除法求值(C語言)

LeetCode:399.Evaluate Division 除法求值(C語言)

技術標籤:LeetCode

題目描述:
給你一個變數對陣列 equations 和一個實數值陣列 values 作為已知條件,其中 equations[i] = [Ai, Bi] 和 values[i] 共同表示等式 Ai / Bi = values[i] 。每個 Ai 或 Bi 是一個表示單個變數的字串。

另有一些以陣列 queries 表示的問題,其中 queries[j] = [Cj, Dj] 表示第 j 個問題,請你根據已知條件找出 Cj / Dj = ? 的結果作為答案。

返回 所有問題的答案 。如果存在某個無法確定的答案,則用 -1.0 替代這個答案。

注意:輸入總是有效的。你可以假設除法運算中不會出現除數為 0 的情況,且不存在任何矛盾的結果。

示例 1:

輸入:equations = [[“a”,“b”],[“b”,“c”]], values = [2.0,3.0], queries = [[“a”,“c”],[“b”,“a”],[“a”,“e”],[“a”,“a”],[“x”,“x”]]
輸出:[6.00000,0.50000,-1.00000,1.00000,-1.00000]
解釋:
條件:a / b = 2.0, b / c = 3.0
問題:a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?
結果:[6.0, 0.5, -1.0, 1.0, -1.0 ]

示例 2:

輸入:equations = [[“a”,“b”],[“b”,“c”],[“bc”,“cd”]], values = [1.5,2.5,5.0], queries = [[“a”,“c”],[“c”,“b”],[“bc”,“cd”],[“cd”,“bc”]]

輸出:[3.75000,0.40000,5.00000,0.20000]

示例 3:

輸入:equations = [[“a”,“b”]], values = [0.5], queries = [[“a”,“b”],[“b”,“a”],[“a”,“c”],[“x”,“y”]]
輸出:[0.50000,2.00000,-1.00000,-1.00000]

提示:

1 <= equations.length <= 20
equations[i].length == 2
1 <= Ai.length, Bi.length <= 5
values.length == equations.length
0.0 < values[i] <= 20.0
1 <= queries.length <= 20
queries[i].length == 2
1 <= Cj.length, Dj.length <= 5
Ai, Bi, Cj, Dj 由小寫英文字母與數字組成

來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/evaluate-division
著作權歸領釦網路所有。商業轉載請聯絡官方授權,非商業轉載請註明出處。
解答:

typedef struct {
    char *x;
    char *y;
    double z;
    UT_hash_handle hh;
} Hash;

Hash *hash;

Hash* derive(Hash *p, double *t) {
    *t = 1;
    while (strcmp(p->x, p->y)) {
        *t *= p->z;
        char *s = p->y;
        HASH_FIND(hh, hash, s, strlen(s), p);
    }
    return p;
}

void _add(char *x, char *y, double z) {
    Hash *p1, *p2;
    HASH_FIND(hh, hash, x, strlen(x), p1);
    HASH_FIND(hh, hash, y, strlen(y), p2);
    if (p1 && p2) {
        //x、y已經出現過,若它們無聯絡,那麼將他們合併關係
        double tx, ty;
        p1 = derive(p1, &tx);
        p2 = derive(p2, &ty);
        if (strcmp(p1->x, p2->x)) {
            p1->y = p2->x;
            p1->z = ty*z/tx;
        }
    } else if (p1) {
        double tx;
        p1 = derive(p1, &tx);
        Hash *p = malloc(sizeof(Hash));
        p->x = y; 
        p->y = p1->x;
        p->z = tx/z;
        HASH_ADD_STR(hash, x, p);
    } else if (p2) {
        double ty;
        p2 = derive(p2, &ty);
        Hash *p = malloc(sizeof(Hash));
        p->x = x;
        p->y = p2->x;
        p->z = z/ty;
        HASH_ADD_STR(hash, x, p);
    } else {
        p1 = malloc(sizeof(Hash));
        p2 = malloc(sizeof(Hash));
        p1->x = x;
        p1->y = y;
        p1->z = z;
        HASH_ADD_STR(hash, x, p1);
        p2->x = y;
        p2->y = y;
        p2->z = 1;
        HASH_ADD_STR(hash, x, p2);
    }
}

double _div(char *x, char *y) {
    Hash *p1, *p2;
    HASH_FIND(hh, hash, x, strlen(x), p1);
    HASH_FIND(hh, hash, y, strlen(y), p2);
    if (p1 && p2) {
        double tx, ty;
        p1 = derive(p1, &tx);
        p2 = derive(p2, &ty);
        if (strcmp(p1->x, p2->x) == 0) {
            return tx/ty;
        }
    } 
    return -1.0;
}

double* calcEquation(char *** equations, int equationsSize, int* equationsColSize, double* values, int valuesSize, char *** queries, int queriesSize, int* queriesColSize, int* returnSize){
    hash = NULL;
    for (int i=0; i<equationsSize; ++i) {
        _add(equations[i][0], equations[i][1], values[i]);
    }
    double *res = malloc(sizeof(double) * queriesSize);
    for (int i=0; i<queriesSize; ++i) {
        res[i] = _div(queries[i][0], queries[i][1]);
    }
    *returnSize = queriesSize;
    return res;
}



執行結果 :
在這裡插入圖片描述

Notes:
完全參考https://leetcode-cn.com/problems/evaluate-division/solution/c-bing-cha-ji-by-dong-feng-32/