537. 复数乘法

537. Complex Number Multiplication

题目描述

Given two strings representing two complex numbers.

You need to return a string representing their multiplication. Note i2 = -1 according to the definition.

LeetCode537. Complex Number Multiplication中等

Example 1:

Input: "1+1i", "1+1i"
Output: "0+2i"
Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.

Example 2:

Input: "1+-1i", "1+-1i"
Output: "0+-2i"
Explanation: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.

Note:

  1. The input strings will not have extra blank.
  2. The input strings will be given in the form of a+bi, where the integer a and b will both belong to the range of [-100, 100]. And the output should be also in this form.

Java 实现

class Solution {
// 复数的乘法: (a+bi)(c+di)=(ac-bd)+(bc+ad)i
public String complexNumberMultiply(String a, String b) {
int[] arrA = getValue(a);
int[] arrB = getValue(b);
int x = arrA[0] * arrB[0] - arrA[1] * arrB[1];
int y = arrA[1] * arrB[0] + arrA[0] * arrB[1];
return x + "+" + y + "i";
} private int[] getValue(String s) {
String[] str = s.split("\\+");
int[] val = new int[2];
val[0] = Integer.parseInt(str[0]);
val[1] = Integer.parseInt(str[1].replace("i", ""));
return val;
}
}

参考资料

最新文章

  1. 实战Java虚拟机之三“G1的新生代GC”
  2. SQLite建表并添加数据
  3. [Xamarin] 簡單使用AlertDialog (转帖)
  4. Ubuntu升级内核
  5. webapp开发经验总结
  6. JSP 服务器响应
  7. [VB.NET]Dictionary类
  8. Jordan Lecture Note-5: Kernels
  9. tcl/tk实例详解——修改目录下所有文件(使用一个字符串代替另外一个)
  10. 【HDU 3949】 XOR (线性基,高斯消元)
  11. 精通javasCRIPT-学习笔记 Features,Functions,Object
  12. C++inserter
  13. 【learning】莫比乌斯反演
  14. [物理学与PDEs]第1章第8节 静电场和静磁场 8.1 静电场
  15. Lua + win 10 + vs2017的运行环境和创建cocos2dx 3.17的lua项目(亲测)
  16. zabbix_agent添加到系统服务启动(八)
  17. iOS开发-消息转发
  18. netstat 在windows下和Linux下查看网络连接和端口占用
  19. Hibernate高效查询,只查询部分/指定字段
  20. VMware 安装CentOS 6.5图文步骤 以及安装后无法联网的解决办法

热门文章

  1. Windows环境下设置Tomcat8以服务的形式运行,不再打开Tomcat窗口
  2. 09-Flutter移动电商实战-移动商城数据请求实战
  3. python 字符串方法整理
  4. (尚026)Vue_案例_动态初始化显示(尚025)
  5. rac集群状态脚本执行awk拼接有问题
  6. BJOI做题记录
  7. 原创:史上对BM25模型最全面最深刻的解读以及lucene排序深入讲解
  8. 【2019.11.06】SDN上机第2次作业
  9. ACR Code Pacs
  10. JAVA:使用栈实现一个队列