一、手写算法

https://leetcode-cn.com/problems/implement-queue-using-stacks/

思路


  • 代码

    ```javascript var MyQueue = function() { this.len = 0; this.queue = []; };

/**

  • @param {number} x
  • @return {void} */ MyQueue.prototype.push = function(x) { this.queue[this.len] = x; this.len++; };

/**

  • @return {number} */ MyQueue.prototype.pop = function() { if(this.len > 0){ var a = this.queue.shift(); this.len—; return a; }else{ return -1 } };

/**

  • @return {number} */ MyQueue.prototype.peek = function() { if(this.len > 0){ return this.queue[0]; }else{ return -1 } };

/**

  • @return {boolean} */ MyQueue.prototype.empty = function() { return this.queue.length === 0 ? true :false };

/**

  • Your MyQueue object will be instantiated and called as such:
  • var obj = new MyQueue()
  • obj.push(x)
  • var param_2 = obj.pop()
  • var param_3 = obj.peek()
  • var param_4 = obj.empty() */ ```

    复杂度分析

  • 时间复杂度:
  • 空间复杂度:

二、编程题

  1. // 手写题:https://bigfrontend.dev/zh/problem/the-angle-between-hour-hand-and-minute-hand-of-a-clock
  1. /**
  2. * Second
  3. * @param {string} time
  4. * @returns {number}
  5. */
  6. function angle(time) {
  7. // your code here
  8. let [hours, minus] = time.split(':');
  9. let hourAngle = (360 / 12) * (hours % 12);// 小时每大格度数 * 小时 (% 12 ,24小时制)
  10. let minusAngle = (360 / 12 / 5) * minus;// 分钟每格角度*分钟
  11. let pyAngle = (30 / 60) * minus;// 时分偏移量 30度 * x = 60分(x是每走一分钟,小时的偏移量)
  12. let angle = Math.abs(hourAngle + pyAngle - minusAngle);/求夹角
  13. return Math.round(Math.min(angle, 360 - angle));// 最大为180度,求小角,并四舍五入
  14. }
  15. /**
  16. * First
  17. * @param {string} time
  18. * @returns {number}
  19. */
  20. function angle(time){
  21. var a = time.split(':');
  22. if(a[0] === '12' && a[1] === '00'){
  23. var b = a[1] * 6 === 0 ? a[1] * 6 :Math.abs((a[0] > 12 ? a[0] - 12 : a[0] )* 30 + 0.5 * a[1] - a[1] * 6)
  24. }else{
  25. if(a[0] === '12' && Number(a[1]) > 30){
  26. var b = Math.round(Math.abs(a[0]> 12 ? a[0] - 12 : a[0] * 30 + 0.5 * a[1] - a[1] * 6) )
  27. }else{
  28. var b = Math.abs(Math.abs((a[0] >= 12 ? a[0] - 12 : a[0]) * 30 + 0.5 * Number(a[1])) - Math.abs(a[1] * 6))
  29. if(b>180){
  30. b = Math.round(360-b)
  31. }
  32. else{
  33. b=Math.round(b)
  34. }
  35. }
  36. }
  37. return b
  38. }