题目描述:

设置循环双端队列 - 图1

代码实现:

  • 利用数组实现
  1. /**
  2. * Initialize your data structure here. Set the size of the deque to be k.
  3. * @param {number} k
  4. */
  5. var MyCircularDeque = function(k) {
  6. this.arr = []
  7. this.size = k
  8. };
  9. /**
  10. * Adds an item at the front of Deque. Return true if the operation is successful.
  11. * @param {number} value
  12. * @return {boolean}
  13. */
  14. MyCircularDeque.prototype.insertFront = function(value) {
  15. if (this.size === this.arr.length) {
  16. return false
  17. } else {
  18. this.arr.unshift(value)
  19. return true
  20. }
  21. };
  22. /**
  23. * Adds an item at the rear of Deque. Return true if the operation is successful.
  24. * @param {number} value
  25. * @return {boolean}
  26. */
  27. MyCircularDeque.prototype.insertLast = function(value) {
  28. if (this.size === this.arr.length) {
  29. return false
  30. } else {
  31. this.arr.push(value)
  32. return true
  33. }
  34. };
  35. /**
  36. * Deletes an item from the front of Deque. Return true if the operation is successful.
  37. * @return {boolean}
  38. */
  39. MyCircularDeque.prototype.deleteFront = function() {
  40. if (this.arr.length === 0) {
  41. return false
  42. } else {
  43. this.arr.shift()
  44. return true
  45. }
  46. };
  47. /**
  48. * Deletes an item from the rear of Deque. Return true if the operation is successful.
  49. * @return {boolean}
  50. */
  51. MyCircularDeque.prototype.deleteLast = function() {
  52. if (this.arr.length === 0) {
  53. return false
  54. } else {
  55. this.arr.pop()
  56. return true
  57. }
  58. };
  59. /**
  60. * Get the front item from the deque.
  61. * @return {number}
  62. */
  63. MyCircularDeque.prototype.getFront = function() {
  64. if (this.arr.length === 0) {
  65. return -1
  66. } else {
  67. return this.arr[0]
  68. }
  69. };
  70. /**
  71. * Get the last item from the deque.
  72. * @return {number}
  73. */
  74. MyCircularDeque.prototype.getRear = function() {
  75. if (this.arr.length === 0) {
  76. return -1
  77. } else {
  78. return this.arr[this.arr.length - 1]
  79. }
  80. };
  81. /**
  82. * Checks whether the circular deque is empty or not.
  83. * @return {boolean}
  84. */
  85. MyCircularDeque.prototype.isEmpty = function() {
  86. if (this.arr.length === 0) {
  87. return true
  88. }
  89. return false
  90. };
  91. /**
  92. * Checks whether the circular deque is full or not.
  93. * @return {boolean}
  94. */
  95. MyCircularDeque.prototype.isFull = function() {
  96. if (this.size === this.arr.length) {
  97. return true
  98. }
  99. return false
  100. };
  101. /**
  102. * Your MyCircularDeque object will be instantiated and called as such:
  103. * var obj = new MyCircularDeque(k)
  104. * var param_1 = obj.insertFront(value)
  105. * var param_2 = obj.insertLast(value)
  106. * var param_3 = obj.deleteFront()
  107. * var param_4 = obj.deleteLast()
  108. * var param_5 = obj.getFront()
  109. * var param_6 = obj.getRear()
  110. * var param_7 = obj.isEmpty()
  111. * var param_8 = obj.isFull()
  112. */

设置循环双端队列 - 图2