3-7
来源:1-1 课程介绍

还未曾去过倒悬山
2021-04-09
1、请用 ES6 的 class,封装一个 Stack 类,包括 push、pop、peek 方法。
class Stack {
constructor(){
this.stack = []
}
push(val) {
this.stack.push(val)
}
pop() {
return this.stack.pop()
}
peek() {
return this.stack[this.stack.length -1]
}
}
2、请用栈这个数据结构,将100这个十进制数字转为二进制。
let stack = []
let string = ''
function staFn (num) {
let newNum = parseInt((num / 2))
if( newNum != 0){
stack.push(num%2)
staFn(newNum)
}else{
stack.push(num%2)
while(stack.length) {
string += stack.pop()
}
return stack
}
}
写回答
1回答
-
lewis
2021-04-10
good
112021-04-10
相似问题