Skip to content

Commit d55f9a3

Browse files
committed
LeetCode first
0 parents  commit d55f9a3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2755
-0
lines changed

.vscode/settings.json

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"files.associations": {
3+
"__bit_reference": "cpp",
4+
"__config": "cpp",
5+
"__debug": "cpp",
6+
"__errc": "cpp",
7+
"__functional_base": "cpp",
8+
"__hash_table": "cpp",
9+
"__locale": "cpp",
10+
"__mutex_base": "cpp",
11+
"__node_handle": "cpp",
12+
"__nullptr": "cpp",
13+
"__split_buffer": "cpp",
14+
"__string": "cpp",
15+
"__threading_support": "cpp",
16+
"__tuple": "cpp",
17+
"algorithm": "cpp",
18+
"array": "cpp",
19+
"atomic": "cpp",
20+
"bit": "cpp",
21+
"bitset": "cpp",
22+
"cctype": "cpp",
23+
"chrono": "cpp",
24+
"clocale": "cpp",
25+
"cmath": "cpp",
26+
"complex": "cpp",
27+
"cstdarg": "cpp",
28+
"cstddef": "cpp",
29+
"cstdint": "cpp",
30+
"cstdio": "cpp",
31+
"cstdlib": "cpp",
32+
"cstring": "cpp",
33+
"ctime": "cpp",
34+
"cwchar": "cpp",
35+
"cwctype": "cpp",
36+
"deque": "cpp",
37+
"exception": "cpp",
38+
"functional": "cpp",
39+
"initializer_list": "cpp",
40+
"ios": "cpp",
41+
"iosfwd": "cpp",
42+
"iostream": "cpp",
43+
"istream": "cpp",
44+
"iterator": "cpp",
45+
"limits": "cpp",
46+
"locale": "cpp",
47+
"memory": "cpp",
48+
"mutex": "cpp",
49+
"new": "cpp",
50+
"optional": "cpp",
51+
"ostream": "cpp",
52+
"ratio": "cpp",
53+
"sstream": "cpp",
54+
"stack": "cpp",
55+
"stdexcept": "cpp",
56+
"streambuf": "cpp",
57+
"string": "cpp",
58+
"string_view": "cpp",
59+
"system_error": "cpp",
60+
"tuple": "cpp",
61+
"type_traits": "cpp",
62+
"typeinfo": "cpp",
63+
"unordered_map": "cpp",
64+
"utility": "cpp",
65+
"vector": "cpp",
66+
"list": "cpp"
67+
}
68+
}

FooBar.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include<iostream>
2+
#include<semaphore.h>
3+
using namespace std;
4+
5+
class FooBar {
6+
private:
7+
int n;
8+
sem_t sem_foo, sem_bar;
9+
10+
public:
11+
FooBar(int n) {
12+
this->n = n;
13+
sem_init(&sem_foo, 0, 0);
14+
sem_init(&sem_bar, 0, 1);
15+
}
16+
17+
~FooBar()
18+
{
19+
sem_destroy(&sem_foo);
20+
sem_destroy(&sem_bar);
21+
}
22+
23+
void foo(function<void()> printFoo) {
24+
25+
for (int i = 0; i < n; i++) {
26+
sem_wait(&sem_bar);
27+
// printFoo() outputs "foo". Do not change or remove this line.
28+
printFoo();
29+
sem_post(&sem_foo);
30+
}
31+
}
32+
33+
void bar(function<void()> printBar) {
34+
35+
for (int i = 0; i < n; i++) {
36+
sem_wait(&sem_foo);
37+
// printBar() outputs "bar". Do not change or remove this line.
38+
printBar();
39+
sem_post(&sem_bar);
40+
}
41+
}
42+
};
43+
44+
int main()
45+
{
46+
47+
}

KthLargest.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include<iostream>
2+
#include<vector>
3+
#include<queue>
4+
#include<functional>
5+
using namespace std;
6+
7+
//如果使用排序,每次插入的复杂度最小为o(n), 插入查找时查找可以实现o(log(n)),但是移动元素依然需要o(n)。
8+
//而使用大小顶堆进行插入,调整堆的时间复杂度为o(log(n)),所以最好选用大小顶堆来做选择最大或者最小值。
9+
//因为返回之后不会删除所有返回的数,使用add操作时会导堆膨胀,所以应该维护一个大小为k的。
10+
11+
class KthLargest {
12+
public:
13+
KthLargest(int k, vector<int>& nums)
14+
:_k(k)
15+
{
16+
//建堆
17+
for(auto & i : nums)
18+
{
19+
add(i);
20+
}
21+
}
22+
23+
int add(int val)
24+
{
25+
_data.push(val);
26+
if(_data.size() > _k)
27+
{
28+
_data.pop();
29+
}
30+
31+
return _data.top();
32+
}
33+
34+
/* 使用vector排序, 时间复杂度高
35+
int add(int val)
36+
{
37+
_data.push_back(val);
38+
sort(_data.begin(), _data.end(), std::greater<int>());
39+
if(_data.size() > _k)
40+
{
41+
_data.pop_back();
42+
}
43+
44+
return _data.back();
45+
}
46+
*/
47+
48+
private:
49+
int _k;
50+
//优先级队列,建立一个小顶堆, 底层使用堆排序实现
51+
priority_queue<int, vector<int>, std::greater<int> > _data;
52+
};
53+
54+
55+
int main()
56+
{
57+
vector<int> vec = {4,5,8,2};
58+
KthLargest* k = new KthLargest(3, vec);
59+
cout << k->add(3) << endl;
60+
cout << k->add(5) << endl;
61+
cout << k->add(10) << endl;
62+
cout << k->add(9) << endl;
63+
cout << k->add(4) << endl;
64+
return 0;
65+
}

LRUCache.cpp

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//146. LRU 缓存机制
2+
#include<iostream>
3+
#include<list>
4+
#include<unordered_map>
5+
6+
using namespace std;
7+
//原理:
8+
// LRU: 最近最少使用算法,在cache中,如果cache中容量已满,首先删除元素是在最近时间内,最不经常使用的元素。
9+
// 注意和最少使用法的区别。
10+
// 1:使用双向链表list存储数据: pair<int, int> (key, value), LRU;
11+
// 选择list 因为 头部、中间插入元素,或者删除尾部元素时间复杂度均为o(1),无需移动元素;
12+
// 最近使用的元素放在链表的最前面。
13+
// 2: 选择unordered_map<int, list<pair<int, int>>::iterator> 做哈希表,unordered_map利用哈希表实现, 元素间无序,查找和插入的时间复杂度均为o(1);
14+
// 所以通过key完成查找节点的时间复杂度o(1)。
15+
16+
//输出双向链表
17+
std::ostream& operator<<(std::ostream& ostr, const std::list<pair<int, int> >& list)
18+
{
19+
for (auto i : list) {
20+
ostr << i.first << ", " << i.second << "; ";
21+
}
22+
return ostr;
23+
}
24+
25+
class LRUCache {
26+
public:
27+
using ListIter = list<pair<int, int> >::iterator;
28+
using UnorderMapIter = unordered_map<int, ListIter>::iterator;
29+
LRUCache(int capacity)
30+
:_capacity(capacity)
31+
{
32+
33+
}
34+
35+
int get(int key)
36+
{
37+
UnorderMapIter ite = _cache.find(key);
38+
//
39+
//查找时未找到, 直接返回 -1
40+
if(ite == _cache.end())
41+
{
42+
return -1;
43+
}
44+
45+
//如果存在,因为最近一次使用到该元素,所以需将该key的数据移动到链表最前面;
46+
_data.splice(_data.begin(), _data, ite->second);
47+
//_cache[key] = _data.begin();
48+
return ite->second->second;
49+
}
50+
51+
void put(int key, int value)
52+
{
53+
UnorderMapIter ite = _cache.find(key);
54+
if(ite == _cache.end())
55+
{
56+
//如果该元素没在cache中, 需要添加到cache中,首先判断cache是否已满,如果满了,将链表最后一个元素移除
57+
if(_data.size() == _capacity)
58+
{
59+
_cache.erase(_data.back().first);
60+
_data.pop_back();
61+
}
62+
63+
_data.push_front(make_pair(key, value));
64+
_cache.insert(make_pair(key, _data.begin()));
65+
}
66+
else
67+
{
68+
//如果元素已经存在cache中,更新该key的value值,并且将该元素移到链表最前面。
69+
ite->second->second = value;
70+
_data.splice(_data.begin(), _data, ite->second);
71+
_cache[key] = _data.begin();
72+
}
73+
}
74+
//打印链表
75+
void print()
76+
{
77+
cout << _data << endl;
78+
}
79+
private:
80+
81+
int _capacity;
82+
list<pair<int, int> > _data;
83+
unordered_map<int, ListIter> _cache;
84+
};
85+
86+
int main()
87+
{
88+
LRUCache lru(2);
89+
lru.put(1,1);
90+
lru.put(2,2);
91+
lru.print();
92+
lru.put(3,3);
93+
lru.print();
94+
lru.get(1);
95+
lru.print();
96+
cout << "get2 " << lru.get(2) << endl;;
97+
lru.print();
98+
lru.put(2, 3);
99+
lru.print();
100+
return 0;
101+
}

addBinary.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include<iostream>
2+
#include<string>
3+
4+
using namespace std;
5+
string addBinary(string a, string b)
6+
{
7+
string result;
8+
int carry = 0;
9+
int index_a = a.size() - 1, index_b = b.size() - 1;
10+
while(index_a >= 0 && index_b >= 0)
11+
{
12+
int i = a[index_a] - 48;
13+
int j = b[index_b] - 48;
14+
char c = (i + j + carry) % 2 + 48;
15+
carry = (i + j + carry) / 2;
16+
result.push_back(c);
17+
--index_a; --index_b;
18+
}
19+
cout << "reverse s1 : " << result << endl;
20+
//单层遍历
21+
while(index_a >= 0)
22+
{
23+
int i = a[index_a] - 48;
24+
char c = (i + carry) % 2 + 48;
25+
carry = (i + carry) / 2;
26+
result.push_back(c);
27+
--index_a;
28+
}
29+
30+
while(index_b >= 0)
31+
{
32+
int i = b[index_b] - 48;
33+
char c = (i + carry) % 2 + 48;
34+
carry = (i + carry) / 2;
35+
result.push_back(c);
36+
--index_b;
37+
}
38+
39+
if(carry == 1)
40+
{
41+
result.push_back((char)carry + 48);
42+
}
43+
44+
cout << "result : " << result << endl;
45+
46+
string s;
47+
for(int i = result.size() - 1; i >= 0; --i)
48+
{
49+
s.push_back(result[i]);
50+
}
51+
52+
return s;
53+
}
54+
55+
int main()
56+
{
57+
string a = "101";
58+
string b = "101";
59+
cout << addBinary(a, b) << endl;
60+
return 0;
61+
}

0 commit comments

Comments
 (0)