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;
}
}

参考资料

LeetCode 537. 复数乘法(Complex Number Multiplication)的更多相关文章

  1. [Swift]LeetCode537. 复数乘法 | Complex Number Multiplication

    Given two strings representing two complex numbers. You need to return a string representing their m ...

  2. LC 537. Complex Number Multiplication

    Given two strings representing two complex numbers. You need to return a string representing their m ...

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

    537. 复数乘法 给定两个表示复数的字符串. 返回表示它们乘积的字符串.注意,根据定义 i2 = -1 . 示例 1: 输入: "1+1i", "1+1i" ...

  4. 【LeetCode】537. Complex Number Multiplication 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 日期 题目地址:https://leetcode.com/pr ...

  5. 537 Complex Number Multiplication 复数乘法

    详见:https://leetcode.com/problems/complex-number-multiplication/description/ C++: class Solution { pu ...

  6. [LeetCode] Complex Number Multiplication 复数相乘

    Given two strings representing two complex numbers. You need to return a string representing their m ...

  7. LeetCode Complex Number Multiplication

    原题链接在这里:https://leetcode.com/problems/complex-number-multiplication/description/ 题目: Given two strin ...

  8. 537. Complex Number Multiplication

    题目大意: 给出a, b两个用字符串表示的虚数,求a*b 题目思路: 偷了个懒,Python3的正则表达式匹配了一下,当然acm里肯定是不行的 class Solution: def complexN ...

  9. 【一天一道LeetCode】#260. Single Number III

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. linux 查看某个目录下文件的数量

    今日思语:时间是个庸医,却自称能包治百病~ 在linux环境下,经常需要查看某个文件目录下的文件数有多少,除了进入当前目录下查看,还可以使用命令: ls -l | grep "^-" ...

  2. ARDUNIO IMU processing姿态数据可视化

    https://www.arduino.cn/thread-42852-1-1.html 关键数据打包 float roll, pitch, heading; Serial.print("O ...

  3. cube.js 学习 cube 连接mongodb 试用

    cube.js 对于mongodb 的连接是通过mongodb bi connector(mysql 协议)处理的,以下为简单的试用 安装mongo bi connector 这个玩意用docker ...

  4. P2624 [HNOI2008]明明的烦恼

    #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #inclu ...

  5. 【Java 8】巧用Optional之优雅规避NPE问题

    避之不及的 NullPointerException NPE : NullPointerException 空指针异常是最常见的Java异常之一,抛出NPE错误不是用户操作的错误,而是开发人员的错误, ...

  6. 2015-2016-2《Java程序设计》团队博客2

     简易画图板介绍 一.功能结构图 二.主类设计 1.总体设计:在设计简易画图板时,根据程序功能的分类,包含了十二个文件,包括SimpleDraw.java,MenuContainer.java,Dra ...

  7. Network Function Virtualization: Challenges and Opportunities for Innovations

    年份:2015 ABSTRACT 最近提出了网络功能虚拟化,以提高网络服务供应的灵活性并减少新服务的上市时间. 通过利用虚拟化技术和通用的商用可编程硬件(例如通用服务器,存储和交换机),NFV可以将网 ...

  8. DELPHI7 ADO二层升三层新增LINUX服务器方案

    DELPHI7 ADO二层升三层新增LINUX服务器方案 引子:笔者曾经无数次在用户的LINUX服务器上创建一个WINDOWS虚拟机,用于运行自己DELPHI开发中间件. 现在再不需要如此麻烦了. 咏 ...

  9. [转]centos sqlite3安装及简单命令

    安装: 方法一: wget http://www.sqlite.org/sqlite-autoconf-3070500.tar.gz tar xvzf sqlite-autoconf-3070500. ...

  10. ISO/IEC 9899:2011 条款5——环境

    5.环境 1.一个实现在两个不同的数据处理系统环境下分别翻译C源文件与执行C程序,在本国际标准中分别称为翻译环境和执行环境.它们的特征定义并包含了根据顺从标准实现的语法及语义规则所构造的顺从标准的C程 ...