537. Complex Number Multiplication

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.
class Solution:
    def complexNumberMultiply(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        n0=int(a.split(‘+‘)[0])
        n1=int(a.split(‘+‘)[1].split(‘i‘)[0])
        m0=int(b.split(‘+‘)[0])
        m1=int(b.split(‘+‘)[1].split(‘i‘)[0])
        return str(n0*m0-n1*m1)+str(‘+‘)+str(n0*m1+n1*m0)+str(‘i‘)


时间: 2024-08-11 01:20:18

537. Complex Number Multiplication的相关文章

537 Complex Number Multiplication 复数乘法

详见:https://leetcode.com/problems/complex-number-multiplication/description/ C++: class Solution { public: string complexNumberMultiply(string a, string b) { int n1 = a.size(), n2 = b.size(); auto p1 = a.find_last_of("+"), p2 = b.find_last_of(&qu

Complex Number Multiplication

public class ComplexNumberMultiplicationSolution { public static String complexNumberMultiply(String a, String b) { int p1 = Integer.parseInt(a.split("\\+")[0]); int n1 = Integer.parseInt(a.split("\\+")[1].split("i")[0]); int

URAL 1748. The Most Complex Number 反素数

题目来源:URAL 1748. The Most Complex Number 题意:求一个小于等于n的因子最多的数 思路:搜索+剪枝 #include <cstdio> #include <cstring> using namespace std; typedef unsigned __int64 LL; LL prime[16] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53}; LL num, sum, n; void dfs(i

URAL 1748. The Most Complex Number(反素数)

题目链接 题意 :给你一个n,让你找出小于等于n的数中因子个数最多的那个数,并且输出因子个数,如果有多个答案,输出数最小的那个 思路 : 官方题解 : (1)此题最容易想到的是穷举,但是肯定超时. (2)我们可以知道,计算约数的个数和质因数分解有着很大的联系: 若Q的质因数分解为:Q=p1^k1*p2^k2*…*pm^km(p1…pm为素数,k1…km≥1),则Q有(k1+1)(k2+1)…(km+1)个约数.但是质因数分解的时间复杂度很高,所以也会超时. (3)通过以上的公式,我们可以“突发奇

4-30 Java正则匹配

做CC时经常要用正则表达式过滤数据,当时洗的数据比较复杂,规则比较多.这次做leetcode,复习一下Java的正则匹配.Leetcode 537. Complex Number Multiplication 从表示复数的字符串里把实部和虚部取出来. http://blog.csdn.net/yin380697242/article/details/52049999 Pattern类,构造函数是私有类型,不能通过new新建,要通过Pattern.compile()获得一个匹配模式.Pattern

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

【LeetCode】数学(共106题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers [7]Reverse Integer [8]String to Integer (atoi) [9]Palindrome Number [12]Integer to Roman [13]Roman to Integer [29]Divide Two Integers [43]Multiply Strings [50]Pow(x,

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman