Skip to content

67. 二进制求和

67. 二进制求和

代码

javascript
// 67. 二进制求和:https://leetcode.cn/problems/add-binary/description/
// 输入:a = "11", b = "1"
// 输出:"100"

export function addBinary (a, b) {
  const len = Math.max(a.length, b.length)
  a.length === len ? (b = b.padStart(len, '0')) : (a = a.padStart(len, '0'))
  let ad = 0
  let res = ''
  for (let idx = len - 1; idx >= 0; idx--) {
    const cur = Number(a[idx]) + Number(b[idx]) + ad
    if (cur > 1) {
      res = `${cur % 2}${res}`
      ad = 1
    } else {
      res =  `${cur}${res}`
      ad = 0
    }
  }
  if (ad > 0) {
    res =  `${ad}${res}`
  }
  return res
}
typescript
// 67. 二进制求和:https://leetcode.cn/problems/add-binary/description/
// 输入:a = "11", b = "1"
// 输出:"100"

export function addBinary (a: string, b: string): string {
  const len = Math.max(a.length, b.length)
  a.length === len ? (b = b.padStart(len, '0')) : (a = a.padStart(len, '0'))
  let ad = 0
  let res = ''
  for (let idx = len - 1; idx >= 0; idx--) {
    const cur = Number(a[idx]) + Number(b[idx]) + ad
    if (cur > 1) {
      res = `${cur % 2}${res}`
      ad = 1
    } else {
      res =  `${cur}${res}`
      ad = 0
    }
  }
  if (ad > 0) {
    res =  `${ad}${res}`
  }
  return res
}

测试代码

ts
import { expect, test } from 'vitest'
import { addBinary } from './typescript.ts'
import { addBinary as addBinaryJs } from './javascript.js'

test(`addBinary`, () => {
  expect(addBinary('11', '1')).toBe('100')
  expect(addBinary('10', '111')).toBe('1001')
})

test(`addBinaryJs`, () => {
  expect(addBinaryJs('11', '1')).toBe('100')
  expect(addBinaryJs('10', '111')).toBe('1001')
})