Skip to content

Latest commit

 

History

History
33 lines (27 loc) · 776 Bytes

537.复数乘法.md

File metadata and controls

33 lines (27 loc) · 776 Bytes

537.复数乘法

/*
 * @lc app=leetcode.cn id=537 lang=typescript
 *
 * [537] 复数乘法
 */

// @lc code=start
function complexNumberMultiply(num1: string, num2: string): string {}
// @lc code=end

解法 1: 模拟

function complexNumberMultiply(num1: string, num2: string): string {
  const [[a1, b1], [a2, b2]] = [num1, num2].map(num => num.split('+').map(i => parseInt(i)))
  return `${a1 * a2 - b1 * b2}+${a1 * b2 + a2 * b1}i`
}

Case

test.each([
  { input: { num1: '1+1i', num2: '1+1i' }, output: '0+2i' },
  { input: { num1: '1+-1i', num2: '1+-1i' }, output: '0+-2i' },
])('input: num1 = $input.num1, num2 = $input.num2', ({ input: { num1, num2 }, output }) => {
  expect(complexNumberMultiply(num1, num2)).toEqual(output)
})