首页 > 其他 > 详细

537. 复数乘法

时间:2019-07-21 16:48:41      阅读:100      评论:0      收藏:0      [点我收藏+]

题目地址: https://leetcode-cn.com/problems/complex-number-multiplication/

代码地址:https://github.com/javartisan/edx-ds/blob/master/src/main/java/com/javartisan/leetcode/Solution537.java

 

class Solution537 {

    public String complexNumberMultiply(String a, String b) {
        
        String[] splitA = split(a);
        String[] splitB = split(b);
        int a1 =Integer.parseInt(splitA[0]);
        int a2 =Integer.parseInt(splitA[1]);
        int b1 =Integer.parseInt(splitB[0]);
        int b2 =Integer.parseInt(splitB[1]);

        int c1 = a1 * b1 - a2 * b2 ;
        int c2 = a1 * b2 + a2 * b1;
        StringBuilder sb = new StringBuilder();
        sb.append(c1);
        sb.append("+");
        sb.append(c2).append("i");
        return  sb.toString();
        
    }


    public static String[] split(String expr){
        // endsWith
        if(expr.endsWith("i")){
            expr = expr.substring(0, expr.length()-1);
        }
        return expr.split("\\+");   
    }

}

  

537. 复数乘法

原文:https://www.cnblogs.com/leodaxin/p/11221542.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!