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

题目:

Given two strings representing two complex numbers.

You need to return a string representing their multiplication. Note i2 = -1 according to the definition.

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.

题解:

(aReal + aImag*i)*(bReal + bImag*i) = (aReal*bReal- aImag*bImag) + (aReal*bImag+bReal*aImag)*i.

从原有string里提取出实数和虚数两部分组成答案.

Time Complexity: O(a.length() + b.length()). split用时

Space: O(a.length() + b.length()). 中间的string arr.

AC Java:

 class Solution {
public String complexNumberMultiply(String a, String b) {
String [] aArr = a.split("\\+|i");
String [] bArr = b.split("\\+|i");
int aReal = Integer.valueOf(aArr[0]);
int aImag = Integer.valueOf(aArr[1]);
int bReal = Integer.valueOf(bArr[0]);
int bImag = Integer.valueOf(bArr[1]);
return (aReal*bReal-aImag*bImag) + "+" + (aReal*bImag+aImag*bReal) + "i";
}
}

  

LeetCode Complex Number Multiplication的更多相关文章

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

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

  2. LeetCode 537. 复数乘法(Complex Number Multiplication)

    537. 复数乘法 537. Complex Number Multiplication 题目描述 Given two strings representing two complex numbers ...

  3. LC 537. Complex Number Multiplication

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

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

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

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

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

  6. 537 Complex Number Multiplication 复数乘法

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

  7. 537. Complex Number Multiplication

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

  8. C#版 - Leetcode 191. Number of 1 Bits-题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  9. [leetcode]200. Number of Islands岛屿个数

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

随机推荐

  1. GIT如何使用:大杀器!所有常用指令整理

    1 pwd 显示当前目录2 mkdir 创建目录 cd 进入文件3 git init 变成Git可以管理的仓库(千万不要修改目录下的.git隐藏文件夹)4 ls -ah 可以把.git文件显示出来5 ...

  2. Python面试题之集合推导式、字典推导式

    集合推导式 集合推导式(set comprehensions)跟列表推导式也是类似的, 唯一的区别在于它们使用大括号{}表示. Code: sets = {x for x in range(10)} ...

  3. NOIP 数字游戏

    描述 丁丁最近沉迷于一个数字游戏之中.这个游戏看似简单,但丁丁在研究了许多天之后却发觉原来在简单的规则下想要赢得这个游戏并不那么容易.游戏是这样的,在你面前有一圈整数(一共n个),你要按顺序将其分为m ...

  4. JAVA基础补漏--static

    静态方法不能访问非静态变量的原因 静态的方法和变量在内存中先产生,非静态的后产生,在静态调用时非静态可能还未创建,所以会发生错误,故不能访问. static的内存图 静态代码块 static { Sy ...

  5. 十二道MR习题 - 4 - TopN问题

    题目: 有一个很大的文件,这文件中的内容全部都是数字,要求尝试从这个文件中找出最大的10个数字. 分析: 看起来像是一个比较简单的问题.不用大数据框架的话,也能比较轻易的实现:就是逐个读取文件中的每个 ...

  6. NiceFish的ERROR in AppModule is not an NgModule问题

    大漠老师的angular2新手教程项目,nicefish刚开始下下来运行ng serve --prod --aot 是 出现ERROR in AppModule is not an NgModule ...

  7. ps(笔记)

    窗口 工作区 默认窗口(恢复) ctrl + n 点阵图(像素图) 小方格组成的 alt 键 配合 放大缩小 ppi dpi 打印输出. 画布新建 z键 局部放大 右击实际像素操作 f键 全屏 空格键 ...

  8. 在线演示demo

    *{display:none} 仿微博添加和删除动画 body{} input,button,select,textarea{outline:none;} .sdiv{width:400px;} .b ...

  9. MySQL 入门篇

    历史 MySQL 是由 David Axmark.Allan Larsson 和 Michael Widenius 3 个瑞典人于 20 世纪 90 年代开发的一个关系型数据库.MySQL 之名取自创 ...

  10. SSH登陆阿里云服务器出现Permission denied (publickey)错误解决方案

    操作环境: 操作系统:Mac10.11.5 阿里云服务器:Ubuntu16.04 远程连接:SSH 注:首先我们已假设你已经自己生成了SSH秘钥,并已经配置到阿里云.绑定了自己的云服务器. 但是后来发 ...