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 ...
随机推荐
- TypeScript语言学习笔记(4)枚举
枚举 // 数值型枚举 enum Direction { Up = 1, Down, Left, Right, } // Up=0 enum Direction { Up, Down, Left, R ...
- AtCoder Beginner Contest 132 解题报告
前四题都好水.后面两道题好难. C Divide the Problems #include <cstdio> #include <algorithm> using names ...
- Django API view 登录认证
文件分类 url from django.contrib import admin from django.urls import path, re_path from django.urls imp ...
- 使用WinDbg调试入门(内核模式)
windbg是一个内核模式和用户模式调试器,包含在Windows调试工具中.这里我们提供了一些实践练习,可以帮助您开始使用windbg作为内核模式调试器. 设置内核模式调试 内核模式调试环境通常有两台 ...
- 帝国cms7.5整合百度编辑器ueditor教程
1.根据自己使用的帝国cms版本编码下载对应的ueditor版本 下载地址 http://ueditor.baidu.com/website/download.html#ueditor 2.解压附件, ...
- 服务器收不到支付宝notify_url异步回调请求的问题排查
小背景 最近在调整支付宝支付的功能时发现,不能够正常接收支付宝付款成功之后的回调通知了,从代码到配置最后到服务器配置都排查了一遍,最终发现问题原因竟然是因为我们的回调地址notify_url是http ...
- modao账户
chairman987@163.com 墨刀注册 p@ssw0rd OR 123456
- 用DLL方式封装MDI子窗体
用DLL方式封装MDI子窗体是一种常用的软件研发技术,他的长处: 研发人员能够负责某一个模块的编写包括(界面+逻辑),能够互不干扰,模块研发完成后,主程式统一调用. 易于程式升级,当程式升级时,不用编 ...
- Laravel模型事件的实现原理详解
模型事件在 Laravel 的世界中,你对 Eloquent 大多数操作都会或多或少的触发一些模型事件,下面这篇文章主要给大家介绍了关于Laravel模型事件的实现原理,文中通过示例代码介绍的非常详细 ...
- ElasticSearch5.2.2 安装(老版本)
https://www.elastic.co/downloads/elasticsearchElasticSearch是一个高可扩展的开源的全文搜索分析引擎.它允许你快速的存储.搜索和分析大量数据.E ...