![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Leetcode
我不转弯ll
这个作者很懒,什么都没留下…
展开
-
Leetcode 91. 解码方法
91. 解码方法 示例 1: 输入:s = “12” 输出:2 解释:它可以解码为 “AB”(1 2)或者 “L”(12)。 示例 2: 输入:s = “226” 输出:3 解释:它可以解码为 “BZ” (2 26), “VF” (22 6), 或者 “BBF” (2 2 6) 。 动态规划题,dp[i]表示前i个数字可以有的编码种类的个数。 考虑前i个数字,第i个数字被编码有两种情况: 1、当它不为0时,这个数字可以单独被编码为一个字母; 2、当它前一个数字不是0,并且这两个数字组合起来不大于26时,可原创 2021-04-22 21:02:26 · 113 阅读 · 1 评论 -
Leetcode 88. 合并两个有序数组
88. 合并两个有序数组 因为nums1数组的大小是m+n,为了不占用额外的内存空间,可以直接在nums1数组上进行操作; 使用两个指针从两个数组的最后遍历,模拟即可 class Solution { public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int p = m - 1, q = n - 1; int cnt = m +原创 2021-04-05 17:21:01 · 101 阅读 · 0 评论 -
Leetcode 781. 森林中的兔子
781. 森林中的兔子 class Solution { public: int numRabbits(vector<int>& answers) { int len = answers.size(); if(len == 0) return 0; else{ int ans = 0; sort(answers.begin(), answers.end());原创 2021-04-04 19:34:41 · 108 阅读 · 0 评论 -
Leetcode 1143. 最长公共子序列
1143. 最长公共子序列 边界条件:dp[i][0] = 0 ,dp[0][j] = 0 转移方程:if text1[i] == text2[j]: dp[i + 1][j + 1] = dp[i][j] + 1; else dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]); class Solution { public: int longestCommonSubsequence(string text1, string text2) {原创 2021-04-03 13:39:42 · 68 阅读 · 0 评论 -
Leetcode 面试题 17.21. 直方图的水量
面试题 17.21. 直方图的水量 每个位置的最大接水量 = max(左边最高单位,左边最高单位)- 自身的高度 class Solution { public: int trap(vector<int>& height) { int len = height.size(); if(len == 0) return 0; vector<int> highl(len, 0), highr(len, 0);原创 2021-04-02 15:32:39 · 74 阅读 · 0 评论 -
Leetcode 1006. 笨阶乘
1006. 笨阶乘 笨阶乘:用栈模拟就好了 class Solution { public: int clumsy(int N) { stack<int> st; st.push(N); N--; int n = 0; while(N){ if(n % 4 == 0){ st.top() = st.top() * N; }原创 2021-04-01 22:18:37 · 59 阅读 · 0 评论 -
Leetcode 74. 搜索二维矩阵
74. 搜索二维矩阵 我能想到的笨方法:用两次二分查找,先二分找到要查找元素应该在的一行,然后在该行二分。 class Solution { public: bool searchMatrix(vector<vector<int>>& matrix, int target) { int rows = matrix.size(); int cols = matrix[0].size(); int l = 0, r = r原创 2021-03-30 11:35:10 · 1181 阅读 · 3 评论 -
Leetcode 190. 颠倒二进制位
190. 颠倒二进制位 第一种方法简单的模拟就可以,注意是二进制,答案要用long防止溢出 class Solution { public: uint32_t reverseBits(uint32_t n) { long ans = 0; for(int i = 0; i < 32; i++){ ans = ans * 2 + n % 2; n = n / 2; } return原创 2021-03-29 11:26:46 · 186 阅读 · 0 评论 -
Leetcode 173. 二叉搜索树迭代器
173. 二叉搜索树迭代器 可以利用栈这一数据结构,通过迭代的方式对二叉树做中序遍历。此时,我们无需预先计算出中序遍历的全部结果,只需要实时维护当前栈的情况即可。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), righ原创 2021-03-28 21:27:24 · 52 阅读 · 0 评论 -
Leetcode 61. 旋转链表
61. 旋转链表 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x),原创 2021-03-27 21:47:49 · 59 阅读 · 1 评论 -
Leetcode 83. 删除排序链表中的重复元素
83. 删除排序链表中的重复元素 今天的题目是昨天题目的简化版,删除链表中的重复元素,但是重复的元素不必全部删除,保留一个就好; 只需要从第一个结点开始判断下一个结点(cur)与当前结点(pre2)是否相等: 相等就删除第一个结点(pre2),pre1结点的next指向第二个结点(cur); 不相等就往后推,头部结点(pre1)等于第一个结点(pre2),第一个结点(pre2)等于第二个结点(cur) while判断是要注意不能仅使用 pre2->next != nullptr, 若初始时空的原创 2021-03-26 10:44:40 · 82 阅读 · 3 评论 -
Leetcode 82. 删除排序链表中的重复元素 II
82. 删除排序链表中的重复元素 II 链表要注意表头虚节点定义 ListNode* dummy = new ListNode(-1) 可以防止意外的错误 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x原创 2021-03-25 14:17:34 · 53 阅读 · 1 评论 -
Leetcode每日一题 2021.3.24
456. 132 模式(单调栈) 主要思路是从后往前去遍历 2 表示当前仅小于最大3的有效数字: 初始时为 INT_MIN无效值 3 则不断用一个单调栈维护,保证是单调递减的 在过程中,一旦发现比 有效2 小的数字,那么肯定有一个3大于2, 那么就存在这样的132模式了 这里每次会更新为更大的数字,这样子1更加容易满足条件 class Solution { public: bool find132pattern(vector<int>& nums) { st原创 2021-03-24 17:18:46 · 85 阅读 · 1 评论 -
Leetcode每日一题 2021.3.23
341. 扁平化嵌套列表迭代器 由于「栈」的先进后出的特性,我们需要逆序在栈里放入各个元素。 处理流程分为两步: 在构造函数中应该初始化,把当前列表的各个元素(不用摊平)逆序放入栈中。 在 hasNext() 方法中,访问(不弹出)栈顶元素,判断是否为 int: 如果是 int 那么说明有下一个元素,返回 true;然后 next() 就会被调用,把栈顶的 int 弹出; 如果是 list 需要把当前列表的各个元素(不用摊平)逆序放入栈中。 如果栈为空,那么说明原始的嵌套列表已经访问结束了,返回 fals原创 2021-03-23 16:24:40 · 169 阅读 · 6 评论 -
Leetcode每日一题 2021.3.22
191. 位1的个数(位运算) n&(n-1) 的计算结果为把n二进制最低位的1变为0 class Solution { public: int hammingWeight(uint32_t n) { int ans = 0; while(n){ n = (n & n - 1); ans++; } return ans; } }; ...原创 2021-03-22 10:30:44 · 72 阅读 · 0 评论 -
Leetcode每日一题 2021.3.21
73. 矩阵置零 class Solution { public: void setZeroes(vector<vector<int>>& matrix) { //矩阵的第一行和第一列设置为标志位,标记这一行一列是否有0 int rows = matrix.size(); int cols = matrix[0].size(); int flag_row = 1, flag_col = 1;原创 2021-03-21 14:43:54 · 59 阅读 · 0 评论 -
Leetcode每日一题 2021.3.20
150. 逆波兰表达式求值 栈 class Solution { public: int evalRPN(vector<string>& tokens) { stack<int> ans; for(int i = 0; i < tokens.size(); i++){ if(tokens[i] == "+"){ int a = ans.top();原创 2021-03-20 20:44:05 · 64 阅读 · 0 评论 -
Leetcode每日一题 2021.3.19
1603. 设计停车系统 难度:简单 class ParkingSystem { public: int a, b, c; ParkingSystem(int big, int medium, int small) { a = big; b = medium; c = small; } bool addCar(int carType) { if(carType == 1){ i原创 2021-03-19 11:14:08 · 76 阅读 · 0 评论 -
Leetcode每日一题 2021.3.18
92. 反转链表 II(链) 数据结构数据结构 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *n原创 2021-03-18 20:23:27 · 102 阅读 · 3 评论 -
Leetcode每日一题 2021.3.17
115. 不同的子序列(原创 2021-03-17 14:41:11 · 73 阅读 · 1 评论 -
Leetconde 每日一题 2021.3.15 - 3.16
2021/3/15 54. 螺旋矩阵(模拟) 按照行列简单的模拟即可 class Solution { public: vector<int> spiralOrder(vector<vector<int>>& matrix) { int rows = matrix.size(); int cols = matrix[0].size(); vector<int> ans(rows*cols);原创 2021-03-15 19:57:37 · 94 阅读 · 1 评论 -
Leetconde 每日一题(3.8-3.14)
2021/3/8 132. 分割回文串 II(dp) 与昨天的题目不同,数据范围不一样,最后需要求得的答案也不同;这里主要还是需要动态规划,而且需要两次 1、参考昨天的题目,首先求出字符串任意位置i到j是否为回文串,做法与上一题相同; 2、为了求最小的分割数,需要一次dp,参考代码 class Solution { public: int minCut(string s) { int n = s.size(); vector<vector<int>原创 2021-03-08 10:32:17 · 142 阅读 · 2 评论 -
Leetcode 每日一题 (3.3-3.7)
2021/3/3 338.比特位计数(dp) 时间复杂度o(n)的做法: 1、当num是2的次幂的时,num二进制中的1的数目就为1,判断用(num & (num-1)) == 0(注意括号),这样的数标记为high;num不是2的次幂时,设前一个high为x,二进制中1的数目就为count(num-x)+1; class Solution { public: vector<int> countBits(int num) { vector<int> c原创 2021-03-03 11:22:37 · 101 阅读 · 0 评论