- 博客(19)
- 收藏
- 关注
翻译 最小栈
题目class MinStack {public:MinStack() { }void push(int x) { m_stkValue.push(x);if (m_stkMinVal.empty() || x < m_stkMinVal.top())m_stkMinVal.push(x); else m_stkMinVal.push(m_stkMinVal.top...
2019-09-30 23:09:43
138
翻译 排序链表
题目思路这个方法总共分为三步:遍历链表将值获取到数组中 O(n) S(n)2. 排序数组 O(nlogn) (快排)3. 将数组元素按顺序写回链表 O(n)题解class Solution {public: void quickSort(vector<int> &arr, int front, int tail) { if (front < tail...
2019-09-29 10:26:19
97
翻译 LRU缓存机制
题目思路假设链表头结点到入环点距离为a,则在快慢指针相遇后,慢指针再走a步到达入环点,这里使用一个临时指针从开始节点走,当临时指针和慢指针相遇的时候,即为入环点。题解class Solution {public: ListNode *detectCycle(ListNode *head) {ListNode * p_slow = head, * p_fast = head, ...
2019-09-27 23:14:15
117
翻译 环形链表
题目思路题解class Solution {public: int maxProfit(vector<int>& prices) { if(prices.size()<2) return 0; int i=0; int maxpro=0; int peak=prices[0]; int valley=prices...
2019-09-23 22:18:45
109
翻译 买卖股票的最佳时机
题目题解int maxProfit(int* prices, int pricesSize){ int maxprofit = 0; int profit = 0; int i,j; for (i = 0; i < pricesSize - 1; i++) { for (j = i + 1; j < pricesSize; j++) { profit =...
2019-09-22 08:19:57
76
翻译 二叉树的最大深度
题目题解class Solution {public: int maxDepth(TreeNode* root) { int maxdepth=0; if(root==NULL) { return 0; } else {maxdepth=max(maxDepth(root->left),maxDe...
2019-09-21 22:12:34
157
翻译 格雷编码
题解class Solution {public: vector<int> grayCode(int n) {vector<int> ret = { 0 }; unsigned int mask = 1; for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) { for...
2019-09-20 23:44:53
85
翻译 合并俩个有序链表
一、题目二、思路遍历l2,插入l1对应的位置。要为l1创建一个虚拟头结点,这样如果l2的最小值小于l1的最小值就可以头插了。l1链表 : l1 在左 , p1 在右 , l1从虚拟头结点开始, p1紧跟其后。l2链表 : l2 在左 , p2 在右 , l2从第一个结点开始, p2紧跟其后。每次比较 l2所指结点的值 与 p1所指结点的值 , 若满足l2 <= p1 , 则把l...
2019-09-19 22:52:51
82
翻译 子集
一、题目二、思路解析定义一个“终点”。举例:N-1为11111111111,意味着所有元素都已经被记录在该二进制数上了二维数组存储每一个子比如说:i从00000遍历到11111. i的每一个状态都意味着一种子集。遍历意味着得到所有子集存储当前i代表着的子集,j的数值为0,1,2,3···以此类推。j意味着nums数组中的第j位元素"1<<j"意味着1,10,100···通...
2019-09-18 22:31:12
166
翻译 爬梯子
一、题目二、思路假如有n阶台阶需要你进行攀爬,每次有且仅能爬1或2个台阶,求有多少种方法。这个题目可以认为是在p次执行步骤里,有k次选择爬2个台阶,然后求有多少种组合方式,是一个排列组合问题。因此我们需要先确定执行步骤的上下限,即p的最大值和最小值,以及两种最值情况下,1和2的数量。然后我们发现,每当选择爬2个台阶的步骤被替换为爬1个台阶后,p值会发生变化,即k–,p++;然后依托排列组合公...
2019-09-17 21:29:34
265
翻译 不同路径
一、题目二、思路从起点 (x=0,y=0)(x=0,y=0)(x=0,y=0) 出发,下一步只能向右或者向下到达第二点,向右则为 (x+1,y)(x+1,y)(x+1,y) 向下则为 (x,y+1)(x,y+1)(x,y+1),一直到 (x=m,y=n)(x=m,y=n)(x=m,y=n) 这个点则为结束点视为一条路径。因此从起点到终点的所有路径总数则为 222 个 以第二个点到终点的路径数...
2019-09-16 22:36:03
91
翻译 旋转列表
一、题目二、分析(这道题对我来说有点困难,借鉴了已知题解的内容)取得链表长度len,让它成环(即tail -> next = head),向右移动k步相当于head顺着指针路线走len-k步,然后向右移动len-1步找到tail节点,让他指向nullptr。三、解析class Solution {public: ListNode* rotateRight(ListNode* h...
2019-09-15 23:06:36
126
翻译 day13
class Solution {public:struct cmp{bool operator()(ListNode* l1, ListNode* l2){return l1->val > l2->val;}}; ListNode* mergeKLists(vector<ListNode*>& lists){ ListNode* ph...
2019-09-07 22:22:17
122
翻译 day12
class Solution {public:ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {if(l1 == NULL) return l2; else if (l2 == NULL) return l1;else if (l1->val < l2->val) {l1->next = mergeTwo...
2019-09-06 22:20:36
79
翻译 day11
class Solution {public: bool isValid(string s) { if (s.empty()) return true;stack<char> sk; unordered_map<char, char> m = {{')', '('}, {']', '['}, {'}', '{'}};for (char& c : s...
2019-09-05 22:34:54
99
翻译 day10
class Solution {public:int threeSumClosest(vector<int>& nums, int target) { sort(nums.begin(),nums.end());if(nums.size()<3) return {}; int closest=target+10000000; for(int i=0;i<...
2019-09-04 22:53:53
77
翻译 三数相加
vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> res; if(nums.size() < 2) return res; sort(nums.begin(), nums.end()); int ...
2019-09-03 22:33:01
229
翻译 day5
int temp;long y=0;int start=x;if(x<0){return false;}else{while(x!=0){temp=x%10;x=x/10;y=temp+y*10;}if(start==y){return true;}return false;}
2019-08-31 22:19:34
102
翻译 俩数相加
{ int len1=1; int len2=1; ListNode* p=l1; ListNode* q=l2; while(p->next!=NULL) { len1++; p=p->next; } w...
2019-08-26 22:28:07
180
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人