- 博客(209)
- 收藏
- 关注
原创 memset和sprintf小记
#include#include#includeusing namespace std;struct Test{string a;string b;};int main(){Test ta{“123”,“abc”};cout << ta.a << " " << ta.b << endl;memset(&ta, 0, ...
2019-12-30 22:23:46 460
原创 类的继承的实例
#include#include#include#include #include #includeusing namespace std;class Base{public:virtual void display() = 0;};class A :public Base{public:void display(){cout << “A class”...
2019-12-30 22:21:28 260
原创 N个括号有多少种合法的组合
两种算法,思想都是左括号始终大于等于右括号,当左括号等于右括号的时候且等于N的时候。记录为1#include#include#include#include #include #include#includeusing namespace std;void display(int x, int y){cout << x << " " << ...
2019-12-12 22:13:10 716
原创 C++11的一种多线程安全的单例模式
#include #include using namespace std;class Test{public:Test(){cout << “create Test class” << endl;num++;cout << “create Test class finish” << endl;}void ttt(){st...
2019-12-11 23:20:36 258
原创 C++访问控制
#includeusing namespace std;class base{public:int a = 555;private:int b = 666;protected:int c = 777;};class publicTest :public base{public:void fun(){cout << "publicTest: ";cout...
2019-12-09 22:42:03 135
原创 类中的枚举变量
#includeusing namespace std;class A{public:enum Test{ONE,TWO,THREE};};int main(){cout << sizeof(A) << endl;//枚举变量不占用类的空间A::Test b = A::TWO;cout << int(b) << end...
2019-11-27 20:55:03 413 1
原创 c++14 lambdas新特性
#include #include using namespace std;int main(){std::unique_ptr<int> p(new int);int x = 5;*p = 11;auto y = [p = std::move(p)]() { std::cout << "inside: " << *p << "\n"...
2019-11-26 23:18:23 128
原创 智能指针的使用
#include#include#include#includeusing namespace std;class SreBob{public:SreBob();SreBob(initializer_list il);//大括号初始化的调用的函数int size()const { return data->size(); }bool empty()const { ret...
2019-11-25 21:28:00 114
原创 C++重载
#includeusing namespace std;int maxt(int a, int b){if (a > b){return a;}return b;}int maxt(int a, int b,int c){if (a > b && a > c){return a;}else if (b > a &&am...
2019-11-24 21:34:45 110
原创 智能指针实现单链表
#include#include#include#includeusing namespace std;struct LinkNode;using sharedPtr = std::shared_ptr;struct LinkNode{int val;sharedPtr next;};class LinkList{private:int length;sharedP...
2019-11-23 22:29:16 388
原创 定时器的实现
#include#include#include#includeusing namespace std;#define MSOFDAY (246060*1000UL)void func(char str[100]){cout << sizeof(str) << endl;}class Timer{public:void fun(int num, s...
2019-11-19 23:39:31 162
原创 C++模板编程新的小特点
#includeusing namespace std;template<int *p>struct wrapper{int get() { return *p; }void set(int v) { *p = v; }};template<int &p>struct wrapper1{int get() { return p; }void ...
2019-11-16 22:45:41 105
原创 一系列测试题目
#includeusing namespace std;void Foo(char str1[100]){cout << "sizeof(str) = " << sizeof(str1) << endl;}void GetM1(char *p){p = (char *)malloc(100);}void test1(void){char ...
2019-11-13 22:30:31 186
原创 一系列小的题目
#include#includeusing namespace std;class A{public:virtual void f() = 0;};class B :virtual public A{public:void f(){cout << “B” << endl;}};class C :virtual public A{publi...
2019-11-11 21:51:03 132
原创 一系列小的题目
#include#include#include#include #include #include#include#includeusing namespace std;class A{public:A() :c(3), b©, a(b) {}public:int a;int b;int c;};class MyString{public:explici...
2019-11-09 23:24:05 121
原创 const_cast、static_cast、dynamic_cast总结实例
#include#include#include#include #include using namespace std;struct SA{int i;};enum enumtest{A=1,B};class Base{public:int m;virtual void fun(){}};class Dclass :public Base{pub...
2019-11-06 22:04:17 97
原创 一系列自测题目
#include#include#include#includeusing namespace std;class A{private:A(){cout << “create A” << endl;}int a1 = 100;static A a;static std::mutex mtx;public:static A getA(){i...
2019-11-05 23:02:17 113
原创 一系列题目自检
#include#include#include#include#includeusing namespace std;struct LinkNode{int val;LinkNode *next;LinkNode(int val, LinkNode *next = nullptr){this->val = val;this->next = next;}}...
2019-11-04 23:24:18 304
原创 信号量的实现和使用
#include#include#include<condition_variable>#include#includeusing namespace std;class Semaphore{public:explicit Semaphore(unsigned int count);~Semaphore();public:void wait();void si...
2019-09-29 00:30:47 235
原创 [LeetCode] 905. Sort Array By Parity 按奇偶排序数组
前后指针操作即可。#include#includeusing namespace std;void SortArrayByParity(vector &vec){int left = 0;int right = vec.size() - 1;while (left < right){while (left < right && vec[lef...
2019-09-27 00:01:16 181
原创 C++11信号量
信号量我感觉可以实现线程之间传递和唤醒。很有意思的一种线程实现和线程同步。#include#include#include#include<condition_variable>using namespace std;class semaphone{public:semaphone(unsigned int count) :count(count) {}void w...
2019-09-24 23:13:06 511
原创 [LeetCode] Valid Palindrome 验证回文字符串
#include#includeusing namespace std;bool isZimu(char ch){if (ch >= ‘a’ && ch <= ‘z’ || ch >= ‘A’ && ch <= ‘Z’){return true;}else{return false;}}bool ValidPali...
2019-09-23 23:14:18 98
原创 [LeetCode] 252. Meeting Rooms 会议室
不是很难#include#include #includeusing namespace std;bool MeetingRooms(vector<std::pair<int, int>> &vec){sort(vec.begin(), vec.end(), [](std::pair<int, int> x, std::pair<in...
2019-09-20 23:59:48 204
原创 [LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和
左右指针的想法用的好多啊#include#includeusing namespace std;int maxt(int a, int b){if (a > b){return a;}return b;}int mint(int a, int b){if (a < b){return a;}return b;}int MinimumSize...
2019-09-19 23:10:16 97
原创 [LeetCode] 137. Single Number II 单独的数字之二
1.位运算很巧妙的应用啊。#include#includeusing namespace std;int SingleNumberII(vector &vec){int res = 0;for (int i = 0; i < 32; i++){int sum = 0;for (int j = 0; j < vec.size(); j++){sum += ...
2019-09-18 23:32:51 139
原创 [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数
需要对于完全二叉树和满二叉树的概念和性质很熟悉#include#include#includeusing namespace std;struct TreeNode{int val;TreeNode lChild;TreeNode rChild;TreeNode(int val, TreeNodel = nullptr, TreeNoder = nullptr){t...
2019-09-17 22:44:01 97
原创 c语言结构体的比较
只判断是否相等,不比较大小有时候我们需要知道比较结构体的某一部分,以便做进一步处理。这就需要我们在比较的时候做一些结构体的地址的偏移和比较。我们通过比较test的fileds的是否相等,来进行下一步操作。#include<stdio.h>struct fileds{int a;int b;int c;};struct data{int user;int mask...
2019-09-16 22:28:28 6839
原创 [LeetCode] Trapping Rain Water 收集雨水
能收集雨水的地方肯定是两边都比他高的地方#include#includeusing namespace std;int maxt(int a, int b){if (a > b){return a;}return b;}int mint(int a, int b){if (a < b){return a;}return b;}int Trappi...
2019-09-15 21:36:13 68
原创 [LeetCode] Trapping Rain Water 收集雨水
好难的一道题目啊,不看图片不知道什么意思#include#includeusing namespace std;int maxt(int a, int b){if (a > b){return a;}return b;}int mint(int a, int b){if (a < b){return a;}return b;}int Tr...
2019-09-14 23:27:56 82
原创 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
#include#include<unordered_map>#includeusing namespace std;int maxt(int a, int b){if (a > b){return a;}return b;}int LongestSubstringTwoDistinctCharacters(string str){int res = ...
2019-09-14 00:22:59 76
原创 [LeetCode] Strobogrammatic Number 对称数
不是很难的一道题目#include#includeusing namespace std;bool StrobogrammaticNumber(string str){int n = str.size() - 1;int i = 0;while (i < n){if (str[i] == str[n]){if (str[i] != ‘1’ && st...
2019-09-12 23:45:00 91
原创 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
按照二叉树的性质遍历即可#includeusing namespace std;struct TreeNode{int val;TreeNode lChild;TreeNode rChild;TreeNode(int val, TreeNodel = nullptr, TreeNoder = nullptr){this->val = val;this->l...
2019-09-11 23:19:05 84
原创 [LeetCode] 72. Edit Distance 编辑距离
#include#includeusing namespace std;int minT(int a, int b){if (a < b){return a;}return b;}int EditDistanceT(string word1, int i,int lenn, string word2, int j,int lenm){if (i == lenn){...
2019-09-10 22:41:10 104
原创 [LeetCode] Binary Tree Right Side View 二叉树的右侧视图
树的层次遍历的思路来解决#include#include#includeusing namespace std;struct TreeNode{int val;TreeNode lChild;TreeNode rChild;TreeNode(int val, TreeNodel = nullptr, TreeNoder = nullptr){this->val...
2019-09-09 22:47:46 60
原创 非递归实现二叉树的后序遍历
#include#includeusing namespace std;struct TreeNode{int val;TreeNode lChild;TreeNode rChild;TreeNode(int val, TreeNodel = nullptr, TreeNoder = nullptr){this->val = val;this->lChild ...
2019-09-07 23:56:44 89
原创 很有意思的一段代码
#include<stdio.h>typedef int(*func)(int, int);int minT(int a, int b){return a < b ? a : b;}int maxT(int a, int b){return a > b ? a : b;}int sum(int a, int b){return a + b;}s...
2019-09-05 22:41:09 248
原创 [LeetCode] 214. Shortest Palindrome 最短回文串
#include#includeusing namespace std;string ShortestPalindrome(string str){string t = str;reverse(t.begin(), t.end());int n = t.size(),i=0;for (i = n; i >= 0; i–){if (str.substr(0, i) == ...
2019-09-03 22:57:52 147
原创 [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
二叉树的后序遍历的非递归实现(肯定要用栈)先序遍历顺序root->left->right后续遍历顺序left->right->root我们在先序遍历的的非递归实现的时候变为root->right->left,然后在翻转即可或者插入的时候做相关的操作也可以。#include#include#includeusing namespace...
2019-09-01 00:08:55 86
原创 一道有意思的多线程题目
#include#include#include#include<condition_variable>using namespace std;int num = 3;std::mutex mtx;std::condition_variable con;void test(int flag){unique_lockstd::mutex lk(mtx);whil...
2019-08-30 23:54:21 132
原创 30. Substring with Concatenation of All Words 串联所有单词的子串
#include#include#include#include<unordered_map>using namespace std;vector getlen(string s, vector &vec){vector res;unordered_map<string, int> mapstr;for (auto x : vec){ma...
2019-08-28 23:18:40 75
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人