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命令 node常用

    //查看进程netstat -nlp //查找某一进程 netstat -nlp | grep node //后台挂起 nohup npm start &

  2. 【批处理】choice命令,call 命令,start 命令,rem

    [1]choice命令简介 使用此命令可以提示用户输入一个选择项,根据用户输入的选择项再决定执行具体的过程. 使用时应该加/c:参数,c: 后应写提示可输入的字符或数字,之间无空格.冒号是可选项. 使 ...

  3. rust学习(二)

    play on line match if #![allow(unused)] fn write_bar(size: u64){ match size{ o => println!(" ...

  4. 【luoguP2999】 [USACO10NOV]巧克力牛奶Chocolate Milk

    题目链接 考虑每条路径都经过的一个点,它可以到达每个出度为零点(终点),且每个入读为零点(起点)都能到达它, 拓扑排序记录下每个结点能到达的出度为零点的个数和沿反边能到达的入读为零点个数,判断是否等于 ...

  5. hdoj - 1248 寒冰王座

    Problem Description 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,只有一张钞票),为了防止自己在战斗中频繁的死掉,他决定给自己买一些道具,于是他来到了地精商店前.死亡骑 ...

  6. 集成了SSM框架的系统怎么做测试?

    1.首先在测试文件夹下新建一个测试基类BaseTest BaseTest中的代码如下: package wbl_ssm_blog.mapper; import org.junit.Test; impo ...

  7. python复合数据类型以及英文词频统计

    这个作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2753. 1.列表,元组,字典,集合分别如何增删改查及遍历. 列 ...

  8. TICK/TIGK运维栈安装运行【上】

    TICK/TIGK运运维metrics栈包括 InfuluxDB:为时间序列数据专门定制的高性能数据存储.TSM引擎允许高速接收和数据压缩.完全go语言编写.编译成一个单一的二进制,没有外部依赖.简单 ...

  9. free中buffer 与 cache 的区别

    通常人们所说的Cache就是指缓存SRAM. SRAM叫静态内存,“静态”指的是当我们将一笔数据写入SRAM后,除非重新写入新数据或关闭电源,否则写入的数据保持不变. 由于CPU的速度比内存和硬盘的速 ...

  10. net use命令详解(转)

    net use命令详解 1)建立空连接: net use \\IP\ipc$ "" /user:"" (一定要注意:这一行命令中包含了3个空格) 2)建立非空连 ...