Skip to content

Commit bf74d73

Browse files
committed
上周忘记交作业了,这周续上
1 parent c804da9 commit bf74d73

6 files changed

Lines changed: 122 additions & 1 deletion

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
4+
map<int, int> map;
5+
for (int i : arr1) map[i]++;
6+
7+
int offset = 0;
8+
vector<int> res(arr1.size(), 0);
9+
for (int i : arr2)
10+
while(map[i]--)
11+
res[offset++] = i;
12+
13+
for (auto &v : map)
14+
while (v.second-- > 0)
15+
res[offset++] = v.first;
16+
17+
return res;
18+
}
19+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
uint32_t reverseBits(uint32_t n) {
4+
int res = 0;
5+
for(int i = 0; i < 32; ++i)
6+
{
7+
res = (res << 1) + (n & 1);
8+
n >>= 1;
9+
}
10+
return res;
11+
}
12+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
int hammingWeight(uint32_t n) {
4+
int cnt = 0;
5+
for (int i = 0; i < 32; i++) {
6+
cnt += n & 1;
7+
n = n >> 1;
8+
}
9+
return cnt;
10+
}
11+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
bool isPowerOfTwo(int n) {
4+
if (n <= 0) return false;
5+
while (n > 1) {
6+
if (n & 1) return false;
7+
n = n >> 1;
8+
}
9+
return true;
10+
}
11+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
bool isAnagram(string s, string t) {
4+
map<char, int> map1;
5+
map<char, int> map2;
6+
for (char c : s) map1[c]++;
7+
for (char c : t) map2[c]++;
8+
9+
if (map1.size() != map2.size()) return false;
10+
11+
map<char, int>::iterator it1, it2;
12+
for (it1 = map1.begin(), it2 = map2.begin(); it1 != map1.end(); it1++, it2++)
13+
if (it1->first != it2->first || it1->second != it2->second)
14+
return false;
15+
16+
return true;
17+
}
18+
};

Week08/NOTE.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,51 @@
1-
学习笔记
1+
学习笔记
2+
3+
4+
1. 快速排序
5+
int parttion(int *arr, int begin, int end) {
6+
if (begin < 0 || end < begin || !arr)
7+
return -1;
8+
9+
int pis = arr[end];
10+
int i = begin - 1;
11+
for (int j = begin; j < end; j++) {
12+
if (arr[j] <= pis) {
13+
i++;
14+
swap(arr[j], arr[i]);
15+
}
16+
}
17+
swap(arr[i], arr[end]);
18+
return i + 1;
19+
}
20+
21+
void qsort(int *arr, int begin, int end) {
22+
if (begin < 0 || end < begin || !arr)
23+
return;
24+
25+
stack<int> s;
26+
s.push(end);
27+
s.push(begin);
28+
29+
while (s.size()) {
30+
int left = s.top();
31+
s.pop();
32+
int right = s.top();
33+
s.pop();
34+
35+
int k = pattion(arr, left, right);
36+
if (left < k) {
37+
s.push(k - 1);
38+
s.push(left));
39+
}
40+
if (right > k) {
41+
s.push(right);
42+
s.push(k + 1));
43+
}
44+
}
45+
}
46+
47+
48+
2. 位图
49+
布隆过滤器
50+
LRU
51+
整数类算法题

0 commit comments

Comments
 (0)