算法-栈-十进制转二进制
程序员文章站
2022-07-15 09:35:52
...
class Stack {
items = []
push = (e) => {
this.items.push(e)
}
pop = () => {
return this.items.pop()
}
peek = () => {
return this.items[this.items.length - 1]
}
isEmpty = () => {
return this.items.length === 0
}
size = () => {
return this.items.length
}
toString = () => {
return this.items.join('')
}
}
let stack = new Stack()
//十进制转二进制
test = (x) => {
const a = x % 2
stack.push(a)
const aa = parseInt(x / 2, 10)
if (aa !== 0) {
test(aa)
}
}
test(1000)
console.log('====www===', stack.toString())
上一篇: c++ 十进制转二进制
下一篇: 这道题其实我们都走错方向了?