随笔分类 - C/CPP学习
摘要:题目就是给一串全小写的字符,要求删除重复的字符。另外最后的结果必须首先按照原文顺序排列,其次是按照字典序排列。比如说:Given"bcabc"Return"abc"Given"cbacdcbc"Return"acdb"按照提示,说是贪心算法。贪心吗,每一步只要当前最优的解。好吧,当前最优的解是什么?...
阅读全文
摘要:重要的事情说三遍:array 并不会被默认初始化array 并不会被默认初始化array 并不会被默认初始化
阅读全文
摘要:其实和大小没啥太大关系,主要因素是元素个数,也就是说,1,2 和 3,4 可能构成的排列组合个数是一样的,大概的思路就是这样的:式子出来了,那代码也就出来了:int numTrees(int n) { vector treeNumList(n + 1); function getTree...
阅读全文
摘要:我第一时间想到的方案是化归而不是规划,毕竟之前的问题类似,所以就有了这个版本:int uniquePathsWithObstacles(vector>& obstacleGrid) { if (obstacleGrid.empty()){ return 0; } co...
阅读全文
摘要:但是解答很简单。因为机器人只能向右 / 下 移动,所以要到达目标 A 的可能路线就是到达它正上方的那一块的可能路线数和到达它正右方的那一块的可能路线数之和。所以就简单了:int uniquePaths(int m, int n){ vector> record((m + 1), vector(...
阅读全文
摘要:我最早的思路是反过来遍历,结果总是不对。因为老是觉得动态规划就是列递归,所以一心琢磨着怎样递归,导致一直不对。正确的思路应该是什么呢?应该是尝试用符号表示其最优的状态和其子状态,然后找出状态之间转移的方法。最后实现这个方法。最长增长序列的最优状态是什么样子呢?既然是最长,肯定是要容纳尽可能多的元素,...
阅读全文
摘要:class Complex{private: double real_ = 0.0; double imag_ = 0.0;public: Complex() = default; explicit Complex(double real, double imag): ...
阅读全文
摘要:std::mutex m; std::condition_variable cond; int flag = 0; constexpr int kLoopTimes = 10; void foo(int id){ for (int i = 0; i != kLoopTimes; ++i){ std:
阅读全文
摘要:templateIterator parallelFind(Iterator first, Iterator last, MatchType match){ struct FindElement { void operator()(Iterator begin, Itera...
阅读全文
摘要:const std::string getCurrentSystemTime(){ auto tt = std::chrono::system_clock::to_time_t (std::chrono::system_clock::now()); struct tm* ptm = local...
阅读全文
摘要:class ThreadsJoiner{ std::vector& threads;public: ThreadsJoiner(std::vector& threads_): threads(threads_) {} ~ThreadsJoiner() ...
阅读全文
摘要:int foo(int n){ if (n < 1){ return 0; } int result = 0; for(int i = 1, factor = 1; i <= n; (factor += (pow(10.0, i))), ++i){ ...
阅读全文
摘要:templateT parallelAccumulate(Iterator first, Iterator last, T init){ size_t const length = std::distance(first, last); if (length == 0){ ...
阅读全文
摘要:templatestruct AccumulateBlock{ T operator()(Iterator first, Iterator last) { return std::accumulate(first, last, T()); }};class Threa...
阅读全文
摘要:在知乎上看到一个问题,说自己的函数分明是对的,输入少量数据验证表明也是对的,可是当数据量达到一定规模的时候,程序会变得特别特别的慢,不知为什么。后来发现是因为他把函数声明和数组声明都写在 main 函数里了,声明在 main 函数中的变量都是分配在栈上的,因此当数据过多的时候,就会出现栈溢出的情况。...
阅读全文
摘要:templatestruct Sorter{ struct ChunkToSort { std::list data; std::promise> promise; }; ThreadSafeStack chun...
阅读全文
摘要:templateclass ThreadSafeStack{private: std::stack data; mutable std::mutex m;public: ThreadSafeStack() = default; ThreadSafeStack(con...
阅读全文
摘要:templateclass LockFreeStack{private: struct Node; struct CountedNode { int externalCount = 0; Node* ptr = nullp...
阅读全文
摘要:constexpr size_t maxHazardPointers = 100;struct HazardPointer{ std::atomic id; std::atomic pointer;};array hazardPointers;class Hazard...
阅读全文
摘要:templateclass LockFreeStack{private: struct Node { std::shared_ptr data; Node* next; Node(T const& value): ...
阅读全文
浙公网安备 33010602011771号