文学网站建设,学校网站开发实际意义,网站后台无ftp,wordpress移动端视频文章目录 LeetCode 练习随笔力扣上的题目和 OJ题目相比不同之处#xff1f;定义问题排序问题统计问题其他 LeetCode 练习随笔 做题环境 C 中等题很值#xff0c;收获挺多的 不会的题看题解#xff0c;一道题卡1 h #xff0c;多来几道#xff0c;时间上耗不起。 力扣上的题… 文章目录 LeetCode 练习随笔力扣上的题目和 OJ题目相比不同之处定义问题排序问题统计问题其他 LeetCode 练习随笔 做题环境 C 中等题很值收获挺多的 不会的题看题解一道题卡1 h 多来几道时间上耗不起。 力扣上的题目和 OJ题目相比不同之处
一开始上手力扣不习惯OJ 的题目提交的是完整代码力扣上的C只提交目标函数代码比如某个题目你只需要完成topKFrequent(nums,k)这个函数。
class Solution {vectorint topKFrequent(vectorint nums, int k) {}
};这也就意味着程序设计的输入不需要自己额外设计了同时限制了你数据的输入和返回内容的格式。 多亏了oj许多来自力扣的题并补全了完整代码目标函数部分空缺渐渐习惯了编写目标函数的答题习惯。
定义问题 在哪定义结构体 有两个位置可写 1类里面讲究的话写在private中这里就不讲究了。 class Solution {
public:struct node{int data; };vectorint topKFrequent(vectorint nums, int k) {}
};2顶部区域 struct node{int data; };class Solution {
public:struct node{int data; };vectorint topKFrequent(vectorint nums, int k) {}
};在哪定义全局变量 1顶部区域 #includealgorithm
int a 3;
class Solution {
public:vectorint topKFrequent(vectorint nums, int k) {vectorintv;v.push_back(a);
return v;}
};2换个方式传参方式改为引用 很多全局变量是可以被替代的比如希望值被修改并返回到原来作用域但返回值位置紧张。 暴力时全局变量开大数组还是有点用处的。 在哪定义头文件 一般是全面的多虑了。 下面这种做题区域顶部写头文件试过编译通过。 #includealgorithmclass Solution {
public:vectorint topKFrequent(vectorint nums, int k) {}
};排序问题 vector 存入结构体怎么自定义比较规则 1自定义规则为结构体形式优先队列也可用 class Solution
{public:typedef struct node{int data;int sum;} ll;
// static bool cmp(ll a, ll b)
// {
// return a.sum b.sum;
// }struct CompareBySumDesc{bool operator()( ll a, ll b) {return a.sum b.sum;}};vectorint topKFrequent(vector int nums,int k){vectorllv;//...//vector排序sort(v.begin(),v.end(),CompareBySumDesc());
// sort(v.begin(),v.end(),cmp);//...return res;}};2自定义规则为静态函数简单 class Solution
{public:typedef struct node{int data;int sum;} ll;static bool cmp(ll a, ll b){return a.sum b.sum;}// struct CompareBySumDesc
// {
// bool operator()(const ll a, const ll b) const
// {
// return a.sum b.sum;
// }
// };vectorint topKFrequent(vector int nums,int k){vectorllv;//...//vector排序
// sort(v.begin(),v.end(),CompareBySumDesc());sort(v.begin(),v.end(),cmp);//...return res;}};vector 存入结构体怎么逆置 用 stack。 存入结构体的 priority_queue 怎么自定义比较规则 给 ll 按照 sum 排序 class Solution
{
public:typedef struct node{int data;int sum;} ll;//试过了static 函数行不通只能 struct cmp{bool operator()( ll a, ll b){return a.sum b.sum;}};void f(){priority_queuell, vectorll, cmp pq;};
int main()
{Solution().f();return 0;
} //默认降序//没有结构体升序
priority_queue int,vectorint,greaterint q;使用 priority_queue 实现堆排序 优先队列本身是堆实现的。只需维护好优先队列的容量 k超过pop掉。
统计问题
map 统计的神。遍历别忘了迭代器初始化。
其他 删除 vector 任意位置元素 vectorintv;
//删除 v[i]
swap( v[i],v[v.size()-1]);
v.pop_back();其实只是 v.size()减少内存不释放。 vectorint v;v.push_back(11);v.push_back(22);v.push_back(33);v.pop_back();v.pop_back();v.pop_back();coutv.size()endl;v.push_back(44);coutv[0]endl;coutv[1]endl;coutv[2]endl;这里存入三个元素之后全部删除v.size() 结果是 0是预期的结果 但此时通过下标访问v[0]~v[2]原来的值仍然可以访问到删除时内存没有释放掉。 之后再加入新的元素44,v[0] 内容被覆盖了。 vectorvectorint v;尖括号嵌套尽可能规范地隔开 priority_queue int,vectorint,greaterint pq;这种尖括号紧贴一起依稀记得编译没通过。 连续子序列和问题优先考虑滑动窗口。