<?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>desgin Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/desgin/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/desgin/</link>
	<description></description>
	<lastBuildDate>Sun, 15 Mar 2020 08:37:17 +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>desgin Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/desgin/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1381. Design a Stack With Increment Operation</title>
		<link>https://zxi.mytechroad.com/blog/stack/leetcode-1381-design-a-stack-with-increment-operation/</link>
					<comments>https://zxi.mytechroad.com/blog/stack/leetcode-1381-design-a-stack-with-increment-operation/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 15 Mar 2020 08:30:22 +0000</pubDate>
				<category><![CDATA[Stack]]></category>
		<category><![CDATA[data structure]]></category>
		<category><![CDATA[desgin]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[stack]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6497</guid>

					<description><![CDATA[<p>Design a stack which supports the following operations. Implement the&#160;CustomStack&#160;class: CustomStack(int maxSize)&#160;Initializes the object with&#160;maxSize&#160;which is the maximum number of elements in the stack or&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/stack/leetcode-1381-design-a-stack-with-increment-operation/">花花酱 LeetCode 1381. Design a Stack With Increment Operation</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Design a stack which supports the following operations.</p>



<p>Implement the&nbsp;<code>CustomStack</code>&nbsp;class:</p>



<ul><li><code>CustomStack(int maxSize)</code>&nbsp;Initializes the object with&nbsp;<code>maxSize</code>&nbsp;which is the maximum number of elements in the stack or do nothing if the stack reached the&nbsp;<code>maxSize</code>.</li><li><code>void push(int x)</code>&nbsp;Adds&nbsp;<code>x</code>&nbsp;to the top of the stack if the stack hasn&#8217;t reached the&nbsp;<code>maxSize</code>.</li><li><code>int pop()</code>&nbsp;Pops and returns the top of stack or&nbsp;<strong>-1</strong>&nbsp;if the stack is empty.</li><li><code>void inc(int k, int val)</code>&nbsp;Increments the bottom&nbsp;<code>k</code>&nbsp;elements of the stack by&nbsp;<code>val</code>. If there are less than&nbsp;<code>k</code>&nbsp;elements in the stack, just increment all the elements in the stack.</li></ul>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted wp-block-preformatted;crayon:false"><strong>Input</strong>
["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"]
[[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]
<strong>Output</strong>
[null,null,null,2,null,null,null,null,null,103,202,201,-1]
<strong>Explanation</strong>
CustomStack customStack = new CustomStack(3); // Stack is Empty []
customStack.push(1);                          // stack becomes [1]
customStack.push(2);                          // stack becomes [1, 2]
customStack.pop();                            // return 2 --&gt; Return top of the stack 2, stack becomes [1]
customStack.push(2);                          // stack becomes [1, 2]
customStack.push(3);                          // stack becomes [1, 2, 3]
customStack.push(4);                          // stack still [1, 2, 3], Don't add another elements as size is 4
customStack.increment(5, 100);                // stack becomes [101, 102, 103]
customStack.increment(2, 100);                // stack becomes [201, 202, 103]
customStack.pop();                            // return 103 --&gt; Return top of the stack 103, stack becomes [201, 202]
customStack.pop();                            // return 202 --&gt; Return top of the stack 102, stack becomes [201]
customStack.pop();                            // return 201 --&gt; Return top of the stack 101, stack becomes []
customStack.pop();                            // return -1 --&gt; Stack is empty return -1.
</pre>



<h2><strong>Solution: Simulation</strong></h2>



<p>Time complexity: <br>init: O(1)<br>pop: O(1)<br>push: O(1)<br>inc: O(k)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class CustomStack {
public:
  CustomStack(int maxSize): max_size_(maxSize) {}

  void push(int x) {
    if (data_.size() == max_size_) return;
    data_.push_back(x);
  }

  int pop() {
    if (data_.empty()) return -1;
    int val = data_.back();
    data_.pop_back();
    return val;
  }

  void increment(int k, int val) {
    for (int i = 0; i &lt; min(static_cast&lt;size_t&gt;(k), 
                            data_.size()); ++i)
      data_[i] += val;
  }
private:
  int max_size_;
  vector&lt;int&gt; data_;
};

/**
 * Your CustomStack object will be instantiated and called as such:
 * CustomStack* obj = new CustomStack(maxSize);
 * obj-&gt;push(x);
 * int param_2 = obj-&gt;pop();
 * obj-&gt;increment(k,val);
 */</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/stack/leetcode-1381-design-a-stack-with-increment-operation/">花花酱 LeetCode 1381. Design a Stack With Increment Operation</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/stack/leetcode-1381-design-a-stack-with-increment-operation/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 707. Design Linked List</title>
		<link>https://zxi.mytechroad.com/blog/list/leetcode-707-design-linked-list/</link>
					<comments>https://zxi.mytechroad.com/blog/list/leetcode-707-design-linked-list/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 09 Aug 2018 07:33:13 +0000</pubDate>
				<category><![CDATA[List]]></category>
		<category><![CDATA[desgin]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[implemenation]]></category>
		<category><![CDATA[list]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3481</guid>

					<description><![CDATA[<p>Problem Design your implementation of the linked list. You can choose to use the singly linked list or the doubly linked list. A node in a&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-707-design-linked-list/">花花酱 LeetCode 707. Design Linked List</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/dmezFFv522I?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1>Problem</h1>
<p>Design your implementation of the linked list. You can choose to use the singly linked list or the doubly linked list. A node in a singly linked list should have two attributes: <code>val</code> and <code>next</code>. <code>val</code> is the value of the current node, and <code>next</code> is a pointer/reference to the next node. If you want to use the doubly linked list, you will need one more attribute <code>prev</code> to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.</p>
<p>Implement these functions in your linked list class:</p>
<ul>
<li>get(index) : Get the value of the <code>index</code>-th node in the linked list. If the index is invalid, return <code>-1</code>.</li>
<li>addAtHead(val) : Add a node of value <code>val</code> before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.</li>
<li>addAtTail(val) : Append a node of value <code>val</code> to the last element of the linked list.</li>
<li>addAtIndex(index, val) : Add a node of value <code>val</code> before the <code>index</code>-th node in the linked list. If <code>index</code> equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.</li>
<li>deleteAtIndex(index) : Delete the <code>index</code>-th node in the linked list, if the index is valid.</li>
</ul>
<p><strong>Example:</strong></p><pre class="crayon-plain-tag">MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1, 2);  // linked list becomes 1-&gt;2-&gt;3
linkedList.get(1);            // returns 2
linkedList.deleteAtIndex(1);  // now the linked list is 1-&gt;3
linkedList.get(1);            // returns 3</pre><p><strong>Note:</strong></p>
<ul>
<li>All values will be in the range of <code>[1, 1000]</code>.</li>
<li>The number of operations will be in the range of <code>[1, 1000]</code>.</li>
<li>Please do not use the built-in LinkedList library.</li>
</ul>
<p><img class="alignnone size-full wp-image-3491" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/707-ep216.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/707-ep216.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/707-ep216-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/707-ep216-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /><br />
<img class="alignnone size-full wp-image-3490" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/707-ep216-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/707-ep216-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/707-ep216-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/707-ep216-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /><br />
<img class="alignnone size-full wp-image-3489" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/707-ep216-3.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/707-ep216-3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/707-ep216-3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/707-ep216-3-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /><br />
<img class="alignnone size-full wp-image-3488" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/707-ep216-4.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/707-ep216-4.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/707-ep216-4-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/707-ep216-4-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<h1><strong>Solution: Single linked list</strong></h1>
<p>Keep tracking head and tail of the list.</p>
<p>Time Complexity:</p>
<p>addAtHead, addAtTail O(1)</p>
<p>addAtIndex O(index)</p>
<p>deleteAtIndex O(index)</p>
<p>Space complexity: O(1)</p>
<p>C++</p>
<p>Tracking head/tail and size of the list.</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 24 ms
class MyLinkedList {
public:
  MyLinkedList(): head_(nullptr), tail_(nullptr), size_(0) {}
  
  ~MyLinkedList() {
    Node* node = head_;
    while (node) {
      Node* cur = node;
      node = node-&gt;next;
      delete cur;
    }
    head_ = nullptr;
    tail_ = nullptr;
  }
  
  int get(int index) {
    if (index &lt; 0 || index &gt;= size_) return -1;
    auto node = head_;
    while (index--)
      node = node-&gt;next;    
    return node-&gt;val;
  }

  void addAtHead(int val) {    
    head_ = new Node(val, head_);
    if (size_++ == 0)
      tail_ = head_;   
  }
  
  void addAtTail(int val) {
    auto node = new Node(val);
    if (size_++ == 0) {
      head_ = tail_ = node;
    } else {    
      tail_-&gt;next = node;
      tail_ = tail_-&gt;next;
    }    
  }

  void addAtIndex(int index, int val) {
    if (index &lt; 0 || index &gt; size_) return;
    if (index == 0) return addAtHead(val);
    if (index == size_) return addAtTail(val);
    Node dummy(0, head_);
    Node* prev = &amp;dummy;
    while (index--) prev = prev-&gt;next;
    prev-&gt;next = new Node(val, prev-&gt;next);    
    ++size_;
  }

  void deleteAtIndex(int index) {
    if (index &lt; 0 || index &gt;= size_) return;
    Node dummy(0, head_);
    Node* prev = &amp;dummy;
    for (int i = 0; i &lt; index; ++i)
      prev = prev-&gt;next;
    Node* node_to_delete = prev-&gt;next;
    prev-&gt;next = prev-&gt;next-&gt;next;
    if (index == 0) head_ = prev-&gt;next;
    if (index == size_ - 1) tail_ = prev;
    delete node_to_delete;
    --size_;
  }
private:
  struct Node {
    int val;
    Node* next;
    Node(int _val): Node(_val, nullptr) {}
    Node(int _val, Node* _next): val(_val), next(_next) {}
  };
  Node* head_;
  Node* tail_;
  int size_;
};</pre><p>v2</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 28 ms
class MyLinkedList {
public:  
  MyLinkedList(): head_(nullptr), tail_(nullptr), dummy_(0), size_(0) {}
  
  ~MyLinkedList() {
    Node* node = head_;
    while (node) {
      Node* cur = node;
      node = node-&gt;next;
      delete cur;
    }
    head_ = nullptr;
    tail_ = nullptr;
  }
  
  int get(int index) {
    if (index &lt; 0 || index &gt;= size_) return -1;
    return getNode(index)-&gt;val;
  }

  void addAtHead(int val) {    
    head_ = new Node(val, head_);
    if (size_++ == 0)
      tail_ = head_;   
  }
  
  void addAtTail(int val) {
    auto node = new Node(val);
    if (size_++ == 0) {
      head_ = tail_ = node;
    } else {    
      tail_-&gt;next = node;
      tail_ = tail_-&gt;next;
    }    
  }

  void addAtIndex(int index, int val) {
    if (index &lt; 0 || index &gt; size_) return;
    if (index == 0) return addAtHead(val);
    if (index == size_) return addAtTail(val);
    Node* prev = getNode(index - 1);
    prev-&gt;next = new Node(val, prev-&gt;next);
    ++size_;
  }

  void deleteAtIndex(int index) {
    if (index &lt; 0 || index &gt;= size_) return;
    Node* prev = getNode(index - 1);
    Node* node_to_delete = prev-&gt;next;
    prev-&gt;next = node_to_delete-&gt;next;
    if (index == 0) head_ = prev-&gt;next;
    if (index == size_ - 1) tail_ = prev;
    delete node_to_delete;
    --size_;
  }
private:
  struct Node {
    int val;
    Node* next;
    Node(int _val): Node(_val, nullptr) {}
    Node(int _val, Node* _next): val(_val), next(_next) {}
  };
  
  Node* head_; // Does not own
  Node* tail_; // Does not own
  Node dummy_;
  int size_;
  
  Node* getNode(int index) {
    dummy_.next = head_;
    Node* n = &amp;dummy_;
    for (int i = 0; i &lt;= index; ++i)
      n = n-&gt;next;
    return n;
  }
};</pre><p>Python3</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 132 ms
"""
class Node:
  def __init__(self, val, _next=None):
    self.val = val
    self.next = _next
    
class MyLinkedList:  
  def __init__(self):
    self.head = self.tail = None
    self.size = 0
  
  def getNode(self, index):
    n = Node(0, self.head)
    for i in range(index + 1):
      n = n.next
    return n
    
  def get(self, index):
    if index &lt; 0 or index &gt;= self.size: return -1
    return self.getNode(index).val


  def addAtHead(self, val):
    n = Node(val, self.head)
    self.head = n
    if self.size == 0:
      self.tail = n
    self.size += 1


  def addAtTail(self, val):
    n = Node(val)
    if self.size == 0:
      self.head = self.tail = n
    else:
      self.tail.next = n
      self.tail = n
    self.size += 1

  def addAtIndex(self, index, val):
    if index &lt; 0 or index &gt; self.size: return
    if index == 0: return self.addAtHead(val)
    if index == self.size: return self.addAtTail(val)
    prev = self.getNode(index - 1)
    n = Node(val, prev.next)
    prev.next = n
    self.size += 1


  def deleteAtIndex(self, index):
    if index &lt; 0 or index &gt;= self.size: return
    prev = self.getNode(index - 1)
    prev.next = prev.next.next
    if index == 0: self.head = prev.next
    if index == self.size - 1: self.tail = prev
    self.size -= 1</pre><p>Java</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 74 ms
class MyLinkedList {
  class Node {
    public int val;
    public Node next;
    public Node(int val) { this.val = val; this.next = null; }
    public Node(int val, Node next) { this.val = val; this.next = next; }
  }
  
  private Node head;
  private Node tail;
  private int size;
  
  public MyLinkedList() {
    this.head = this.tail = null;
    this.size = 0;
  }
  
  private Node getNode(int index) {
    Node n = new Node(0, this.head);
    while (index-- &gt;= 0) {
      n = n.next;
    }
    return n;
  }

  public int get(int index) {
    if (index &lt; 0 || index &gt;= size) return -1;
    return getNode(index).val;
  }

  public void addAtHead(int val) {
    this.head = new Node(val, this.head);
    if (this.size++ == 0)
      this.tail = this.head;    
  }

  public void addAtTail(int val) {
    Node n = new Node(val);
    if (this.size++ == 0)
      this.head = this.tail = n;
    else
      this.tail = this.tail.next = n;
  }

  public void addAtIndex(int index, int val) {
    if (index &lt; 0 || index &gt; this.size) return;
    if (index == 0)  { this.addAtHead(val); return; }
    if (index == size) { this.addAtTail(val); return; }
    Node prev = this.getNode(index - 1);
    prev.next = new Node(val, prev.next);
    ++this.size;
  }

  public void deleteAtIndex(int index) {
    if (index &lt; 0 || index &gt;= this.size) return;
    Node prev = this.getNode(index - 1);
    prev.next = prev.next.next;
    if (index == 0) this.head = prev.next;
    if (index == this.size - 1) this.tail = prev;
    --this.size;
  }
}</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-707-design-linked-list/">花花酱 LeetCode 707. Design Linked List</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/list/leetcode-707-design-linked-list/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 872. Implement Rand10() Using Rand7()</title>
		<link>https://zxi.mytechroad.com/blog/desgin/leetcode-872-implement-rand10-using-rand7/</link>
					<comments>https://zxi.mytechroad.com/blog/desgin/leetcode-872-implement-rand10-using-rand7/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 16 Jul 2018 16:08:23 +0000</pubDate>
				<category><![CDATA[Desgin]]></category>
		<category><![CDATA[desgin]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[random]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3199</guid>

					<description><![CDATA[<p>Problem Given a function rand7 which generates a uniform random integer in the range 1 to 7, write a function rand10 which generates a uniform random integer in the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/desgin/leetcode-872-implement-rand10-using-rand7/">花花酱 LeetCode 872. Implement Rand10() Using Rand7()</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>Given a function <code>rand7</code> which generates a uniform random integer in the range 1 to 7, write a function <code>rand10</code> which generates a uniform random integer in the range 1 to 10.</p>
<p>Do NOT use system&#8217;s <code>Math.random()</code>.</p>
<div>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-1-1">1</span>
<strong>Output: </strong><span id="example-output-1">[7]</span>
</pre>
<div>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-2-1">2</span>
<strong>Output: </strong><span id="example-output-2">[8,4]</span>
</pre>
<div>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-3-1">3</span>
<strong>Output: </strong><span id="example-output-3">[8,1,10]</span>
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>rand7</code> is predefined.</li>
<li>Each testcase has one argument: <code>n</code>, the number of times that <code>rand10</code> is called.</li>
</ol>
<h1><strong>Solution: Math</strong></h1>
<p>Time complexity: O(49/40) = O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 112 ms
class Solution {
public:
  int rand10() {
    int target = 40;
    while (target &gt;= 40)
      target = 7 * (rand7() - 1) + (rand7() - 1);
    return target % 10 + 1;
  }
};</pre><p>Time complexity: O(7/6 + 7 / 5) = O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 112 ms
class Solution {
public:
  int rand10() {
    int i = INT_MAX;
    int j = INT_MAX;
    while (i &gt; 6) i = rand7(); // i = [1, 2, 3, 4, 5, 6]
    while (j &gt; 5) j = rand7(); // j = [1, 2, 3, 4, 5]
    return j + 5 * (i &amp; 1);
  }
};</pre><p>&nbsp;</p>
</div>
</div>
</div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/desgin/leetcode-872-implement-rand10-using-rand7/">花花酱 LeetCode 872. Implement Rand10() Using Rand7()</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/desgin/leetcode-872-implement-rand10-using-rand7/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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>
