add: 只出现一次的数字等

This commit is contained in:
2019-03-10 16:43:04 +08:00
committed by yi-ge
parent 930ff1be11
commit 8239e7afd5
106 changed files with 7978 additions and 0 deletions

24
src/array/gray-code.js Normal file
View File

@ -0,0 +1,24 @@
// LeetCode 89. 格雷编码 https://leetcode-cn.com/problems/gray-code/
// LintCode 411. 格雷编码 https://www.lintcode.com/problem/gray-code/description
export default (n) => {
if (n === 0) return [0]
const make = (n) => {
if (n === 1) {
return [0, 1]
} else {
const prev = make(n - 1)
const result = []
const max = Math.pow(2, n) - 1
for (let i = 0, len = prev.length; i < len; i++) {
result[i] = `0${prev[i]}`
result[max - i] = `1${prev[i]}`
}
return result
}
}
return make(n).map(val => {
return parseInt(val, 2)
})
}