cocolor
cocolor
全部文章
分类
题解(33)
归档
标签
去牛客网
登录
/
注册
cocolor的博客
全部文章
(共33篇)
题解 | #第一个只出现一次的字符#
用map计数 class Solution { public: int FirstNotRepeatingChar(string str) { unordered_map<char, int> dic; for(char c:str){ ...
2021-06-06
0
396
题解 | #丑数#
首先丑数都是只有质因子235,刚开始没明白这个题目,后来想到所有的数都可以拆成质数之积(毕竟质数的定义就是只有1和自身是因数,也就是不能再拆,想要拆到最后只有235,那形成的时候也应该只有235),那想要235就要由这三个数不停的互相累乘就可以了 class Solution { public: ...
2021-06-06
0
386
题解 | #把数组排成最小的数#
写个仿函数,自定义比较规则,那个放前面更小就把哪个放前面,方便 class Solution { public: struct comp{ bool operator()(string a,string b){ return a+b<b+a; ...
2021-06-06
0
310
题解 | #连续子数组的最大和#
这是个标准的动态规划,需要注意的是明确好dp的意义,因为是要连续数组,所以我们可以规定dp[i]一定包含i class Solution { public: int FindGreatestSumOfSubArray(vector<int> array) { if...
2021-06-06
0
328
题解 | #最小的K个数#
全排列与map红黑树,回头更新一下手写实现最小堆 class Solution { public: vector<int> GetLeastNumbers_Solution(vector<int> input, int k) { if(k>inp...
2021-05-27
0
403
题解 | #数组中出现次数超过一半的数字#
写完看评论去才发现没用上过半的条件,做的麻烦了。其实差不多,两种方法如下: class Solution { public: int MoreThanHalfNum_Solution(vector<int> numbers) { sort(numbers.begi...
2021-05-26
0
364
题解 | #字符串的排列#
c++ 全排列问题 回溯方法,最好使用一个vector<bool>用来表示有没有使用过。去重也是需要注意的地方</bool> class Solution { public: vector<string> ans; string path=""; ...
2021-05-26
0
370
题解 | #二叉搜索树与双向链表#
看了评论区大佬方法,pre要加引用或者干脆设置为一个成员变量,因为回溯后还要用到pre class Solution { public: void backtra(TreeNode* cur,TreeNode*& pre){ if(!cur) return; ...
2021-05-26
0
318
题解 | #复杂链表的复制#
花了不少时间,首先递归不行,然后看评论方法以为会了,自己写结果发现原来的链表也不能改变,下意识地以为会销毁原来的测试现在的 /* struct RandomListNode { int label; struct RandomListNode *next, *random; ...
2021-05-25
0
450
题解 | #二叉树中和为某一值的路径#
递归 class Solution { public: vector<vector<int>> ans; vector<int> path; void backtra(TreeNode* root,int expectNumber){ ...
2021-05-25
0
349
首页
上一页
1
2
3
4
下一页
末页