Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 192406b

Browse files
leetcode
1 parent 4ca52cd commit 192406b

File tree

4 files changed

+388
-0
lines changed

4 files changed

+388
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* @lc app=leetcode.cn id=109 lang=cpp
3+
*
4+
* [109] 有序链表转换二叉搜索树
5+
*
6+
* https://leetcode.cn/problems/convert-sorted-list-to-binary-search-tree/description/
7+
*
8+
* algorithms
9+
* Medium (76.46%)
10+
* Likes: 826
11+
* Dislikes: 0
12+
* Total Accepted: 145K
13+
* Total Submissions: 189.6K
14+
* Testcase Example: '[-10,-3,0,5,9]'
15+
*
16+
* 给定一个单链表的头节点 head ,其中的元素 按升序排序
17+
* ,将其转换为高度平衡的二叉搜索树。
18+
*
19+
* 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差不超过
20+
* 1。
21+
*
22+
*
23+
*
24+
* 示例 1:
25+
*
26+
*
27+
*
28+
*
29+
* 输入: head = [-10,-3,0,5,9]
30+
* 输出: [0,-3,9,-10,null,5]
31+
* 解释:
32+
* 一个可能的答案是[0,-3,9,-10,null,5],它表示所示的高度平衡的二叉搜索树。
33+
*
34+
*
35+
* 示例 2:
36+
*
37+
*
38+
* 输入: head = []
39+
* 输出: []
40+
*
41+
*
42+
*
43+
*
44+
* 提示:
45+
*
46+
*
47+
* head 中的节点数在[0, 2 * 10^4] 范围内
48+
* -10^5 <= Node.val <= 10^5
49+
*
50+
*
51+
*/
52+
53+
// Definition for singly-linked list.
54+
struct ListNode {
55+
int val;
56+
ListNode *next;
57+
ListNode() : val(0), next(nullptr) {}
58+
ListNode(int x) : val(x), next(nullptr) {}
59+
ListNode(int x, ListNode *next) : val(x), next(next) {}
60+
};
61+
62+
// Definition for a binary tree node.
63+
struct TreeNode {
64+
int val;
65+
TreeNode *left;
66+
TreeNode *right;
67+
TreeNode() : val(0), left(nullptr), right(nullptr) {}
68+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
69+
TreeNode(int x, TreeNode *left, TreeNode *right)
70+
: val(x), left(left), right(right) {}
71+
};
72+
73+
// @lc code=start
74+
/**
75+
* Definition for singly-linked list.
76+
* struct ListNode {
77+
* int val;
78+
* ListNode *next;
79+
* ListNode() : val(0), next(nullptr) {}
80+
* ListNode(int x) : val(x), next(nullptr) {}
81+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
82+
* };
83+
*/
84+
/**
85+
* Definition for a binary tree node.
86+
* struct TreeNode {
87+
* int val;
88+
* TreeNode *left;
89+
* TreeNode *right;
90+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
91+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
92+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
93+
* right(right) {}
94+
* };
95+
*/
96+
class Solution {
97+
public:
98+
// 二叉树中序遍历刚好就是链表,也就是从中序遍历结果反推二叉树
99+
// 可以先构建root节点,待到中序遍历到该位置再填入
100+
TreeNode *sortedListToBST(ListNode *head) {
101+
return build(head, 0, len(head)-1);
102+
}
103+
104+
private:
105+
int len(ListNode *head) {
106+
int ret = 0;
107+
while (head != nullptr) {
108+
head = head->next;
109+
ret++;
110+
}
111+
return ret;
112+
}
113+
// head 是指针引用,为了在内部更新head
114+
TreeNode *build(ListNode *&head, int l, int r) {
115+
if (l > r) {
116+
return nullptr;
117+
}
118+
int mid = (l + r + 1) / 2;
119+
TreeNode *root = new TreeNode();
120+
root->left = build(head, l, mid - 1);
121+
root->val = head->val;
122+
head = head->next;
123+
root->right = build(head, mid + 1, r);
124+
return root;
125+
}
126+
};
127+
// @lc code=end
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* @lc app=leetcode.cn id=152 lang=cpp
3+
*
4+
* [152] 乘积最大子数组
5+
*
6+
* https://leetcode.cn/problems/maximum-product-subarray/description/
7+
*
8+
* algorithms
9+
* Medium (43.11%)
10+
* Likes: 2012
11+
* Dislikes: 0
12+
* Total Accepted: 356.5K
13+
* Total Submissions: 826.9K
14+
* Testcase Example: '[2,3,-2,4]'
15+
*
16+
* 给你一个整数数组
17+
* nums ,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。
18+
*
19+
* 测试用例的答案是一个 32-位 整数。
20+
*
21+
* 子数组 是数组的连续子序列。
22+
*
23+
*
24+
*
25+
* 示例 1:
26+
*
27+
*
28+
* 输入: nums = [2,3,-2,4]
29+
* 输出: 6
30+
* 解释: 子数组 [2,3] 有最大乘积 6。
31+
*
32+
*
33+
* 示例 2:
34+
*
35+
*
36+
* 输入: nums = [-2,0,-1]
37+
* 输出: 0
38+
* 解释: 结果不能为 2, 因为 [-2,-1] 不是子数组。
39+
*
40+
*
41+
*
42+
* 提示:
43+
*
44+
*
45+
* 1 <= nums.length <= 2 * 10^4
46+
* -10 <= nums[i] <= 10
47+
* nums 的任何前缀或后缀的乘积都 保证 是一个 32-位 整数
48+
*
49+
*
50+
*/
51+
#include <algorithm>
52+
#include <vector>
53+
// @lc code=start
54+
class Solution {
55+
public:
56+
// 与最大和类似,区别是负数后面还可以再乘负数得到正数,而正数也可以乘负数得到正数,而最大和最需要关注正数即可。
57+
// 因此可以用两个变量分别存正数最大值和负数最小值,每次循环判断当前值去更新这两个值,同时更新全局最大值
58+
int maxProduct(std::vector<int> &nums) {
59+
if (nums.empty()) {
60+
return 0;
61+
}
62+
int res = nums[0], maximum = nums[0], minimum = nums[0];
63+
for (size_t i = 1; i < nums.size(); i++) {
64+
if (nums[i] > 0) {
65+
maximum = std::max(maximum * nums[i], nums[i]);
66+
minimum = std::min(minimum * nums[i], nums[i]);
67+
} else {
68+
int tmp = maximum;
69+
maximum = std::max(minimum * nums[i], nums[i]);
70+
minimum = std::min(tmp * nums[i], nums[i]);
71+
}
72+
res = std::max(maximum, res);
73+
}
74+
return res;
75+
}
76+
};
77+
// @lc code=end

‎cpp/leetcode/155.最小栈.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* @lc app=leetcode.cn id=155 lang=cpp
3+
*
4+
* [155] 最小栈
5+
*
6+
* https://leetcode.cn/problems/min-stack/description/
7+
*
8+
* algorithms
9+
* Medium (58.94%)
10+
* Likes: 1566
11+
* Dislikes: 0
12+
* Total Accepted: 477.5K
13+
* Total Submissions: 810K
14+
* Testcase Example:
15+
* '["MinStack","push","push","push","getMin","pop","top","getMin"]\n[[],[-2],[0],[-3],[],[],[],[]]'
16+
*
17+
* 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。
18+
*
19+
* 实现 MinStack 类:
20+
*
21+
*
22+
* MinStack() 初始化堆栈对象。
23+
* void push(int val) 将元素val推入堆栈。
24+
* void pop() 删除堆栈顶部的元素。
25+
* int top() 获取堆栈顶部的元素。
26+
* int getMin() 获取堆栈中的最小元素。
27+
*
28+
*
29+
*
30+
*
31+
* 示例 1:
32+
*
33+
*
34+
* 输入:
35+
* ["MinStack","push","push","push","getMin","pop","top","getMin"]
36+
* [[],[-2],[0],[-3],[],[],[],[]]
37+
*
38+
* 输出:
39+
* [null,null,null,null,-3,null,0,-2]
40+
*
41+
* 解释:
42+
* MinStack minStack = new MinStack();
43+
* minStack.push(-2);
44+
* minStack.push(0);
45+
* minStack.push(-3);
46+
* minStack.getMin(); --> 返回 -3.
47+
* minStack.pop();
48+
* minStack.top(); --> 返回 0.
49+
* minStack.getMin(); --> 返回 -2.
50+
*
51+
*
52+
*
53+
*
54+
* 提示:
55+
*
56+
*
57+
* -2^31 <= val <= 2^31 - 1
58+
* pop、top 和 getMin 操作总是在 非空栈 上调用
59+
* push, pop, top, and getMin最多被调用 3 * 10^4 次
60+
*
61+
*
62+
*/
63+
#include <stack>
64+
65+
// @lc code=start
66+
class MinStack {
67+
public:
68+
MinStack() : data_(), min_() {}
69+
70+
void push(int val) {
71+
data_.push(val);
72+
if (min_.empty() || val <= min_.top()) {
73+
min_.push(val);
74+
}
75+
}
76+
77+
void pop() {
78+
if (!min_.empty() && min_.top() == data_.top()) {
79+
min_.pop();
80+
}
81+
data_.pop();
82+
}
83+
84+
int top() { return data_.top(); }
85+
86+
int getMin() {
87+
if (min_.empty()) {
88+
return -1;
89+
}
90+
return min_.top();
91+
}
92+
93+
private:
94+
std::stack<int> data_;
95+
std::stack<int> min_; // 遇到更小的进栈,出栈判断是否将最小值出栈
96+
};
97+
98+
/**
99+
* Your MinStack object will be instantiated and called as such:
100+
* MinStack* obj = new MinStack();
101+
* obj->push(val);
102+
* obj->pop();
103+
* int param_3 = obj->top();
104+
* int param_4 = obj->getMin();
105+
*/
106+
// @lc code=end

‎cpp/leetcode/198.打家劫舍.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* @lc app=leetcode.cn id=198 lang=cpp
3+
*
4+
* [198] 打家劫舍
5+
*
6+
* https://leetcode.cn/problems/house-robber/description/
7+
*
8+
* algorithms
9+
* Medium (54.31%)
10+
* Likes: 2560
11+
* Dislikes: 0
12+
* Total Accepted: 742.9K
13+
* Total Submissions: 1.4M
14+
* Testcase Example: '[1,2,3,1]'
15+
*
16+
*
17+
* 你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。
18+
*
19+
* 给定一个代表每个房屋存放金额的非负整数数组,计算你 不触动警报装置的情况下
20+
* ,一夜之内能够偷窃到的最高金额。
21+
*
22+
*
23+
*
24+
* 示例 1:
25+
*
26+
*
27+
* 输入:[1,2,3,1]
28+
* 输出:4
29+
* 解释:偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。
30+
* 偷窃到的最高金额 = 1 + 3 = 4 。
31+
*
32+
* 示例 2:
33+
*
34+
*
35+
* 输入:[2,7,9,3,1]
36+
* 输出:12
37+
* 解释:偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋
38+
* (金额 = 1)。 偷窃到的最高金额 = 2 + 9 + 1 = 12 。
39+
*
40+
*
41+
*
42+
*
43+
* 提示:
44+
*
45+
*
46+
* 1
47+
* 0
48+
*
49+
*
50+
*/
51+
52+
#include <vector>
53+
54+
// @lc code=start
55+
class Solution {
56+
public:
57+
// 这种dp题目,一般是分析某个位置要或丢两种选择怎么选,输出结果有两种模式:
58+
// 一种是维护一个全局最大值,每次循环比较更新;
59+
// 另外一种是直接求dp[n]或者dp[0]的值
60+
// dp[i]标识i个数字内的最大值,因为数字都是正数,只要不触发报警机制都可以要,那要第i个数的前提是第i-1个没选
61+
// 因此 dp[i] = max(dp[i-2]+nums[i], dp[i-1])
62+
int rob(std::vector<int> &nums) {
63+
if (nums.empty()) {
64+
return 0;
65+
}
66+
if (nums.size() < 2) {
67+
return nums[0];
68+
}
69+
std::vector<int> dp(nums.size());
70+
dp[0] = nums[0];
71+
dp[1] = std::max(nums[0], nums[1]);
72+
for (size_t i = 2; i < nums.size(); i++) {
73+
dp[i] = std::max(dp[i - 2] + nums[i], dp[i - 1]);
74+
}
75+
return dp[nums.size() - 1];
76+
}
77+
};
78+
// @lc code=end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /