08 2020 档案
摘要:字符串解码 题目链接:https://leetcode-cn.com/problems/decode-string/ class Solution { public: string decodeString(string s) { //用一个数字栈 保存数字 //一个string栈 保存str //
阅读全文
摘要:每日温度 题目链接:https://leetcode-cn.com/problems/daily-temperatures/ class Solution { public: //每个元素找到它右边第一个比它大的元素的位置,求它们的距离 vector<int> dailyTemperatures(v
阅读全文
摘要:空类 空类 #include <iostream> using namespace std; class Base { void f(){}; }; int main() { cout << sizeof(Base) << endl; //1 return 0; } 虚继承空类的类大小 #inclu
阅读全文
摘要:单调栈 找每个数左边离它最近的比它大/小的数 模板 stack<int> st; for(int i = 1; i <= n; i++) { while(st.size() > 0 && x <= st.top()) st.pop(); st.push(arr[i]); } 单调栈 题目链接:htt
阅读全文
摘要:strcpy #include <iostream> #include <assert.h> using namespace std; char * my_strcpy(char* str1,const char* str2) { char* p = str1; assert(str1 != NUL
阅读全文
摘要:字母异位词分组 题目链接:https://leetcode-cn.com/problems/group-anagrams/ class Solution { public: vector<vector<string>> groupAnagrams(vector<string>& strs) { un
阅读全文
摘要:压缩字符串 题目链接:https://leetcode-cn.com/problems/string-compression/ class Solution { public: int compress(vector<char>& chars) { int n = chars.size(); int
阅读全文
摘要:双指针 对于一个序列,用两个指针维护一段区间; 对于两个序列,维护某种次序,比如归并排序中合并两个有序序列的操作; for(int i = 0, j = 0; j < n; j++) { //当前维护的区间不满足条件,i向前移动到满足条件 while(i < j && check(i, j)) i+
阅读全文
摘要:在排序数组中查找数字 题目链接:https://leetcode-cn.com/problems/zai-pai-xu-shu-zu-zhong-cha-zhao-shu-zi-lcof/ class Solution { public: int search(vector<int>& nums,
阅读全文
摘要:找精确值模板 int l = 0, r = n - 1; while(l <= r) { int mid = l + r >> 1; if(arr[mid] == target) return mid; else if(arr[mid] > target) r = mid - 1; else l =
阅读全文
摘要:滑动窗口 维护一个窗口,不断滑动; for(int l = 0, r = 0; r < s.size();) { //增大窗口 win.add(s[r]); r++; //窗口右移 while(满足某个条件) { win.remove(s[l]); l++; //更新某个值 } } 模板 void
阅读全文
摘要:中心扩散法 int expand(string s, int i, int j) { int n = s.size(); int cnt = 0; while(i >= 0 && j < n) { if(s[i] == s[j]) { cnt++; i--; j++; } else break; }
阅读全文
摘要:0-1背包 题目链接:https://www.acwing.com/problem/content/2/ 思路 实现 #include <iostream> using namespace std; const int N = 1010; int dp[N][N]; int V[N]; int W[
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/subarray-sum-equals-k/ 前缀和 + 枚举 class Solution { public: int subarraySum(vector<int>& nums, int k) { int n = num
阅读全文
摘要:58. II左旋转字符串 题目链接:https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/ class Solution { public: //块交换问题: "abcdefg"; 先交换前2个:"bacdefg"; 再交换
阅读全文
摘要:无向图中连通分量的数目 题目链接: https://leetcode-cn.com/problems/number-of-connected-components-in-an-undirected-graph/ class Solution { public: vector<int> parent;
阅读全文
摘要:并查集 将两个集合合并; 询问两个元素是否在一个集合当中; 基本原理:每个集合用一颗树来维护,每个集合根节点的编号是当前集合的编号,每个节点存储它的父节点,p 表示x的父节点; 如何判断树根: p[x] = x; 查找:如何求x的集合编号: 想判断某个点属于哪个集合,找到它所在的树的树根; whil
阅读全文
摘要:BFS 完全平方数 题目链接:https://leetcode-cn.com/problems/perfect-squares/ class Solution { public: int numSquares(int n) { int max_sqrt = floor(sqrt(n)); queue
阅读全文
摘要:46. 全排列 题目链接:https://leetcode-cn.com/problems/permutations/ class Solution { public: vector<bool> flag; vector<vector<int>> res; vector<vector<int>> p
阅读全文
摘要:78. 子集 题目链接:https://leetcode-cn.com/problems/subsets/ class Solution { public: vector<vector<int>> res; vector<int> path; vector<vector<int>> subsets(
阅读全文
摘要:39. 组合总数 题目链接:https://leetcode-cn.com/problems/combination-sum/ class Solution { public: vector<vector<int>> res; vector<int> path; vector<vector<int>
阅读全文
摘要:auto类型常规推断 C++11中,auto用于自动类型推断,在声明变量的时候根据变量初始值的类型自动为此变量选择匹配的类型,不需要程序员显示指定类型; auto自动类型推断发生在编译器期间,所以不会影响程序执行期间的性能; auto定义变量必须立即初始化;这样编译器才能推断它的实际类型;编译的时候
阅读全文
摘要:decltype 用于推导类型,对于一个给定的变量或者表达式,使用decltype可以得到其类型; decltype的自动类型推断会发生在编译阶段和auto一样 decltype不会直接计算表达式的值 变量 #include <iostream> #include <string> #include
阅读全文
摘要:引用折叠规则 引用折叠 C++11新标准 C++中有明确含义的引用只有两种,一种是&左值引用类型,一种是带&&右值引用类型; 折叠规则:如果任意一个引用为左值引用,那么结果就为左值引用,否则就是右值引用 一共下面四种情况 & & & && && & && && #include <iostream>
阅读全文
摘要:查看类型推断结果 需要清楚编译器推断出来的模板参数以及普通参数类型是什么; 通过查看编译器类型推断的结果;掌握C++类型推断的规则; 借助boost库:利用boost库来把编译器推断的类型信息打印出来;官网:www.boost.org 模板类型推断 #include <iostream> #incl
阅读全文
摘要:类型区别基本概念 #include <iostream> using namespace std; template <typename T> void func(const T &val){} //T是什么类型; //val是什么类型; int main() { func(10); //T是什么类
阅读全文
摘要:总述 ()圆括号是函数调用的明显标记,()也叫函数调用运算符; 如果在类中重载了函数调用运算符,就可以想使用函数一样使用该类的对象了; 只要对象所属的类重载了()函数调用运算符,那么这个类对象就变成了可调用的;而且可以调用多个版本的(),只要在参数类型或者数量上有差别即可; 重载了()的类实例化的对
阅读全文
摘要:用法简介 一种可调用对象,定义了一个匿名函数,并且可以捕获一定范围内的变量; auto f = [](int a)->int{return a + 1}; cout << f(1) << endl; 特点 匿名函数,也可以理解为可调用的代码单元;或者理解成未命名的内联函数; 有一个返回类型,一个参数
阅读全文
摘要:可调用对象 函数指针 void myfunc(int tv) { cout << tv << endl; } int main() { void (*func)(int) = myfunc; //定义函数指针并赋值; func(10); //调用函数,可调用对象; } 仿函数 具有operator(
阅读全文
摘要:手撕String,面试中经常会问到,今天我们自己实现并梳理MyString类,同时加深对拷贝构造,移动构造;运算符重载的理解; 成员变量 字符串MyString类中两个成员变量 char *的字符串m_data; 保存字符串大小的m_size; 构造函数和析构函数 MyString(const ch
阅读全文
摘要:memcpy #include <iostream> #include <string.h> #include <assert.h> using namespace std; void* my_memcpy(void* dst, const void* src, size_t size) { ass
阅读全文
摘要:概述 用类模板实例化一个特定的类; 编译器不能为类模板推断模板参数,为了使用类模板,必须在模板名后用<>来提供额外的信息; 同一套代码,可以应付不同的数据类型; 类模板定义 格式 template <typename T, ...> class name { } 实例化类模板的时候,必须要有类的全部
阅读全文
摘要:概述 泛型编程,是以独立于任何特定类型的方式编写代码,使用泛型编程时,需要提供具体程序实例所操作的类习惯或者值; 模板是泛型编程的基础,模板是创建类或者函数的蓝图或者公式,给这些蓝图或者公式足够的信息,让这些蓝图或者公式真正的转变为具体的类或者函数,这种转换发生在编译时; 模板支持将类型作为参数的程
阅读全文