Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加了lcof-38.52和lc-1195.1226的题解 #341

Merged
merged 1 commit into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lcof/面试题38. 字符串的排列/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,40 @@ var permutation = function (s) {
};
```

### **C++**

```cpp
class Solution {
public:
void func(string str, int index, set<string>& 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<string> permutation(string s) {
set<string> mySet;
func(s, 0, mySet);
vector<string> ret;
for (auto& x : mySet) {
/* 这一题加入mySet是为了进行结果的去重。
但由于在最后加入了将set转vector的过程,所以时间复杂度稍高 */
ret.push_back(x);
}
return ret;
}
};
```

### **...**

```
Expand Down
25 changes: 25 additions & 0 deletions lcof/面试题38. 字符串的排列/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public:
void func(string str, int index, set<string>& 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<string> permutation(string s) {
set<string> mySet;
func(s, 0, mySet);
vector<string> ret;
for (string x : mySet) {
ret.push_back(x);
}
return ret;
}
};
30 changes: 30 additions & 0 deletions lcof/面试题52. 两个链表的第一个公共节点/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
};
59 changes: 59 additions & 0 deletions solution/1100-1199/1195.Fizz Buzz Multithreaded/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,65 @@

```

### **C++**

```cpp
class FizzBuzz {
private:
std::mutex mtx;
atomic<int> index;
int n;

// 这里主要运用到了C++11中的RAII锁(lock_guard)的知识。
// 需要强调的一点是,在进入循环后,要时刻不忘加入index <= n的逻辑
public:
FizzBuzz(int n) {
this->n = n;
index = 1;
}

void fizz(function<void()> printFizz) {
while (index <= n) {
std::lock_guard<std::mutex> lk(mtx);
if (0 == index % 3 && 0 != index % 5 && index <= n) {
printFizz();
index++;
}
}
}

void buzz(function<void()> printBuzz) {
while (index <= n) {
std::lock_guard<std::mutex> lk(mtx);
if (0 == index % 5 && 0 != index % 3 && index <= n){
printBuzz();
index++;
}
}
}

void fizzbuzz(function<void()> printFizzBuzz) {
while (index <= n) {
std::lock_guard<std::mutex> lk(mtx);
if (0 == index % 15 && index <= n) {
printFizzBuzz();
index++;
}
}
}

void number(function<void(int)> printNumber) {
while (index <= n) {
std::lock_guard<std::mutex> lk(mtx);
if (0 != index % 3 && 0 != index % 5 && index <= n) {
printNumber(index);
index++;
}
}
}
};
```

### **...**

```
Expand Down
56 changes: 56 additions & 0 deletions solution/1100-1199/1195.Fizz Buzz Multithreaded/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class FizzBuzz {
private:
std::mutex mtx;
atomic<int> index;
int n;

public:
FizzBuzz(int n) {
this->n = n;
index = 1;
}

// printFizz() outputs "fizz".
void fizz(function<void()> printFizz) {
while (index <= n) {
std::lock_guard<std::mutex> lk(mtx);
if (0 == index % 3 && 0 != index % 5 && index <= n) {
printFizz();
index++;
}
}
}

// printBuzz() outputs "buzz".
void buzz(function<void()> printBuzz) {
while (index <= n) {
std::lock_guard<std::mutex> lk(mtx);
if (0 == index % 5 && 0 != index % 3 && index <= n){
printBuzz();
index++;
}
}
}

// printFizzBuzz() outputs "fizzbuzz".
void fizzbuzz(function<void()> printFizzBuzz) {
while (index <= n) {
std::lock_guard<std::mutex> lk(mtx);
if (0 == index % 15 && index <= n) {
printFizzBuzz();
index++;
}
}
}

// printNumber(x) outputs "x", where x is an integer.
void number(function<void(int)> printNumber) {
while (index <= n) {
std::lock_guard<std::mutex> lk(mtx);
if (0 != index % 3 && 0 != index % 5 && index <= n) {
printNumber(index);
index++;
}
}
}
};
24 changes: 24 additions & 0 deletions solution/1200-1299/1226.The Dining Philosophers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,30 @@ output[i] = [a, b, c] (3个整数)

```

### **C++**

```cpp
class DiningPhilosophers {
public:
using Act = function<void()>;

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<mutex> mutexes_ = vector<mutex>(5);
};
```

### **...**

```
Expand Down
16 changes: 16 additions & 0 deletions solution/1200-1299/1226.The Dining Philosophers/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class DiningPhilosophers {
public:
using Act = function<void()>;

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<mutex> mutexes_ = vector<mutex>(5);
};