LeetCode 537 Complex Number Multiplication

537 Complex Number Multiplication

https://leetcode.com/problems/complex-number-multiplication/


A complex number can be represented as a string on the form "**real**+**imaginary**i" where:

  • real is the real part and is an integer in the range [-100, 100].
  • imaginary is the imaginary part and is an integer in the range [-100, 100].
  • i2 == -1.

Given two complex numbers num1 and num2 as strings, return a string of the complex number that represents their multiplications.

Example 1:

Input: num1 = "1+1i", num2 = "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: num1 = "1+-1i", num2 = "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.

Constraints:

  • num1 and num2 are valid complex numbers.

Basic Solution

思路很简单,从两个字符串中提取出复数的实部和虚部,交叉相乘得到乘积的实部和虚部,按题目格式返回复数字符串。重点在于字符串的处理。

第一遍写得比较冗长,遍历字符串,找到 '+' 时分割,得到实部和虚部;返回时注意加上 '+' 'i'

时间复杂度 O(n),空间复杂度 O(1)

class Solution {
public:
    string complexNumberMultiply(string num1, string num2) {
        int a1, b1, a2, b2, c1, c2;
        string s, ans;
        for(int i = 0; i < num1.size(); ++i){
            if(num1[i] == '+'){
                a1 = stoi(num1.substr(0, i));
                s = num1.substr(i+1);
                s.pop_back();
                b1 = stoi(s);
                break;
            }
        }
        for(int i = 0; i < num2.size(); ++i){
            if(num2[i] == '+'){
                a2 = stoi(num2.substr(0, i));
                s = num2.substr(i+1);
                s.pop_back();
                b2 = stoi(s);
                break;
            }
        }
        c1 = a1 * a2 - b1 * b2;
        c2 = a1 * b2 + a2 * b1;
        ans = to_string(c1);
        s = to_string(c2);
        ans.append(1, '+');		// ans.push_back('+');
        ans += s;
        ans.append(1, 'i');		// ans.push_back('i');
        return ans;
    }
};

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Complex Number Multiplication.

Memory Usage: 5.9 MB, less than 61.85% of C++ online submissions for Complex Number Multiplication.


Optimization

代码可以写得很精炼,利用 sscanf 直接得到整数部分,返回时也可以直接完成字符串拼接操作。重点是 c_str 的格式化读取。

class Solution {
public:
    string complexNumberMultiply(string num1, string num2) {
        int a1, b1, a2, b2;
        sscanf(num1.c_str(), "%d+%di", &a1, &b1);
        sscanf(num2.c_str(), "%d+%di", &a2, &b2);
        return to_string(a1 * a2 - b1 * b2) + "+" + to_string(a1 * b2 + a2 * b1) + "i";
    }
};

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Complex Number Multiplication.

Memory Usage: 6 MB, less than 61.85% of C++ online submissions for Complex Number Multiplication.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值