Skip to content

Commit cf3d2c6

Browse files
ChunelFengjunfeng.fj
and
junfeng.fj
authored
增加了lcof-38.52和lc-1195.1226的题解 (#341)
Co-authored-by: junfeng.fj <junfeng.fj@alibaba-inc.com>
1 parent 12fd98b commit cf3d2c6

File tree

8 files changed

+266
-0
lines changed

8 files changed

+266
-0
lines changed

lcof/面试题38. 字符串的排列/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,40 @@ var permutation = function (s) {
117117
};
118118
```
119119

120+
### **C++**
121+
122+
```cpp
123+
class Solution {
124+
public:
125+
void func(string str, int index, set<string>& mySet) {
126+
if (index == str.size()) {
127+
// 当轮训到最后一个字符的时候,直接放入set中。加入set结构,是为了避免插入的值重复
128+
mySet.insert(str);
129+
} else {
130+
for (int i = index; i < str.size(); i++) {
131+
// 从传入位置(index)开始算,固定第一个字符,然后后面的字符依次跟index位置交换
132+
swap(str[i], str[index]);
133+
int temp = index + 1;
134+
func(str, temp, mySet);
135+
swap(str[i], str[index]);
136+
}
137+
}
138+
}
139+
140+
vector<string> permutation(string s) {
141+
set<string> mySet;
142+
func(s, 0, mySet);
143+
vector<string> ret;
144+
for (auto& x : mySet) {
145+
/* 这一题加入mySet是为了进行结果的去重。
146+
但由于在最后加入了将set转vector的过程,所以时间复杂度稍高 */
147+
ret.push_back(x);
148+
}
149+
return ret;
150+
}
151+
};
152+
```
153+
120154
### **...**
121155

122156
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
void func(string str, int index, set<string>& mySet) {
4+
if (index == str.size()) {
5+
mySet.insert(str);
6+
} else {
7+
for (int i = index; i < str.size(); i++) {
8+
swap(str[i], str[index]);
9+
int temp = index + 1;
10+
func(str, temp, mySet);
11+
swap(str[i], str[index]);
12+
}
13+
}
14+
}
15+
16+
vector<string> permutation(string s) {
17+
set<string> mySet;
18+
func(s, 0, mySet);
19+
vector<string> ret;
20+
for (string x : mySet) {
21+
ret.push_back(x);
22+
}
23+
return ret;
24+
}
25+
};

lcof/面试题52. 两个链表的第一个公共节点/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,36 @@ var getIntersectionNode = function (headA, headB) {
169169
};
170170
```
171171

172+
### **C++**
173+
174+
```cpp
175+
/**
176+
* Definition for singly-linked list.
177+
* struct ListNode {
178+
* int val;
179+
* ListNode *next;
180+
* ListNode(int x) : val(x), next(NULL) {}
181+
* };
182+
*/
183+
184+
class Solution {
185+
public:
186+
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
187+
ListNode* a = headA;
188+
ListNode* b = headB;
189+
while (a != b) {
190+
/* 这个循环的思路是,a先从listA往后走,如果到最后,就接着从listB走;b正好相反。
191+
如果有交集的话,a和b会在分别进入listB和listA之后的循环中项目
192+
如果没有交集的话,则a和b会同时遍历完listA和listB后,值同时为nullptr */
193+
a = (a == nullptr) ? headB : a->next;
194+
b = (b == nullptr) ? headA : b->next;
195+
}
196+
197+
return a;
198+
}
199+
};
200+
```
201+
172202
### **...**
173203

174204
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
10+
class Solution {
11+
public:
12+
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
13+
ListNode* a = headA;
14+
ListNode* b = headB;
15+
while (a != b) {
16+
a = (a == nullptr) ? headB : a->next;
17+
b = (b == nullptr) ? headA : b->next;
18+
}
19+
20+
return a;
21+
}
22+
};

solution/1100-1199/1195.Fizz Buzz Multithreaded/README.md

+59
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,65 @@
5656

5757
```
5858

59+
### **C++**
60+
61+
```cpp
62+
class FizzBuzz {
63+
private:
64+
std::mutex mtx;
65+
atomic<int> index;
66+
int n;
67+
68+
// 这里主要运用到了C++11中的RAII锁(lock_guard)的知识。
69+
// 需要强调的一点是,在进入循环后,要时刻不忘加入index <= n的逻辑
70+
public:
71+
FizzBuzz(int n) {
72+
this->n = n;
73+
index = 1;
74+
}
75+
76+
void fizz(function<void()> printFizz) {
77+
while (index <= n) {
78+
std::lock_guard<std::mutex> lk(mtx);
79+
if (0 == index % 3 && 0 != index % 5 && index <= n) {
80+
printFizz();
81+
index++;
82+
}
83+
}
84+
}
85+
86+
void buzz(function<void()> printBuzz) {
87+
while (index <= n) {
88+
std::lock_guard<std::mutex> lk(mtx);
89+
if (0 == index % 5 && 0 != index % 3 && index <= n){
90+
printBuzz();
91+
index++;
92+
}
93+
}
94+
}
95+
96+
void fizzbuzz(function<void()> printFizzBuzz) {
97+
while (index <= n) {
98+
std::lock_guard<std::mutex> lk(mtx);
99+
if (0 == index % 15 && index <= n) {
100+
printFizzBuzz();
101+
index++;
102+
}
103+
}
104+
}
105+
106+
void number(function<void(int)> printNumber) {
107+
while (index <= n) {
108+
std::lock_guard<std::mutex> lk(mtx);
109+
if (0 != index % 3 && 0 != index % 5 && index <= n) {
110+
printNumber(index);
111+
index++;
112+
}
113+
}
114+
}
115+
};
116+
```
117+
59118
### **...**
60119
61120
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class FizzBuzz {
2+
private:
3+
std::mutex mtx;
4+
atomic<int> index;
5+
int n;
6+
7+
public:
8+
FizzBuzz(int n) {
9+
this->n = n;
10+
index = 1;
11+
}
12+
13+
// printFizz() outputs "fizz".
14+
void fizz(function<void()> printFizz) {
15+
while (index <= n) {
16+
std::lock_guard<std::mutex> lk(mtx);
17+
if (0 == index % 3 && 0 != index % 5 && index <= n) {
18+
printFizz();
19+
index++;
20+
}
21+
}
22+
}
23+
24+
// printBuzz() outputs "buzz".
25+
void buzz(function<void()> printBuzz) {
26+
while (index <= n) {
27+
std::lock_guard<std::mutex> lk(mtx);
28+
if (0 == index % 5 && 0 != index % 3 && index <= n){
29+
printBuzz();
30+
index++;
31+
}
32+
}
33+
}
34+
35+
// printFizzBuzz() outputs "fizzbuzz".
36+
void fizzbuzz(function<void()> printFizzBuzz) {
37+
while (index <= n) {
38+
std::lock_guard<std::mutex> lk(mtx);
39+
if (0 == index % 15 && index <= n) {
40+
printFizzBuzz();
41+
index++;
42+
}
43+
}
44+
}
45+
46+
// printNumber(x) outputs "x", where x is an integer.
47+
void number(function<void(int)> printNumber) {
48+
while (index <= n) {
49+
std::lock_guard<std::mutex> lk(mtx);
50+
if (0 != index % 3 && 0 != index % 5 && index <= n) {
51+
printNumber(index);
52+
index++;
53+
}
54+
}
55+
}
56+
};

solution/1200-1299/1226.The Dining Philosophers/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,30 @@ output[i] = [a, b, c] (3个整数)
7777

7878
```
7979

80+
### **C++**
81+
82+
```cpp
83+
class DiningPhilosophers {
84+
public:
85+
using Act = function<void()>;
86+
87+
void wantsToEat(int philosopher, Act pickLeftFork, Act pickRightFork, Act eat, Act putLeftFork, Act putRightFork) {
88+
/* 这一题实际上是用到了C++17中的scoped_lock知识。
89+
作用是传入scoped_lock(mtx1, mtx2)两个锁,然后在作用范围内,依次顺序上锁mtx1和mtx2;然后在作用范围结束时,再反续解锁mtx2和mtx1。
90+
从而保证了philosopher1有动作的时候,philosopher2无法操作;但是philosopher3和philosopher4不受影响 */
91+
std::scoped_lock lock(mutexes_[philosopher], mutexes_[philosopher >= 4 ? 0 : philosopher + 1]);
92+
pickLeftFork();
93+
pickRightFork();
94+
eat();
95+
putLeftFork();
96+
putRightFork();
97+
}
98+
99+
private:
100+
vector<mutex> mutexes_ = vector<mutex>(5);
101+
};
102+
```
103+
80104
### **...**
81105

82106
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class DiningPhilosophers {
2+
public:
3+
using Act = function<void()>;
4+
5+
void wantsToEat(int philosopher, Act pickLeftFork, Act pickRightFork, Act eat, Act putLeftFork, Act putRightFork) {
6+
std::scoped_lock lock(mutexes_[philosopher], mutexes_[philosopher >= 4 ? 0 : philosopher + 1]);
7+
pickLeftFork();
8+
pickRightFork();
9+
eat();
10+
putLeftFork();
11+
putRightFork();
12+
}
13+
14+
private:
15+
vector<mutex> mutexes_ = vector<mutex>(5);
16+
};

0 commit comments

Comments
 (0)