![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
CN_Thirteen
这个作者很懒,什么都没留下…
展开
-
剑指 Offer 64. 求1+2+…+n(递归)
参考答案递归调用知识点&& 、 ||两个判断只要前面的满足,后面的就无需判断,所以到n=0的时候,就停止了。class Solution {public: int sumNums(int n) { n && (n += sumNums(n-1)); return n; }};...原创 2020-09-11 15:13:18 · 111 阅读 · 0 评论 -
剑指offer29.顺时针打印矩阵(模拟路线)
类似于机器人路线规划那个题,把走的方向存起来,模拟行进路线,用数组标记走过的位置。class Solution {private: static constexpr int direction[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};public: vector<int> spiralOrder(vector<vector<int>>& matrix) { if (matrix.size() .原创 2020-09-06 16:47:51 · 157 阅读 · 0 评论 -
剑指offer65.不用加减法做加法(位运算^求和, &+<<求进位)
知识点与标题一致,存一个变量用来记录和,用一个变量用来记录进位class Solution {public: int add(int a, int b) { while(b) { int equal = a ^ b; b = ((unsigned int) (a & b) << 1); a = equal; } return a; .原创 2020-09-06 11:08:11 · 101 阅读 · 0 评论 -
剑指offer40.最小的K个数
知识点1.简单题官方答案:sort?2.简单题使用优先队列(现成的),存储最小的值。(√)中等题(待完成知识点)1.构建堆2.构建二叉搜索树使用大根堆,我最小的都比你数组的大,所以拉你入队,我把最大的踢了。class Solution {public: vector<int> getLeastNumbers(vector<int>& arr, int k) { //使用优先队列(priority queue),不是先到先得,而是big.原创 2020-09-06 10:26:57 · 74 阅读 · 0 评论 -
剑指offer50.第一次只出现一次的字符(数组映射)
知识点建立一个数组用来包含26个字母,遍历一遍s字符串,记录字母数字下标以及出现对应次数。再次循环字符串,找一找该nums中的次数,当第一个==1时就return。class Solution {public: char firstUniqChar(string s) { int nums[26] = {0}; for(int i = 0; i< s.size(); i++) { int location =.原创 2020-09-05 20:31:11 · 80 阅读 · 0 评论 -
剑指offer62. 圆圈中最后剩下的数字
问题详解:约瑟夫环问题最后一个留下的人的下标:location = (location + m) % 8;class Solution {public: int lastRemaining(int n, int m) { int pos = 0; for(int i = 2;i <= n; i++) { pos = (pos + m) % i; } return pos; }原创 2020-09-05 16:58:40 · 82 阅读 · 0 评论 -
剑指offer52 两个链表的第一个公共节点(我走过你的路,你走过我的路,当我们再次相遇,就在一起吧!)
知识点当我走到山穷水复,我会想去走走你的路,当你感觉手足无措,你来重走我的路,当我们在彼此的道路上再次相逢,命中注定我们会在一起。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: .原创 2020-09-05 16:02:40 · 106 阅读 · 0 评论 -
剑指offer32从上到下打印二叉树II
知识点:标准的BFS,或者说是标准的层次遍历:1.注意对于每层的元素个数,要每次循环改编q内的元素个数队列存的个数应该用定义变量去定义,而不能直接写成如下形式:for(int i = 0; i< q.size(); i++)其他就没啥需要注意得了:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *r.原创 2020-09-05 15:24:25 · 76 阅读 · 0 评论 -
剑指offer57.和为s的两个数字
类似于上一道的浮动框框法:知识点:双指针法用于数组就是搞一下数组的下标:一个从头(最小),一个从尾(最大)不合适就走一走调一调。class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { int sum =0; int i = 0; int j = nums.size() - 1; vector<.原创 2020-09-04 22:03:21 · 78 阅读 · 0 评论 -
剑指offer57 和为s的连续正数序列(浮动框框法)
使用的是经典的浮动框框法:1.知识点就是设定一个范围(上下界):i是下届设定 j是上届设定建立一个vector 用来保存从上界到下界的。每次循环动态改进上下界class Solution {public: vector<vector<int>> findContinuousSequence(int target) { int i = 1; int j = 1; int sum = 0; vecto.原创 2020-09-04 21:30:10 · 91 阅读 · 0 评论 -
剑指offer54.二叉树的第K大节点
1.知识点:二叉树的遍历方式如何写:oid middleSearch(TreeNode* tr,stack<int> &stack){ if(tr) { middleSearch(tr -> left, stack); stack.push(tr -> val); middleSearch(tr -> right, stack); } }.原创 2020-09-04 16:35:34 · 110 阅读 · 0 评论 -
剑指offer55二叉树的深度
使用BFS 与 DFS 两种方法进行计算:DFS:迭代思想,一直走到底,寻找能走的最长路线。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solu.原创 2020-09-04 11:23:21 · 92 阅读 · 0 评论 -
剑指offer28:对称的二叉树(dfs)
dfs走到底,递归检查树的右左,左右子树。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool isS.原创 2020-09-02 21:27:58 · 86 阅读 · 0 评论 -
leetcodeOffer27:二叉树的镜像(看清题目,树的首杀)
使用队列做,保存当前的节点,这个方法我也是最喜欢的浙大何老头教法。一开始没看懂题,以为光输出来就行,题的意思应该是改编整个树的结构,所以要swap左右节点。class Solution {public: TreeNode* mirrorTree(TreeNode* root) { queue<TreeNode*> q; if(root) { q.push(root); } wh.原创 2020-07-31 11:19:11 · 101 阅读 · 0 评论 -
leetcode191:二进制中1的个数(解决二进制的&、|、^、~与左移右移问题)
AC双百思路就是比照最后一位与1的结果有n & 1 =1然后右移,右移就是二进制数整体向右移动,左边补零;>>左移就是二进制数整体向左移动,右边补零注意符号;<<由于定义 n 为 uint32_t 是一个无符号数这里是无符号数,右移的符号>>=class Solution {public: int hammingWeight(uint32_t n) { int count = 0; while(n)原创 2020-07-22 11:13:45 · 191 阅读 · 0 评论 -
leetcode17打印从1到最大的n位数(又写了一个辣鸡算法)
class Solution {public: vector<int> printNumbers(int n) { vector<int> res; if(n == 0) { res.push_back(1); return res; } for(int i = 1;i<pow(10,n);i++) { .原创 2020-07-20 21:49:27 · 90 阅读 · 0 评论 -
leetcode167:两数之和 II - 输入有序数组,不再做辣鸡大王,双指针解法(效果一般)
因为有序排列所以小于目标值时,++小数大于目标值时,–大数class Solution {public: vector<int> twoSum(vector<int>& numbers, int target) { vector<int> loc; int low = 0; int high = numbers.size()-1; while(low < high) { .原创 2020-07-20 10:02:57 · 109 阅读 · 0 评论 -
leetcodeOffer06:从尾到头打印链表(堆栈)
把这个题当翻转链表做可以,然后就贼烂。class Solution {public: vector<int> reversePrint(ListNode* head) { vector<int> res; ListNode* copy = head; if(head == NULL)return res; while(copy -> next) { copy =原创 2020-07-18 15:31:16 · 97 阅读 · 0 评论 -
leetcode21:合并两个排序链表
C++是不是一直内存100%简单方法:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode.原创 2020-07-18 14:36:18 · 115 阅读 · 0 评论 -
leetcodeoffer22:链表中倒数第K个节点
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* getKthFromEnd(ListNode* head, int k) { //采用两个头结点复制为了.原创 2020-07-18 10:07:29 · 99 阅读 · 0 评论 -
leetcodeOffer18:删除链表的节点
84.3%+100% * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* deleteNode(ListNode* head, int val) { //定义俩个头节原创 2020-07-17 23:27:20 · 93 阅读 · 0 评论 -
leetcode509:斐波那契数列
背了的递归不能用哦,当n=43都GG,参照人家咋说就怎么干写法:有class Solution {public: int fib(int n) { if(n==0)return 0; vector<int> nums(n+1); nums[0] = 0; nums[1] = 1; for(int i =2;i<=n;i++ ){ nums[i] = (nums[i-1]+nums[i-原创 2020-07-12 19:16:34 · 119 阅读 · 0 评论 -
leetcodeOffer05:替换空格(字符串处理)辣鸡大王算法
满分。class Solution {public:string replaceSpace(string s) { string ans = ""; for (int i = 0; i < s.length(); i++) if (s[i] == ' ') { ans += "%20"; } else { ans += s[i]; .原创 2020-07-11 20:56:37 · 157 阅读 · 0 评论 -
leetcode1524:二维数组的查找,辣鸡大王算法,竟然效果不错
题目不贴了,leetcode上有class Solution {public: bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) { if(matrix.empty()|| matrix[0].empty())//这句话是抄的,为了针对某种算例 { return false; } for(int i.原创 2020-07-11 20:26:32 · 217 阅读 · 0 评论 -
leetcodeOffer03:数组中的重复数字(standard answer)
原题不贴了有版权class Solution {public: int findRepeatNumber(vector<int>& nums) { sort(nums.begin(),nums.end()); for(int i=0;i<nums.size();i++){ int j=i+1; if(nums[i]==nums[j]){ return nums原创 2020-07-11 15:50:00 · 195 阅读 · 0 评论 -
leetcode1431:拥有最多糖果的孩子辣鸡大王遍历算法一遍过
原题依旧不贴了,很简单的睡前小甜点辣鸡大王遍历做法class Solution {public: vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) { vector<bool> ans; for(int i = 0;i<candies.size();i++){ int count = 0;原创 2020-07-09 23:48:23 · 129 阅读 · 0 评论 -
leetcode206反转链表辣鸡解法三指针蛋疼王
题目不贴了leetcode206/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* reverseList(ListNode* head) { if(原创 2020-07-08 20:12:06 · 109 阅读 · 0 评论 -
leetcode面试16.11跳水板辣鸡解法,就是干
class Solution {public: vector<int> divingBoard(int shorter, int longer, int k) { vector<int> ans; vector<int> nums; for(int i =1;i<=k;i++){ nums.push_back(i); } for(int i =0;i&.原创 2020-07-08 19:27:07 · 148 阅读 · 0 评论 -
leetcode1:两数之和辣鸡算法暴力(解决:为什么有两个return)
class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { vector<int> ans; for(int i = 0;i<nums.size();i++){ for(int j = i+1;j<nums.size();j++){ if(nums[i]+nums[原创 2020-07-06 23:36:26 · 523 阅读 · 2 评论 -
leetcode:160相交链表---辣鸡解法遍历(解决:为什么不能直接用现成的结构体指针进行运算)
题目见leetcode吧,这里不贴了,想明白了答案是要找两个链表地址相同的地方,就当两个地址相同的时候跳出;/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *ge原创 2020-07-06 21:57:45 · 165 阅读 · 0 评论