<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>circular Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/circular/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/circular/</link>
	<description></description>
	<lastBuildDate>Sun, 15 Jul 2018 04:21:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>circular Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/circular/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 641. Design Circular Deque</title>
		<link>https://zxi.mytechroad.com/blog/data-structure/leetcode-641-design-circular-deque/</link>
					<comments>https://zxi.mytechroad.com/blog/data-structure/leetcode-641-design-circular-deque/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 15 Jul 2018 03:58:04 +0000</pubDate>
				<category><![CDATA[Data Structure]]></category>
		<category><![CDATA[circular]]></category>
		<category><![CDATA[deque]]></category>
		<category><![CDATA[desgin]]></category>
		<category><![CDATA[easy]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3147</guid>

					<description><![CDATA[<p>Problem Design your implementation of the circular double-ended queue (deque). Your implementation should support following operations: MyCircularDeque(k): Constructor, set the size of the deque to&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/data-structure/leetcode-641-design-circular-deque/">花花酱 LeetCode 641. Design Circular Deque</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Design your implementation of the circular double-ended queue (deque).<br />
Your implementation should support following operations:</p>
<ul>
<li>MyCircularDeque(k): Constructor, set the size of the deque to be k.</li>
<li>insertFront(): Adds an item at the front of Deque. Return true if the operation is successful.</li>
<li>insertLast(): Adds an item at the rear of Deque. Return true if the operation is successful.</li>
<li>deleteFront(): Deletes an item from the front of Deque. Return true if the operation is successful.</li>
<li>deleteLast(): Deletes an item from the rear of Deque. Return true if the operation is successful.</li>
<li>getFront(): Gets the front item from the Deque. If the deque is empty, return -1.</li>
<li>getRear(): Gets the last item from Deque. If the deque is empty, return -1.</li>
<li>isEmpty(): Checks whether Deque is empty or not.</li>
<li>isFull(): Checks whether Deque is full or not.</li>
</ul>
<p><strong>Example:</strong></p>
<pre class="crayon:false ">MyCircularDeque circularDeque = new MycircularDeque(3); // set the size to be 3
circularDeque.insertLast(1);			// return true
circularDeque.insertLast(2);			// return true
circularDeque.insertFront(3);			// return true
circularDeque.insertFront(4);			// return false, the queue is full
circularDeque.getRear();  				// return 32
circularDeque.isFull();				// return true
circularDeque.deleteLast();			// return true
circularDeque.insertFront(4);			// return true
circularDeque.getFront();				// return 4
</pre>
<p><strong>Note:</strong></p>
<ul>
<li>All values will be in the range of [1, 1000].</li>
<li>The number of operations will be in the range of [1, 1000].</li>
<li>Please do not use the built-in Deque library.</li>
</ul>
<h1><strong>Solution</strong></h1>
<p>Using head and tail to pointer to the head and the tail in the circular buffer.</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 20 ms
class MyCircularDeque {
public:
  /** Initialize your data structure here. Set the size of the deque to be k. */
  MyCircularDeque(int k): k_(k), q_(k), head_(1), tail_(-1), size_(0) {}

  /** Adds an item at the front of Deque. Return true if the operation is successful. */
  bool insertFront(int value) {
    if (isFull()) return false;
    head_ = (head_ - 1 + k_) % k_;
    q_[head_] = value;
    if (size_ == 0) tail_ = head_;
    ++size_;
    return true;
  }

  /** Adds an item at the rear of Deque. Return true if the operation is successful. */
  bool insertLast(int value) {
    if (isFull()) return false;
    tail_ = (tail_ + 1 + k_) % k_;
    q_[tail_] = value;
    if (size_ == 0) head_ = tail_;
    ++size_;
    return true;  
  }

  /** Deletes an item from the front of Deque. Return true if the operation is successful. */
  bool deleteFront() {
    if (isEmpty()) return false;
    head_ = (head_ + 1 + k_) % k_;
    --size_;
    return true;
  }

  /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
  bool deleteLast() {
    if (isEmpty()) return false;
    tail_ = (tail_ - 1 + k_) % k_;
    --size_;
    return true;
  }

  /** Get the front item from the deque. */
  int getFront() {
    if (isEmpty()) return -1;
    return q_[head_];
  }

  /** Get the last item from the deque. */
  int getRear() {
    if (isEmpty()) return -1;
    return q_[tail_];
  }

  /** Checks whether the circular deque is empty or not. */
  bool isEmpty() {
    return size_ == 0;
  }

  /** Checks whether the circular deque is full or not. */
  bool isFull() {
    return size_ == k_;
  }
private:
  const int k_;
  int head_;
  int tail_;
  int size_;
  vector&lt;int&gt; q_;
};</pre><p></p>
<h1><strong>Related Problems</strong></h1>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/uncategorized/leetcode-622-design-circular-queue/">花花酱 LeetCode 622. Design Circular Queue</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/data-structure/leetcode-641-design-circular-deque/">花花酱 LeetCode 641. Design Circular Deque</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/data-structure/leetcode-641-design-circular-deque/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
