作业

来源:3-

郑嫑嫑94

2020-10-10

class Stack {
  constructor(stack) {
    this.stack = stack || [];
  }

  push(value) {
    this.stack.push(value);
  }

  pop() {
    return this.stack.pop();
  }

  peek() {
    return this.stack[this.stack.length - 1];
  }

  getLength() {
    return this.stack.length
  }
}

function tenToTwo(num) {
  const stack = new Stack();
  let str = ''

  while (num > 0) {
    stack.push(num % 2);
    num = Math.floor(num / 2);
  }

  const len = stack.getLength();
  for (let i = 0; i < len; i++) {
    str += stack.pop();
  }

  return parseInt(str)
}

console.log(tenToTwo(100))
写回答

1回答

lewis

2020-10-10

good

0
0

JavaScript版数据结构与算法 轻松解决前端算法面试

夯实算法基础,填补技术短板,助力面试考题最后一公里

2484 学习 · 683 问题

查看课程