add: 220108-CN
This commit is contained in:
parent
a4b2d5557f
commit
7cc71916e3
|
|
@ -0,0 +1,69 @@
|
||||||
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
static std::vector<int> grayCode(const int n) {
|
||||||
|
std::vector<int> ret{0};
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
ret.insert(ret.end(), ret.rbegin(), ret.rend());
|
||||||
|
for (int j = 1 << i; j < (1 << (1 + i)); ++j)
|
||||||
|
ret[j] |= (1 << i);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class SolutionOld {
|
||||||
|
public:
|
||||||
|
static std::vector<int> grayCode(const int n) {
|
||||||
|
int lim = 1 << n, cur = 0;
|
||||||
|
std::vector<int> ret{0};
|
||||||
|
ret.reserve(lim);
|
||||||
|
int* const vis = new int[1 + (lim >> 5)]{0x1};
|
||||||
|
for (int i = 1; i < lim; ++i) {
|
||||||
|
for (int j = 0; j < n; ++j) {
|
||||||
|
if (!(vis[(cur ^ (1 << j)) >> 5] & (1 << ((cur ^ (1 << j)) & 0x1F)))) {
|
||||||
|
ret.push_back(cur ^= (1 << j));
|
||||||
|
vis[cur >> 5] |= (1 << (cur & 0x1F));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delete[] vis;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Solution0ms {
|
||||||
|
public:
|
||||||
|
static std::vector<int> grayCode(int n) {
|
||||||
|
std::vector<int> ans = {0, 1};
|
||||||
|
for (int i = 2; i <= n; i++) {
|
||||||
|
int x = (1 << (i - 1));
|
||||||
|
int idx = ans.size() - 1;
|
||||||
|
while (idx >= 0) {
|
||||||
|
int num = (ans[idx] + x);
|
||||||
|
ans.push_back(num);
|
||||||
|
idx--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
const int n = 30;
|
||||||
|
auto t1 = std::chrono::high_resolution_clock::now();
|
||||||
|
SolutionOld::grayCode(n);
|
||||||
|
auto t2 = std::chrono::high_resolution_clock::now();
|
||||||
|
Solution::grayCode(n);
|
||||||
|
auto t4 = std::chrono::high_resolution_clock::now();
|
||||||
|
Solution0ms::grayCode(n);
|
||||||
|
auto t5 = std::chrono::high_resolution_clock::now();
|
||||||
|
std::cout << "SolutionOld: " << std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count() << "ms" << std::endl;
|
||||||
|
std::cout << "Solution: " << std::chrono::duration_cast<std::chrono::milliseconds>(t4 - t2).count() << "ms" << std::endl;
|
||||||
|
std::cout << "Solution0ms: " << std::chrono::duration_cast<std::chrono::milliseconds>(t5 - t4).count() << "ms" << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
@ -3,4 +3,4 @@ PROJECT(2201)
|
||||||
|
|
||||||
SET(CMAKE_CXX_STANDARD 23)
|
SET(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
ADD_EXECUTABLE(2201 220107.cpp)
|
ADD_EXECUTABLE(2201 220108-CN.cpp)
|
||||||
|
|
@ -3,6 +3,7 @@ PROJECT(LeetCodeDaily)
|
||||||
|
|
||||||
SET(CMAKE_CXX_STANDARD 23)
|
SET(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
|
# -Ofast -march=native -mtune=native -finline-functions -ffast-math -fomit-frame-pointer
|
||||||
SET(CMAKE_CXX_FLAGS "-Wall")
|
SET(CMAKE_CXX_FLAGS "-Wall")
|
||||||
|
|
||||||
ADD_EXECUTABLE(LeetCodeDaily main.cpp)
|
ADD_EXECUTABLE(LeetCodeDaily main.cpp)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue