LeetCode--Complex Number Multiplication

思路:

    按照数学公式进行拆分和计算.

class Solution {
    public String complexNumberMultiply(String a, String b) {
        String[] a_arr=a.split("\\+");
        String[] b_arr=b.split("\\+");
        int a1=Integer.parseInt(a_arr[0]);
        int a2=Integer.parseInt(a_arr[1].replace("i",""));
        int b1=Integer.parseInt(b_arr[0]);
        int b2=Integer.parseInt(b_arr[1].replace("i",""));
        String res1=""+(a1*b1+a2*b2*(-1));
        String res2=""+(a1*b2+a2*b1);
        
        return res1+"+"+res2+"i";
    }
}

猜你喜欢

转载自blog.csdn.net/qq_21752135/article/details/80064683