LeetCode Complex Number Multiplication
2017-10-24 13:46
513 查看
原题链接在这里: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:
Example 2:
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:
题目:
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:
1 class Solution { 2 public String complexNumberMultiply(String a, String b) { 3 String [] aArr = a.split("\\+|i"); 4 String [] bArr = b.split("\\+|i"); 5 int aReal = Integer.valueOf(aArr[0]); 6 int aImag = Integer.valueOf(aArr[1]); 7 int bReal = Integer.valueOf(bArr[0]); 8 int bImag = Integer.valueOf(bArr[1]); 9 return (aReal*bReal-aImag*bImag) + "+" + (aReal*bImag+aImag*bReal) + "i"; 10 } 11 }
相关文章推荐
- [LeetCode] Complex Number Multiplication 复数相乘
- [leetcode]-537 Complex Number Multiplication
- leetcode537: Complex Number Multiplication
- LeetCode - Complex Number Multiplication
- [LeetCode-Algorithms-537] "Complex Number Multiplication" (2017.12.7-WEEK14)
- [Leetcode] #537 Complex Number Multiplication(stringstream)
- leetcode-complex number multiplication
- leetcode 537 Complex Number Multiplication(Medium)
- lintcode/leetcode由易至难第22题:Complex Number Multiplication
- Complex Number Multiplication
- 537. Complex Number Multiplication
- LeetCode 668: Kth Smallest Number in Multiplication Table
- 537. Complex Number Multiplication
- [Leetcode] 668. Kth Smallest Number in Multiplication Table 解题报告
- Complex Number Multiplication
- 537. Complex Number Multiplication
- 537. Complex Number Multiplication
- Complex Number Multiplication
- 537. Complex Number Multiplication
- [LeetCode] Kth Smallest Number in Multiplication Table 乘法表中的第K小的数字