2018.10.22~2018.10.28
每周完成一个ARTS:
每周至少做一个LeetCode的算法题,阅读和点评至少一篇英文技术文章,学习至少一个技术技巧,分享一篇有观点和思考的技术文章(也就是algorithm ,preview ,tip,share 简称ARTS)需要坚持至少一年。
一、Algorithm
本周学习的是LeetCode 622.Designed Circular Queue
1.代码实现:
class MyCircularQueue {
public:
/** Initialize your data structure here. Set the size of the queue to be k. */
MyCircularQueue(int k) {
data.resize(k);
head = 0;
tail = 0;
reset = true;
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
bool enQueue(int value) {
if (isFull()) return false;
// update the reset value when first enqueue happens
if (head == tail && reset) reset = false;
data[tail] = value;
tail = (tail + 1) % data.size();
return true;
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
bool deQueue() {
if (isEmpty()) return false;
head = (head + 1) % data.size();
// update the reset value when last dequeue happens
if (head == tail && !reset) reset = true;
return true;
}
/** Get the front item from the queue. */
int Front() {
if (isEmpty()) return -1;
return data[head];
}
/** Get the last item from the queue. */
int Rear() {
if (isEmpty()) return -1;
return data[(tail + data.size() - 1) % data.size()];
}
/** Checks whether the circular queue is empty or not. */
bool isEmpty() {
if (tail == head && reset) return true;
return false;
}
/** Checks whether the circular queue is full or not. */
bool isFull() {
if (tail == head && !reset) return true;
return false;
}
private:
vector<int> data;
int head;
int tail;
// reset is the mark when the queue is empty
// to differentiate from queue is full
// because in both conditions (tail == head) stands
bool reset;
};
2.、关于循环队列
1).在初始状态,队首=队尾
2).数据 a 入队:
tail指针往后挪一个位置:
数据b入队:
3).出队:
数据a出队:
数据b出队:
4).当队列超出下标7时:
tailz指针回到capacity[0]的位置:
5).此时判定队列为满,浪费了一个空间,
二、preview:
本周阅读的技术文章为:The virtual table
编程实验:
class Base
{
public:
virtual void function1() {};
virtual void function2() {};
};
class D1: public Base
{
public:
virtual void function1() {};
};
class D2: public Base
{
public:
virtual void function2() {};
};
对应的虚函数表:
三、tip:
本周学习了:类对象所占用的空间-为什么空类占用一个字节
四、share:
本周分享的文章为 叶劲峰《C++强大背后》,叶神在其中叙述了C++的练级攻略,结合耗叔的练级攻略,从而确定自己的C++学习路径。