自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(12)
  • 收藏
  • 关注

原创 Leetcode 142. 环形链表 II

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *detectCycle(ListNode *head) { ListNode* ptr = head

2021-03-19 14:10:30 54

原创 Leetcode 20. 有效的括号

解题思路开个栈按照规则模拟即可class Solution {public: stack <int> s1; bool isValid(string s) { int len = s.size(); for(int i = 0; i < len; i++) { if(s[i] == '(') s1.push(1); else if(s[i] == '[') s1.push(2);

2021-03-15 23:41:40 47

原创 Leetcode 22. 括号生成

解题思路每个位置可以放左括号或者右括号所有直接用dfs搜索所有情况,并且限制左右括号的规则情况class Solution {public: vector<string> ans; string s = ""; int k; void dfs(int x, int left, int right) { if(x == k + 1) { ans.push_back(s); return;

2021-03-15 23:31:37 45

原创 Leetcode 338. 比特位计数

解题思路ans[i] 代表i中二进制为1的数对于一个数x, 我们要推出x + 1, 当我们将x + 1时,我们可能会产生二进制的进位,但是我们并不知道他会改变多少个1, 所以将x & (x + 1) 与起来就会求出我们有多少个1进位被改变了, bit[x & (x + 1)] 就代表了我们有多少个1是没改变的,因为最后一位一定要加上, 所以答案为bit[x & (x + 1)] + 1.class Solution {public: vector<int>

2021-03-14 22:34:46 51

原创 Leetcode 78. 子集

解题思路:迭代法实现子集枚举class Solution {public: vector<int> sum; vector<vector<int>> ans; int n; void dfs(int x, vector<int> nums) { if(x == n) { ans.push_back(sum); return; }

2021-03-14 22:06:31 33

原创 Leetcode 136. 只出现一次的数字

解题思路根据异或的性质,a ^ a = 0,将所有数异或起来即为答案class Solution {public: int singleNumber(vector<int>& nums) { int ans = 0; for(int i = 0; i < nums.size(); i++) { ans = (ans ^ nums[i]); } return ans; }}

2021-03-14 21:54:37 57

原创 Leetcode 206. 反转链表

解题思路在遍历链表时,将当前节点的 \textit{next}next 指针改为指向前一个节点。class Solution {public: ListNode* reverseList(ListNode* head) { ListNode* ans = nullptr; while(head) { ListNode* node = head->next; head->next = ans;

2021-03-14 21:50:25 42

原创 Leetcode 104. 二叉树的最大深度

解题思路dfs搜一遍,记录最大的长度即可class Solution {public: int ans = 0; void dfs(TreeNode* root, int sum) { if(!root) return; ans = max(ans, sum); dfs(root->left, sum + 1); dfs(root->right, sum + 1); }

2021-03-14 21:23:39 44

原创 Leetcode 226. 翻转二叉树

解题思路遍历二叉树,将左右结点交换class Solution {public: void dfs(TreeNode* root) { TreeNode* p = new TreeNode(0); p = root->left; root->left = root->right; root->right = p; if(root->left) dfs(root-

2021-03-14 21:22:23 53

原创 Leetcode 617. 合并二叉树

解题思路递归处理每一个结点,具体实现看代码class Solution {public: TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) { if(!root1) return root2; if(!root2) return root1; TreeNode* node = new TreeNode(root1->val + root2->val); n

2021-03-14 21:20:28 41

原创 Leetcode 461.汉明距离

解题思路利用异或的性质,x ^ y 中为1的位就是二进制中不同的位class Solution {public: int hammingDistance(int x, int y) { long long k = (x ^ y); int ans = 0; for(int i = 0; i < 32; i++) { if(k & (1 << i)) { ans++;

2021-03-14 21:15:17 44

原创 leetcode 146.LRU缓存机制(hash + 双向链表)

leetcode 146.LRU缓存机制解题思路这道题我们需要用hash加双向链表的方法来实现。1.首先我们要定义一个双向链表的数据结构struct doubleList { int key, value; doubleList* next; doubleList* prev; doubleList(): key(0), value(0), next(nullptr), prev(nullptr) {} doubleList(int _key, int _va

2021-03-14 21:09:27 109

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除