LeetCode Complex Number Multiplication
原题链接在这里: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 ...
 
随机推荐
- zabbix监控nginx的性能
			
1.nginx配置 需要使用zabbix监控nginx,首先nginx需要配置ngx_status,在nginx的配置文件中加入红框中的配置,然后重启nginx如下图所示: location /ngx ...
 - 【转载】User notification 的实现方法
			
原帖请看:http://cocoathings.blogspot.com/2013/01/introduction-to-user-notifications-in.html 想要实现如图这样的not ...
 - 配置阿里云maven中央库
			
<mirrors> <mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> ...
 - Winform与WPF异步修改控件属性
			
Winform方式: if (this.InvokeRequired) { this.Invoke( ...
 - java中HashMap、HashTable、TreeMap的区别总结【表格对比清楚明了】
			
底层 有序否 键值对能否为Null 遍历 线程安全 哈希Code Hashmap 数组+链表 无序 都可null iterator 不安全 内部hash方法 Hashtable 数组+链表 无序 ...
 - Vector3函数理解-计算两向量之间的角度
			
1.已知两个向量dirA,dirB.Vector3 dirA = new Vector3(-1,1,0); Vector3 dirB = new Vector3(-1,1,1);2.使向量处于同一个平 ...
 - 离线安装Chrome扩展和App
			
------------------离线安装扩展------------------下载1. 用Get CRX扩展到Web Store页,点开一个扩展,右键 Get CRX 可将扩展下载到本机.2. ...
 - HDU 3037 组合数、lucas,逆元
			
题目链接 题目大意,N颗树上取不超过M个果子,求总方案个数模P的值,P是质数且不超过10w,N,M不超过1e9: 在这里树是被认为不同的,也就是将k(0<=k<=M)个小球放入N个不同的盒 ...
 - 【spark】持久化
			
Spark RDD 是惰性求值的. 如果简单地对RDD 调用行动操作,Spark 每次都会重算RDD 以及它的所有依赖.这在迭代算法中消耗格外大. 换句话来说就是 当DAG图遇到转化操作的时候是不求值 ...
 - Struts01---入门小案例
			
创建web项目 实现的效果! 用户点击页面不同的链接,后台调用不同的代码! 创建两个类实现共同的接口! public interface Action { String execute(); } ...