- 博客(16)
- 收藏
- 关注
原创 <leetcode>402.移掉k位数字
class Solution { public: string removeKdigits(string num, int k) { if(k == num.size()) return "0"; string ans; while(k--) { num = removeone(num); n...
2020-03-09 15:51:31
97
原创 <leetcode>105. 从前序与中序遍历序列构造二叉树
代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */...
2020-03-07 01:29:55
93
原创 <leetcode>10.正则表达式匹配
代码: class Solution { public: bool isMatch(string s, string p) { vector<vector<bool>> dp (s.size()+1, vector<bool>(p.size()+1, false)); dp[0][0] = true; f...
2020-03-07 00:12:09
77
原创 <leetcode>328.奇偶链表
思路1: 1.链表拆分&合并 2.每次同时处理奇偶节点,需要判断边界问题,比较复杂 3.末尾处要设置成指向空 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(...
2020-03-05 09:43:09
109
1
原创 <leetcode>143.重排链表(递归)
利用递归来解决该问题,一层一层处理 代码 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { pu...
2020-03-04 23:15:47
177
原创 <leetcode>24.两两交换链表中的节点
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* s...
2020-03-04 16:44:21
61
原创 <leetcode>21.合并两个有序链表
无比丑陋的代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: Li...
2020-03-04 12:47:13
72
原创 <leetcode>2. 两数相加(链表)
原方法: class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { auto l3 = new ListNode(-1); auto res = l3; int jinwei = 0; while(l1 && l...
2020-03-04 11:47:33
63
原创 <leetcode>234.反转链表
比较简洁高效的程序: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: L...
2020-03-04 10:48:57
145
原创 <leetcode>234.回文链表
代码: class Solution { public: bool isPalindrome(ListNode* head) { if(!head || !head->next) return true; ListNode* slow = head; ListNode* fast = head; while(fast ...
2020-03-03 20:04:32
78
原创 <leetcode>15.三数之和
解答思路如下: 代码如下: class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> ans; if(nums.size() < 3)//如果长度不足...
2020-02-25 19:07:43
65
转载 <leetcode>264.丑数Ⅱ
求解第n个丑数a[n],前面肯定存在三个最小的下标i,j,k属于(1,2,…,n-1)使得 a[i]*2,a[j]*3,a[k]*5刚好大于a[n-1],然后选取三者中最小的即为a[n] 下面看看这三个下标,它满足两个条件:1.乘以对应的因子一定大于a[n-1];2.满足1的同时又是最小的。 从头开始,a[1]=1,毫无疑问,i=j=k=1。显然,这时肯定选i,a[2]=2。现在2已经在数组a里了...
2020-02-25 11:19:34
67
原创 <leetcode>983.最低票价
class Solution { public: int mincostTickets(vector<int>& days, vector<int>& costs) { vector<int> dp(366, 0); int k = 0;//访问days[] int tar;储存一年中最后...
2020-02-24 23:13:20
96
转载 <leetcode>一些常用模板
// 快速排序算法模板 void quick_sort(int q[], int l, int r) { if (l >= r) return; int i = l - 1, j = r + 1, x = q[l]; while (i < j) { do i ++ ; while (q[i] < x); do j ...
2020-02-24 18:00:44
187
原创 <leetcode>刷题中的一些小技巧
一些小技巧: 整型数据的上下限可以写成INT_MAX和INT_MIN cout<<INT_MIN;//可以输出最小整型数据。 利用memset可以实现0或false初始化 memset(dp, 0, sizeof(dp));//这里的dp可以是二维数组 二维vector的初始化 vector<vector<int>> dp(m, vector&l...
2020-02-22 21:39:18
146
原创 <leetcode>216.组合总数3
题目描述: 由于循环层数是不确定的,考虑递归的方法来做,每一层都更新余下数字和、余下数字个数、起始数字,都要保留两个储存结果的数组。 递归中利用回溯的思想,每次push_back后必须跟随pop_back,如果其他数字发生改变,也需要在递归调用后改回来。 由于递归的终点是输出一个结果,不需要判断真假,函数返回值void即可。 代码 class Solution { public: ...
2020-02-22 14:18:41
99
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人