Redian新闻
>
LeetCode 力扣官方题解 | 537. 复数乘法

LeetCode 力扣官方题解 | 537. 复数乘法

公众号新闻

537.复数乘法(点击文末阅读原文查看题目)

题目描述

复数 可以用字符串表示,遵循 "实部+虚部 i" 的形式,并满足下述条件:

  • 实部 是一个整数,取值范围是 [-100, 100]

  • 虚部 也是一个整数,取值范围是 [-100, 100]

  • i2 == -1

给你两个字符串表示的复数 num1 和 num2 ,请你遵循复数表示形式,返回表示它们乘积的字符串。



示例 1:

输入:num1 = "1+1i", num2 = "1+1i"输出:"0+2i"解释:(1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i ,你需要将它转换为 0+2i 的形式。


示例 2:

输入:num1 = "1+-1i", num2 = "1+-1i"输出:"0+-2i"解释:(1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i ,你需要将它转换为 0+-2i 的形式。 


提示:

  • num1 和 num2 都是有效的复数表示。


解决方案


方法一:模拟


思路


复数可以写成 a + bi 的形式,其中 a,b ∈ R,a 是实部,b 是虚部,i 是虚数单位,i= −1。

对于给定的两个复数 num1 num2 ,首先分别得到两个复数的实部和虚部,然后计算两个复数的乘法。用 real1 和 imag分别表示 num的实部和虚部,用 realimag分别表示 num2 的实部和虚部,则两个复数的乘法计算如下:

(real1 + imag1 × i) × (real2 + imag× i)


= real× real2 + real1 × imag2 × i + imag1 × real2 × i + imag1 × imag2 × i2

 

= real× real2 + real1 × imag2 × i + imag1 × real2 × i − imag1 × imag2


= (real× real2 − imag1 × imag2 ) + (real1 × imag2 + imag1 × real2) × i


得到两个复数的乘积之后,将乘积转换成复数格式的字符串并返回。


代码

Python3

class Solution:    def complexNumberMultiply(self, num1: str, num2: str) -> str:        real1, imag1 = map(int, num1[:-1].split('+'))        real2, imag2 = map(int, num2[:-1].split('+'))        return f'{real1 * real2 - imag1 * imag2}+{real1 * imag2 + imag1 * real2}i'

Java

class Solution {    public String complexNumberMultiply(String num1, String num2) {        String[] complex1 = num1.split("\\+|i");        String[] complex2 = num2.split("\\+|i");        int real1 = Integer.parseInt(complex1[0]);        int imag1 = Integer.parseInt(complex1[1]);        int real2 = Integer.parseInt(complex2[0]);        int imag2 = Integer.parseInt(complex2[1]);        return String.format("%d+%di", real1 * real2 - imag1 * imag2, real1 * imag2 + imag1 * real2);    }}


C#

public class Solution {    public string ComplexNumberMultiply(string num1, string num2) {        string[] complex1 = num1.Split(new char[2]{'+','i'});        string[] complex2 = num2.Split(new char[2]{'+','i'});        int real1 = int.Parse(complex1[0]);        int imag1 = int.Parse(complex1[1]);        int real2 = int.Parse(complex2[0]);        int imag2 = int.Parse(complex2[1]);        return string.Format("{0}+{1}i", real1 * real2 - imag1 * imag2, real1 * imag2 + imag1 * real2);    }}


C++

class Solution {public:    string complexNumberMultiply(string num1, string num2) {        regex re("\\+|i");         vector<string> complex1(sregex_token_iterator(num1.begin(), num1.end(), re, -1), std::sregex_token_iterator());        vector<string> complex2(sregex_token_iterator(num2.begin(), num2.end(), re, -1), std::sregex_token_iterator());        int real1 = stoi(complex1[0]);        int imag1 = stoi(complex1[1]);        int real2 = stoi(complex2[0]);        int imag2 = stoi(complex2[1]);        return to_string(real1 * real2 - imag1 * imag2) + "+" + to_string(real1 * imag2 + imag1 * real2) + "i";    }};


C

bool parseComplexNumber(const char * num, int * real, int * image) {    char *token = strtok(num, "+");    *real = atoi(token);    token = strtok(NULL, "i");    *image = atoi(token);    return true;};
char * complexNumberMultiply(char * num1, char * num2){ int real1 = 0, imag1 = 0; int real2 = 0, imag2 = 0; char * res = (char *)malloc(sizeof(char) * 20); parseComplexNumber(num1, &real1, &imag1); parseComplexNumber(num2, &real2, &imag2); snprintf(res, 20, "%d+%di", real1 * real2 - imag1 * imag2, real1 * imag2 + imag1 * real2); return res;}


JavaScript

var complexNumberMultiply = function(num1, num2) {    const complex1 = [num1.split("+")[0], num1.split("+")[1].split("i")[0]];    const complex2 = [num2.split("+")[0], num2.split("+")[1].split("i")[0]];    const real1 = parseInt(complex1[0]);    const imag1 = parseInt(complex1[1]);    const real2 = parseInt(complex2[0]);    const imag2 = parseInt(complex2[1]);    return '' + real1 * real2 - imag1 * imag2 + '+' + (real1 * imag2 + imag1 * real2) + 'i';};

Golang

func parseComplexNumber(num string) (real, imag int) {    i := strings.IndexByte(num, '+')    real, _ = strconv.Atoi(num[:i])    imag, _ = strconv.Atoi(num[i+1 : len(num)-1])    return}
func complexNumberMultiply(num1, num2 string) string { real1, imag1 := parseComplexNumber(num1) real2, imag2 := parseComplexNumber(num2) return fmt.Sprintf("%d+%di", real1*real2-imag1*imag2, real1*imag2+imag1*real2)}


复杂度分析

  • 时间复杂度:O(1)。由于两个复数字符串的长度都很小,因此可以将字符串处理的时间视为常数。

  • 空间复杂度:O(1)


BY / 

本文作者:力扣

编辑&版式:Irene

声明:本文归“力扣”版权所有,如需转载请联系。


点个在看,少个 bug

微信扫码关注该文公众号作者

戳这里提交新闻线索和高质量文章给我们。
相关阅读
留学圈疯传!商科生转码《Leetcode刷题指南》,信息量太大AlphaTensor横空出世!打破矩阵乘法计算速度50年纪录,DeepMind新研究再刷Nature封面,详细算法已开源另一种“推翻” VS Code 的尝试:JetBrains Fleet 现开放公测题都白刷了?Amazon导师亲自为你揭秘LeetCode刷题真相!LeetCode 力扣官方题解 | 521. 最长特殊序列 Ⅰ文科老留,再也不用担心被解雇了 (中)小毕娶了日本媳妇(二)发现了《Leetcode刷题指南》,我半年成功转码上岸DeepMind再登Nature封面!推出AlphaTensor:强化学习发现矩阵乘法算法强化学习发现矩阵乘法算法,DeepMind再登Nature封面推出AlphaTensor50年悬而未决的矩阵乘法难题,被DeepMind的新式算法攻克了LeetCode 力扣官方题解 | 2016. 增量元素之间的最大差值人类反超 AI:DeepMind 用 AI 打破矩阵乘法计算速度 50 年记录一周后,数学家再次刷新2022傅雷翻译出版奖获奖者揭晓 Le palmarès du Prix Fu Lei 2022 dévoilé商科留学生转码必备:Leetcode到底该怎么刷?拿下6家offer经验分享:刷LeetCode这件事,其实不靠努力!谷歌亚麻,越来越嫌弃Leetcode刷题家…LeetCode 力扣官方题解 | 969. 煎饼排序挑战刷最少的题进大厂!不会刷leetcode的人,救星来了35岁被裁,刷LeetCode变成了自我感动...LeetCode 力扣官方题解 | 258. 各位相加LeetCode 力扣官方题解 | 6. Z 字形变换LeetCode 力扣官方题解 | 917. 仅仅反转字母LeetCode 力扣官方题解 | 838. 推多米诺气候变化 | IEEE 2023主席Saifur Rahman和IEEE代表团出席COP27Leetcode出题人,揭秘打工体验!马克谈天下(330) 聊聊公投那些事LeetCode 力扣官方题解 | 1791. 找出星型图的中心节点人到中年-对财富承担责任LeetCode刷完200道题,我竟然收到了亚麻的拒信...打卡LeetCode的第100天,我才发现......深度好文|打卡LeetCode的第100天,我才发现......白云升起的地方:尼加拉大瀑布GitHub疯传!求职亚麻的留学生,请立即停止刷LeetCode!LeetCode 力扣官方题解 | 2104. 子数组范围和
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。