[leetcode]Print in Order

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Foo {
private:
    bool isFirst;
    bool isSecond;
    bool isThird;
public:
    Foo() {
        isFirst = false;
        isSecond = false;
        isThird = true;
    }

    void first(std::function<void()> printFirst) {
        while (!isThird);
        isThird = false;
        isSecond = false;
        printFirst();        
        // printFirst() outputs "first". Do not change or remove this line.
        isFirst = true;
    }

    void second(std::function<void()> printSecond) {
        while(!isFirst);
        isFirst = false;
        isThird = false;
        // printSecond() outputs "second". Do not change or remove this line.
        printSecond();
        isSecond = true;
    }

    void third(std::function<void()> printThird) {
        while(!isSecond);
        isFirst = false;
        isSecond = false;
        // printThird() outputs "third". Do not change or remove this line.
        printThird();
        isThird = true;
    }
};
Licensed under CC BY-NC-SA 4.0