diff --git "a/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/README.md" index 6c87232265bc2..f74994db8af28 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/README.md" @@ -117,6 +117,40 @@ var permutation = function (s) { }; ``` +### **C++** + +```cpp +class Solution { +public: + void func(string str, int index, set& mySet) { + if (index == str.size()) { + // 当轮训到最后一个字符的时候,直接放入set中。加入set结构,是为了避免插入的值重复 + mySet.insert(str); + } else { + for (int i = index; i < str.size(); i++) { + // 从传入位置(index)开始算,固定第一个字符,然后后面的字符依次跟index位置交换 + swap(str[i], str[index]); + int temp = index + 1; + func(str, temp, mySet); + swap(str[i], str[index]); + } + } + } + + vector permutation(string s) { + set mySet; + func(s, 0, mySet); + vector ret; + for (auto& x : mySet) { + /* 这一题加入mySet是为了进行结果的去重。 + 但由于在最后加入了将set转vector的过程,所以时间复杂度稍高 */ + ret.push_back(x); + } + return ret; + } +}; +``` + ### **...** ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/Solution.cpp" new file mode 100644 index 0000000000000..3d3775e4dd0f8 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/Solution.cpp" @@ -0,0 +1,25 @@ +class Solution { +public: + void func(string str, int index, set& mySet) { + if (index == str.size()) { + mySet.insert(str); + } else { + for (int i = index; i < str.size(); i++) { + swap(str[i], str[index]); + int temp = index + 1; + func(str, temp, mySet); + swap(str[i], str[index]); + } + } + } + + vector permutation(string s) { + set mySet; + func(s, 0, mySet); + vector ret; + for (string x : mySet) { + ret.push_back(x); + } + return ret; + } +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23052. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23052. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/README.md" index b728a1e9a29e9..88a6cde8c998a 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23052. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23052. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/README.md" @@ -169,6 +169,36 @@ var getIntersectionNode = function (headA, headB) { }; ``` +### **C++** + +```cpp +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ + +class Solution { +public: + ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + ListNode* a = headA; + ListNode* b = headB; + while (a != b) { + /* 这个循环的思路是,a先从listA往后走,如果到最后,就接着从listB走;b正好相反。 + 如果有交集的话,a和b会在分别进入listB和listA之后的循环中项目 + 如果没有交集的话,则a和b会同时遍历完listA和listB后,值同时为nullptr */ + a = (a == nullptr) ? headB : a->next; + b = (b == nullptr) ? headA : b->next; + } + + return a; + } +}; +``` + ### **...** ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23052. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23052. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/Solution.cpp" new file mode 100644 index 0000000000000..098df8e6540f4 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23052. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/Solution.cpp" @@ -0,0 +1,22 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ + +class Solution { +public: + ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + ListNode* a = headA; + ListNode* b = headB; + while (a != b) { + a = (a == nullptr) ? headB : a->next; + b = (b == nullptr) ? headA : b->next; + } + + return a; + } +}; diff --git a/solution/1100-1199/1195.Fizz Buzz Multithreaded/README.md b/solution/1100-1199/1195.Fizz Buzz Multithreaded/README.md index cb01487547905..76d6f229cdbe7 100644 --- a/solution/1100-1199/1195.Fizz Buzz Multithreaded/README.md +++ b/solution/1100-1199/1195.Fizz Buzz Multithreaded/README.md @@ -56,6 +56,65 @@ ``` +### **C++** + +```cpp +class FizzBuzz { +private: + std::mutex mtx; + atomic index; + int n; + +// 这里主要运用到了C++11中的RAII锁(lock_guard)的知识。 +// 需要强调的一点是,在进入循环后,要时刻不忘加入index <= n的逻辑 +public: + FizzBuzz(int n) { + this->n = n; + index = 1; + } + + void fizz(function printFizz) { + while (index <= n) { + std::lock_guard lk(mtx); + if (0 == index % 3 && 0 != index % 5 && index <= n) { + printFizz(); + index++; + } + } + } + + void buzz(function printBuzz) { + while (index <= n) { + std::lock_guard lk(mtx); + if (0 == index % 5 && 0 != index % 3 && index <= n){ + printBuzz(); + index++; + } + } + } + + void fizzbuzz(function printFizzBuzz) { + while (index <= n) { + std::lock_guard lk(mtx); + if (0 == index % 15 && index <= n) { + printFizzBuzz(); + index++; + } + } + } + + void number(function printNumber) { + while (index <= n) { + std::lock_guard lk(mtx); + if (0 != index % 3 && 0 != index % 5 && index <= n) { + printNumber(index); + index++; + } + } + } +}; +``` + ### **...** ``` diff --git a/solution/1100-1199/1195.Fizz Buzz Multithreaded/Solution.cpp b/solution/1100-1199/1195.Fizz Buzz Multithreaded/Solution.cpp new file mode 100644 index 0000000000000..59141210279a1 --- /dev/null +++ b/solution/1100-1199/1195.Fizz Buzz Multithreaded/Solution.cpp @@ -0,0 +1,56 @@ +class FizzBuzz { +private: + std::mutex mtx; + atomic index; + int n; + +public: + FizzBuzz(int n) { + this->n = n; + index = 1; + } + + // printFizz() outputs "fizz". + void fizz(function printFizz) { + while (index <= n) { + std::lock_guard lk(mtx); + if (0 == index % 3 && 0 != index % 5 && index <= n) { + printFizz(); + index++; + } + } + } + + // printBuzz() outputs "buzz". + void buzz(function printBuzz) { + while (index <= n) { + std::lock_guard lk(mtx); + if (0 == index % 5 && 0 != index % 3 && index <= n){ + printBuzz(); + index++; + } + } + } + + // printFizzBuzz() outputs "fizzbuzz". + void fizzbuzz(function printFizzBuzz) { + while (index <= n) { + std::lock_guard lk(mtx); + if (0 == index % 15 && index <= n) { + printFizzBuzz(); + index++; + } + } + } + + // printNumber(x) outputs "x", where x is an integer. + void number(function printNumber) { + while (index <= n) { + std::lock_guard lk(mtx); + if (0 != index % 3 && 0 != index % 5 && index <= n) { + printNumber(index); + index++; + } + } + } +}; \ No newline at end of file diff --git a/solution/1200-1299/1226.The Dining Philosophers/README.md b/solution/1200-1299/1226.The Dining Philosophers/README.md index f320c0c985318..d8e602e9a0ef8 100644 --- a/solution/1200-1299/1226.The Dining Philosophers/README.md +++ b/solution/1200-1299/1226.The Dining Philosophers/README.md @@ -77,6 +77,30 @@ output[i] = [a, b, c] (3个整数) ``` +### **C++** + +```cpp +class DiningPhilosophers { +public: + using Act = function; + + void wantsToEat(int philosopher, Act pickLeftFork, Act pickRightFork, Act eat, Act putLeftFork, Act putRightFork) { + /* 这一题实际上是用到了C++17中的scoped_lock知识。 + 作用是传入scoped_lock(mtx1, mtx2)两个锁,然后在作用范围内,依次顺序上锁mtx1和mtx2;然后在作用范围结束时,再反续解锁mtx2和mtx1。 + 从而保证了philosopher1有动作的时候,philosopher2无法操作;但是philosopher3和philosopher4不受影响 */ + std::scoped_lock lock(mutexes_[philosopher], mutexes_[philosopher >= 4 ? 0 : philosopher + 1]); + pickLeftFork(); + pickRightFork(); + eat(); + putLeftFork(); + putRightFork(); + } + +private: + vector mutexes_ = vector(5); +}; +``` + ### **...** ``` diff --git a/solution/1200-1299/1226.The Dining Philosophers/Solution.cpp b/solution/1200-1299/1226.The Dining Philosophers/Solution.cpp new file mode 100644 index 0000000000000..159a2f8dc854d --- /dev/null +++ b/solution/1200-1299/1226.The Dining Philosophers/Solution.cpp @@ -0,0 +1,16 @@ +class DiningPhilosophers { +public: + using Act = function; + + void wantsToEat(int philosopher, Act pickLeftFork, Act pickRightFork, Act eat, Act putLeftFork, Act putRightFork) { + std::scoped_lock lock(mutexes_[philosopher], mutexes_[philosopher >= 4 ? 0 : philosopher + 1]); + pickLeftFork(); + pickRightFork(); + eat(); + putLeftFork(); + putRightFork(); + } + +private: + vector mutexes_ = vector(5); +}; \ No newline at end of file