原题链接在这里: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:
- 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.
题解:
(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的更多相关文章
-
[LeetCode] Complex Number Multiplication 复数相乘
Given two strings representing two complex numbers. You need to return a string representing their m ...
-
LeetCode 537. 复数乘法(Complex Number Multiplication)
537. 复数乘法 537. Complex Number Multiplication 题目描述 Given two strings representing two complex numbers ...
-
LC 537. Complex Number Multiplication
Given two strings representing two complex numbers. You need to return a string representing their m ...
-
【LeetCode】537. Complex Number Multiplication 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 日期 题目地址:https://leetcode.com/pr ...
-
[Swift]LeetCode537. 复数乘法 | Complex Number Multiplication
Given two strings representing two complex numbers. You need to return a string representing their m ...
-
537 Complex Number Multiplication 复数乘法
详见:https://leetcode.com/problems/complex-number-multiplication/description/ C++: class Solution { pu ...
-
537. Complex Number Multiplication
题目大意: 给出a, b两个用字符串表示的虚数,求a*b 题目思路: 偷了个懒,Python3的正则表达式匹配了一下,当然acm里肯定是不行的 class Solution: def complexN ...
-
C#版 - Leetcode 191. Number of 1 Bits-题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
-
[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 ...
随机推荐
-
java分享第十天(http协议简介)
URL模式URL(Uniform Resource Locator) 地址用于描述一个网络上的资源, 基本格式如下scheme 指定低层使用的协议(例如:http, https, ftp)host H ...
-
canvas 的一些效果
<html> <head> <style> *{ margin: 0; padding: 0; } body{ background:green; } #div{ ...
-
service的简单使用
Service的生命周期方法比Activity少一些,只有onCreate, onStart, onDestroy 我们有两种方式启动一个Service,他们对Service生命周期的影响是不一样的. ...
-
Java 第16章 封装
封装(encapsulation) 类使得数据和对数据的操作集成在一起,从而对使用该类的其他人来说,可以不管它的实现方法,而只管用它的功能,从而实现所谓的信息隐藏. 封装 , 使用类图描述类 ...
-
SQLServer中的死锁的介绍
简介 什么是死锁? 我认为,死锁是由于两个对象在拥有一份资源的情况下申请另一份资源,而另一份资源恰好又是这两对象正持有的,导致两对象无法完成操作,且所持资源无法释放. 什么又是阻塞? 阻塞是 ...
-
STM32F10x 学习笔记5(USART实现串口通讯 1)
STM32F10x 系列单片机中都包含了USART 模块,所谓USART,就是通用同步异步收发器.通用同步异步收发器(USART)提供了一种灵活的方法与使用工业标准NRZ异步串行数据格式的外部设备之间 ...
-
【Android进阶】Activity和Fragement中onSaveInstanceState()的使用详解
在activity(或者是fragement)被杀掉之前调用保存每个实例的状态,以保证该状态可以在onCreate(Bundle)或者onRestoreInstanceState(Bundle) (传 ...
-
实现一个简单的Laravel的dd库
前几天写了一个简单的Laravel的dd库. 为什么自己要写一个这样的库? Laravel本身已经实现了自己的输出dd函数,但是我之所以要写这样一个库,一来是因为Laravel本身对这个库的封装没办法 ...
-
position,display,float,overflow,margin,padding之间的相互影响
1.元素分为块级元素和行内元素, 块级元素可以设置宽高,会自动换行,并且会发生相邻margin的合并问题.行内元素设置宽和高无效,以水平方向排列,(行内元素,绝对定位,浮动元素不会发生外边距合并)并且 ...
-
css去除ios文本框默认圆角
css去除ios文本框默认圆角 input, textarea {-webkit-appearance: none;}