537. Complex Number Multiplication
来源:互联网 发布:阿里云ftp端口号 编辑:程序博客网 时间:2024/05/16 04:52
537. Complex Number Multiplication
1、题目
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.
2、翻译
给定两个表示两个复数的字符串。
您需要返回一个表示其乘法的字符串。注意i 2 = -1根据定义。
示例1:
输入: “1 + 1i”,“1 + 1i”
输出: “0 + 2i”
说明:(1 + i)(1 + i)= 1 + i 2 + 2 i = 2i,以0 + 2i的形式。
示例2:
输入: “1 + -1i”,“1 + -1i”
输出: “0 + -2i”
说明:(1 - i)(1 - i)= 1 + i 2 - 2 i =您需要将其转换为0 + -2i的形式。
注意:
输入字符串不会有额外的空格。
输入字符串将以+ bi的形式给出,其中整数a和b都属于[-100,100]的范围。而输出也应该是这种形式。
3、解题思路
这道题不难,解题思路都是一样的,就是根据+进行分割,然后进行转成数字进行计算,速度快慢的区别在于java函数的使用上,一种是字符串操作多,另外一种是字符串操作少,我写的就比较多,所以速度慢,如下所示
public class Solution { public String complexNumberMultiply(String a, String b) { String []temA=a.split("[+]"); temA[1] = temA[1].replace("i", ""); String []temB=b.split("[+]"); temB[1] = temB[1].replace("i", ""); String result=""; result+= String.valueOf(Integer.valueOf(temA[0]) * Integer.valueOf(temB[0]) + (-1) *Integer.valueOf(temA[1]) * Integer.valueOf(temB[1])); result+= "+"; result+= String.valueOf(Integer.valueOf(temA[0]) * Integer.valueOf(temB[1]) + Integer.valueOf(temA[1]) * Integer.valueOf(temB[0])); result+= "i"; return result; }}
最快的代码是下面这个
public class Solution { public String complexNumberMultiply(String a, String b) { int index = a.indexOf("+"); int r1 = Integer.parseInt(a.substring(0, index)); int c1 = Integer.parseInt(a.substring(index + 1, a.length() - 1)); index = b.indexOf("+"); int r2 = Integer.parseInt(b.substring(0, index)); int c2 = Integer.parseInt(b.substring(index + 1, b.length() - 1)); int r = r1 * r2 - c1 * c2; int c = r1 * c2 + r2 * c1; return r + "+" + c + "i"; }}
欢迎加入中科院开源软件自习室:631696396
- 537. Complex Number Multiplication
- 537. Complex Number Multiplication
- 537. Complex Number Multiplication
- 537. Complex Number Multiplication
- 537. Complex Number Multiplication
- 537. Complex Number Multiplication
- 537. Complex Number Multiplication
- 537. Complex Number Multiplication
- 537. Complex Number Multiplication
- 537. Complex Number Multiplication
- 537. Complex Number Multiplication
- 537. Complex Number Multiplication
- 537. Complex Number Multiplication
- 537. Complex Number Multiplication
- 537.Complex Number Multiplication
- Leetcode-537. Complex Number Multiplication
- leetcode 537. Complex Number Multiplication
- LeetCode 537. Complex Number Multiplication
- STM32库文件结构及CMSIS介绍
- 代码操作数据库性能优化总结
- ThinkPhp5 模型查询结果转化成数组
- VUE2 前端实现 静态二级省市联动选择select
- mybatise+oracle添加数据时如果数据存在就更新,如果不存在就插入
- 537. Complex Number Multiplication
- DOS命令:跨网段访问设置route
- Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded b
- 事故分析
- 线性最小二乘
- 根据Url参数调用具体的方法
- UE4-利用UMG动画制作场景切换Loading进度条
- JQ 获取祖、父、兄、子节点方法
- iOS 开发系统自带摇一摇