LeetCode 537. 复数乘法(Complex Number Multiplication)
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:
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:
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:
- The input strings will not have extra blank.
- 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;
}
}
参考资料
- https://leetcode-cn.com/problems/complex-number-multiplication/
- https://leetcode.com/problems/complex-number-multiplication/
- https://leetcode.com/problems/complex-number-multiplication/discuss/100428/Java-7ms-easy-to-understand-solution
LeetCode 537. 复数乘法(Complex Number Multiplication)的更多相关文章
- [Swift]LeetCode537. 复数乘法 | Complex Number Multiplication
Given two strings representing two complex numbers. You need to return a string representing their m ...
- LC 537. Complex Number Multiplication
Given two strings representing two complex numbers. You need to return a string representing their m ...
- Java实现 LeetCode 537 复数乘法(关于数学唯一的水题)
537. 复数乘法 给定两个表示复数的字符串. 返回表示它们乘积的字符串.注意,根据定义 i2 = -1 . 示例 1: 输入: "1+1i", "1+1i" ...
- 【LeetCode】537. Complex Number Multiplication 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 日期 题目地址:https://leetcode.com/pr ...
- 537 Complex Number Multiplication 复数乘法
详见:https://leetcode.com/problems/complex-number-multiplication/description/ C++: class Solution { pu ...
- [LeetCode] Complex Number Multiplication 复数相乘
Given two strings representing two complex numbers. You need to return a string representing their m ...
- LeetCode Complex Number Multiplication
原题链接在这里:https://leetcode.com/problems/complex-number-multiplication/description/ 题目: Given two strin ...
- 537. Complex Number Multiplication
题目大意: 给出a, b两个用字符串表示的虚数,求a*b 题目思路: 偷了个懒,Python3的正则表达式匹配了一下,当然acm里肯定是不行的 class Solution: def complexN ...
- 【一天一道LeetCode】#260. Single Number III
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- Python的私有变量与装饰器@property的用法
Python的私有变量是在变量前面加上双横杠(例如:__test)来标识, Python私有变量只能在类内部使用,不被外部调用,且当变量被标记为私有后,调用时需再变量的前端插入类名,在类名前添加一个下 ...
- ctx控制超时的使用
cancel package main import ( "context" "fmt" "time" ) func gen(ctx con ...
- Clickhouse 性能瓶颈排查 IO过高
前几天公司clickhouse 有个查询很慢.经理一直追问为什么慢 是cpu 不够 还是IO 占用太高,还是其他的原因.于是有了以下的排查 执行该条,在不考虑优化sql 的情况下 进行性能排查 1.首 ...
- BCB6 如何跨工程(Project)进行源码级调试
如何跨工程(Project)进行源码级调试 在日常工作中,如何跨工程(Project)进行源码级调试这是个无法回避的问题.例如:一个应用程序工程为“prj_A”,一个动态库工程为“prj_B”,“pr ...
- circus security 来自官方的安全建议
转自:https://circus.readthedocs.io/en/latest/design/security/ Circus is built on the top of the ZeroMQ ...
- chsh
修改shell进程
- VSCode 本地如何查看历史页面
1.首先要在VSCode的扩展中安装一个 Local history插件,蓝色框部分不用管,直接安装即可 2.安装并操作:安装后,修改 productManage/supplierList/addSu ...
- IT 常用单词表
程序员英语单词册 前言 程序员必备的600个英语词汇(1) 程序员必备的600个英语词汇(2) 程序员必备的600个英语词汇(3) 程序员必备的600个英语词汇(4) 程序员不 ...
- uniapp - 键盘弹起背景图片不会被挤压
[释义] uni.getSystemInfoSync()获取屏幕可用高度windowScreen做为背景图高度即可(非虚拟DOM也可以使用本思路). [源码] <template> < ...
- Python之Lambda与三元运算
Python之Lambda与三元运算 Lambda 运算 概念:是指一类无需定义标识符(函数名)的函数或者子程序.特点:匿名函数不使用def定义函数,使用lambda来创建匿名函数1.lambda只是 ...