Skip to content

Commit

Permalink
feat: 增加 67. 二进制求和
Browse files Browse the repository at this point in the history
  • Loading branch information
fxss5201 committed Aug 25, 2024
1 parent 081d298 commit 09645c3
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/.vitepress/leetcodeItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ const leetcodeItems = [
"text": "66. 加一",
"link": "/leetcode/plusOne"
},
{
"text": "67. 二进制求和",
"link": "/leetcode/addBinary"
},
{
"text": "83. 删除排序链表中的重复元素",
"link": "/leetcode/removeDuplicatesFromSortedList"
Expand Down
12 changes: 12 additions & 0 deletions docs/leetcode/addBinary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 67. 二进制求和

[67. 二进制求和](https://leetcode.cn/problems/add-binary/description/)

## 代码

::: code-group

<<< ../../src/leetcode/addBinary/javascript.js{javascript} [javascript]
<<< ../../src/leetcode/addBinary/typescript.ts{typescript} [typescript]

:::
1 change: 1 addition & 0 deletions docs/leetcode/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
- [63. 不同路径 II](./uniquePathsIi)
- [64. 最小路径和](./minimumPathSum)
- [66. 加一](./plusOne)
- [67. 二进制求和](./addBinary)
- [83. 删除排序链表中的重复元素](./removeDuplicatesFromSortedList)
- [2085. 统计出现过一次的公共字符串](./countCommonWordsWithOneOccurrence)
- [2182. 构造限制重复的字符串](./constructStringWithRepeatLimit)
Expand Down
24 changes: 24 additions & 0 deletions src/leetcode/addBinary/javascript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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
}
24 changes: 24 additions & 0 deletions src/leetcode/addBinary/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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
}
13 changes: 13 additions & 0 deletions test/leetcode/addBinary.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect, test } from 'vitest'
import { addBinary } from '../../src/leetcode/addBinary/typescript.ts'
import { addBinary as addBinaryJs } from '../../src/leetcode/addBinary/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')
})

0 comments on commit 09645c3

Please sign in to comment.