• Java实现 LeetCode 537 复数乘法(关于数学唯一的水题)


    537. 复数乘法

    给定两个表示复数的字符串。

    返回表示它们乘积的字符串。注意,根据定义 i2 = -1 。

    示例 1:

    输入: “1+1i”, “1+1i”
    输出: “0+2i”
    解释: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i ,你需要将它转换为 0+2i 的形式。
    示例 2:

    输入: “1±1i”, “1±1i”
    输出: “0±2i”
    解释: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i ,你需要将它转换为 0±2i 的形式。
    注意:

    输入字符串不包含额外的空格。
    输入字符串将以 a+bi 的形式给出,其中整数 a 和 b 的范围均在 [-100, 100] 之间。输出也应当符合这种形式。

    class Solution {
         public String complexNumberMultiply(String a, String b) {
    
            StringBuilder res = new StringBuilder();
    
            int aspit = a.indexOf('+');
            int bspit = b.indexOf('+');
    
            int aa = Integer.parseInt( a.substring(0,aspit) );
            int ab = Integer.parseInt( a.substring(aspit+1, a.length()-1) );
            int ba = Integer.parseInt( b.substring(0,bspit) );
            int bb = Integer.parseInt( b.substring(bspit+1, b.length()-1) );
     
            res.append(aa * ba - ab * bb);
            res.append('+');
            res.append(aa * bb + ab * ba);
            res.append('i');
            return res.toString();
        }
    }
    
  • 相关阅读:
    合并字符串中的多个空格
    IfcSpecularRoughness
    IfcSpecularExponent
    IfcPresentableText
    IfcFontWeight
    IfcFontVariant
    uwb ifc模型定位測試
    IfcFontStyle
    IfcGeometricModelResource
    qt6安装
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12946398.html
Copyright © 2020-2023  润新知