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 ...
随机推荐
-
【开源】OSharp3.3框架解说系列(7.1):初始化流程概述
OSharp是什么? OSharp是个快速开发框架,但不是一个大而全的包罗万象的框架,严格的说,OSharp中什么都没有实现.与其他大而全的框架最大的不同点,就是OSharp只做抽象封装,不做实现.依 ...
-
Python自动化 【第五篇】:Python基础-常用模块
目录 模块介绍 time和datetime模块 random os sys shutil json和pickle shelve xml处理 yaml处理 configparser hashlib re ...
-
linux驱动系列之调试环境搭建一
2014年刚开始学习linux时,搭建环境花了很多时间.当时最熟悉的是单片机如Mag16和stm32,依据以往学习单片机的经验肯定要用下载器下载程序,但是我找了很久没有比较好的IDE,不像Mag16有 ...
-
一些Android框架
从网上收集一些框架,敲代码偷懒这些框架非常实用,必须记下来,为了以后少写代码,用别人好的框架 ThinkAndroid ThinkAndroid(一个ThinkAndroid教程地址:http://m ...
-
RH033读书笔记(11)-Lab 12 Configuring the bash Shell
Sequence 1: Configuring the bash Shell Deliverable: A system with new aliases that clear the screen, ...
-
Python:游戏:贪吃蛇原理及代码实现
一.游戏介绍 贪吃蛇是个非常简单的游戏,适合练手.先来看一下我的游戏截图: 玩法介绍:回车键:开始游戏空格键:暂停 / 继续↑↓←→方向键 或 WSAD 键:控制移动方向. 食物分红.绿.蓝三种,分别 ...
-
elementui command绑定变量对象方法
command绑定变量对象方法 使用v-bind : command绑定 简写 :command
-
【PAT】B1084 外观数列(20 分)(纯C)
第一层循环,用来循环计算第几个元素 第二层用来计算当前元素的下一个 #include<stdio.h> #include<string.h> char aaa[100000] ...
-
在jupyter notebook导入tensorflow出错:No module named tensorflow 解决办法
1.背景 首先说一下我的环境: os : windows10 anaconda版本:2.7 官网提供了两种方法来安装TensorFlow:pip和anaconda.我使用的是anaconda方法.按照 ...
-
WordPaster-CuteEditor6.7整合教程
CuteEditor6.7下载地址:http://yunpan.cn/QzvjC5iaH5HJm 1.添加CuteEditor.dll的引用 2.在编辑器页面,为编辑器工具栏增加控件按钮 3.在前台增 ...