- 博客(35)
- 收藏
- 关注
原创 bind2nd(const _Fn2& _Func, const _Ty& _Right) 源码解析
小白,有错欢迎评论执教。 // TEMPLATE FUNCTION bind2ndtemplate<class _Fn2, class _Ty> inline binder2nd<_Fn2> bind2nd(const _Fn2& _Func, const _Ty& _Right) { // return a binder2nd functo...
2019-12-14 18:52:49 194
原创 413. 等差数列划分
i>2时:A[i]构成等差只与A[i-1]有关。而且以A[i]结尾的等差数列个数会比A[i-1] 多一个。class Solution {public: int numberOfArithmeticSlices(vector<int>& A) { int dp = 0; int sum = 0; for (...
2019-09-16 17:01:25 169
原创 中文 leetcode 191 位1的个数
比较下面两种代码效率:class Solution {public: int hammingWeight(uint32_t n) { int _cnt = 0; while(n>0){ _cnt ++; n = n&(n-1); } return _cnt;...
2019-09-09 20:48:29 93
原创 中文leetcode 338 比特位计数
方法1:正整数 ii&(i-1) 是去掉 i二进制表示序列 从左往右第一个1.所以说i+1也不会发生进位,那么我们只要统计i&(i-1) 里面1的个数,在加上1,就是 i 的二进制表示中 1的个数。i&(i-1) 的结果比 i小,所以只要将 比 i小的整数的1的个数 存放在数组里面,在计算整数 i中 1的个数的时候,可以用到 前面部分的数组。---------...
2019-09-09 20:15:03 92
原创 leetcode 647. Palindromic Substrings
字符串长度 ni 表示子串最后一个一个字符索引j 表示子串第一个字符索引int dp[n][n]注: 01 == 1表示s[0] ==s[1], 01 == 0 表示s[0] != s[1]?loop 1: i = 0 j<=i 11(表示同一个字符)那么一定是回文子串loop 2:i = 1; ? ?01,11 (i-j<2)loop 3:i = 2;...
2019-07-07 16:16:58 71
原创 leetcode 103. Binary Tree Zigzag Level Order Traversal
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */cla...
2019-07-07 13:23:59 70
原创 leetcode Two Sum
/*给定一个vector<int> nums,找到两个数等于一个target方法:采用map<int,int> // value ,index1 2, 7, 9,target 9mp.count(target-nums[0])等于0,放入 mp[nums[i]]mp.count(target-nums[1])等于0,放入 mp[nums[i]]所以2,要去找...
2019-07-06 15:50:51 54
原创 Leetcode 414. Third Maximum Number
// 要求时间复杂度为O(n),所以不能进行排序int thirdMax(vector<int>& nums) { if (nums.size() == 0) return -1; long one = LONG_MIN;// 这么写的原因是因为int的最小值INT_MIN,如果将one写成 int one = INT_MIN // ,那么如果nums里面第三个数是I...
2019-06-17 16:12:27 55
原创 Leetcode103. Binary Tree Zigzag Level Order Traversal
遇到一个mark表示读到一行/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL)...
2019-06-17 12:24:14 66
原创 leetcode 102. Binary Tree Level Order Traversal
每一层我们末尾放入一个标记,碰到标记表示一层遍历完毕,最后叶子节点左右孩子为NULL,所以这次碰到标记则表示已经将叶子层遍历完毕,整棵树已经遍历完毕。vector<vector<int> > levelOrder(TreeNode* root) { queue<TreeNode*> que; vector<vector<int&g...
2019-06-17 11:10:49 54
原创 leetcode897Increasing Order Search Tree
//这种方法返回的是vector,代码量多,第二种方法返回void,传入的是vector<>,运行快,代码量少。vector<int> getnode(TreeNode* root) { vector<int> vec; vector<int> tmp; if (root == NULL) return vec; ...
2019-06-14 17:29:03 62
原创 leetcode559. Maximum Depth of N-ary Tree
/*// Definition for a Node.class Node {public: int val; vector<Node*> children; Node() {} Node(int _val, vector<Node*> _children) { val = _val; childre...
2019-06-14 15:36:21 91
原创 leetcode 110. Balanced Binary Tree
关于平衡二叉树的定义:给个网上链接,感谢这位博主的简书,收益颇丰https://www.jianshu.com/p/fdb3c8c331f1/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;...
2019-06-14 14:44:03 70
原创 字符串按照空字符切割
const vector<string> mysplit(const string& s, const char& c){ string buff{ "" }; vector<string> v; for (auto n : s) { if (n != c) buff += n;// n == c &...
2019-06-14 10:53:14 422
原创 leetcode104. Maximum Depth of Binary Tree
// 从叶子节点考虑,叶子节点子节点为null,所以叶子节点return 1,也就是叶子节点深度为1,从叶子节点往上数树的max深度int maxDepth(TreeNode* root) { if (root == NULL) return 0; int left = 0; int right = 0; left += maxDepth(root...
2019-06-14 10:42:31 75
原创 数组中存在两个只出现一次的数,其他元素只重复两次,找到只出现一次的数
int res = 0; for (int i = 0; i < vec.size(); i++) { res ^= vec[i]; } int i = 0; for (; i < 32; i++) { if ((res&(1 << i)) == 1) break;...
2019-06-14 09:51:07 808
原创 leetcode 1008. Construct Binary Search Tree from Preorder Traversal
这题和701.Insert into a Binary Search Tree 解体思路一样。/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) :...
2019-06-14 01:41:38 196
原创 leetcode700 Search in a Binary Search Tree
//递归/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*...
2019-06-14 01:18:46 63
原创 leetcode654 leetcode654 最大二叉树(利用vector创建)
[3,2,1,6,0,5]stk is emptypush 32<3 3->(right)2 push 2stk 3 21<2 2->(right)1 push 1stk 3 2 16现在是最大的,拿6和stk里面的各个元素进行比较,将小于6的作为左孩子。stk里面的元素已经存在了树结构关系。6>1 6->(left)1 pop ...
2019-06-13 22:35:37 161
原创 leetcode 949
bool isAlienSorted(vector<string> &words, string order) { int map[26]; for (int i = 0; i < 26; i++) map[order[i] - 'a'] = i; for (string &w : w...
2019-05-13 00:53:18 207
原创 基类指针指向派生类对象
一、 函数在基类和派生类中都存在二、函数在基类中不存在,在派生类中存在三、 将基类指针强制转换为派生类指针四、基类中存在虚函数的情况 (多态)...
2019-04-29 16:21:28 888
原创 strspn strcspn
(1) strspn函数原型:size_t strspn(const char *s, const char * reject);功能:若strcspn()返回的数值为n, 则代表字符串s 从s第一个字符开始连续有n 个字符都包含reject 的字符内.注意:字符串 s 可以从reject 的任意位置开始匹配。例子1:str 在object 中一直找,直到找到a开始匹配...
2019-04-29 15:23:11 169
原创 leetcode 24. Swap Nodes in Pairs
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/class Solution {public: ListNode* sw...
2019-04-19 00:44:09 103
原创 leetcode 143. Reorder List
举个例子:用例子给出方法的基本思想1->2->3放入stack<ListNode*> 为 (为了方便,这里简单的用val去表示)3(栈顶)21(栈底)current = head根据 current->next 取到 2,,用 tmp = 2(这里2 还有后面的next ,即为 3)head->next = 3将3 出栈...
2019-04-18 22:03:48 147
原创 leetcode 445. Add Two Numbers II
链表从后面开始加起,所以要考虑用容器去作为中介,stack是先进后出,非常的合适。/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*...
2019-04-18 20:16:47 47
原创 leetcode 19. Remove Nth Node From End of List
做关于链表类的题目,经常提示 空指针 ,导致每次写程序,都会写if()去判断/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/c...
2019-04-18 19:42:59 54
原创 leetcode 90 Subsets II
第一种方法:class Solution {public: vector<vector<int>> subsetsWithDup(vector<int>& nums) { vector<vector<int>> vec({ {} }); int length = nums.size(); if...
2019-04-18 01:12:06 50
原创 leetcode 78 subsets
想法:举个例子:给定{1,3}{}也是子集A = {{}}首先拿到 1,放入到A的各个元素中,然后添加到AA = {{},{1}}拿到3,放入到A的各个元素中,然后添加到AA = {{},{1},{3},{1,3}}代码:class Solution {public: vector<vector<int>> subsets(...
2019-04-18 00:44:11 82
原创 leetcode 20. Valid Parentheses
bool isValid(string s) { stack<char> stck; for (int i = 0; i < s.length(); i++) { if ((s[i] == '(') || (s[i] == '{') || (s[i] == '[')) stck.push(s[i]);...
2019-04-17 21:39:12 49
原创 leetcode 23 merge-k-sorted-lists
第一种想法:第一个链表和第二个合并成一个新链表,将新链表与第三个链表合并,以此类推。代码如下/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}...
2019-04-17 21:34:27 72
原创 leetcode 21 merge-two-sorted-lists
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/class Solution {public: ListNode* me...
2019-04-17 20:40:23 55
原创 leetcode 61 Rotate list
struct ListNode { int val; ListNode *next; ListNode(ListNode* tmp) { val = tmp->val; next = tmp->next; };};ListNode *head = NULL;void ctL(vector<int&...
2019-04-17 16:35:29 74
原创 leetcode 16 3Sum closest
int threeSumClosest(vector<int> &nums,int &target){ int result; int gap = INT_MAX; int len = nums.size(); if (len <= 2) return gap; sort(nums.begin(),nums.end...
2019-04-16 11:22:41 73
原创 Leetcode15 3Sum
这里提供两种方法第一中非常的耗时,没有经过优化bool equalto(vector<vector<int>>num1, vector<int> num2){ //if (num2.size() == 0) //{ // return false; //} sort(num2.begin(), num2.e...
2019-04-15 21:16:30 75
原创 leetcode 50 Pow(x, n)
double myPow(double x, long n) { long temp = n; if((x <= -100.0)&&(x >= 100)) return 0; if((temp<INT_MIN)||(temp>INT_MAX)) return 0;// if(temp<0) { x...
2019-04-04 21:14:16 60
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人