8000 construct bst · qinyafee/leetcode@a2410dc · GitHub
[go: up one dir, main page]

Skip to content

Commit a2410dc

Browse files
committed
construct bst
Signed-off-by: Yafei <yafee.qin@gmail.com>
1 parent 0a45da3 commit a2410dc

File tree

3 files changed

+61
-35
lines changed

3 files changed

+61
-35
lines changed

algorithms/cpp/constructBinaryTreeFromPreorderAndInorderTraversal/constructBinaryTreeFromPreorderAndInorderTraversal.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// 递归
22
// 保证元素无重复
33
// 前序遍历中的第一个节点就是根节点
4+
// 无论前序/中序,左/右子树节点相等
45
//链接:https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/solution/cong-qian-xu-yu-zhong-xu-bian-li-xu-lie-gou-zao-9/
56
// time O(n), space O(n)
67
class Solution {

algorithms/cpp/removeDuplicatesFromSortedArray/removeDuplicatesFromSortedArray.cpp

Lines changed: 60 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,71 @@
22
// Author : Hao Chen
33
// Date : 2014-06-22
44

5-
/**********************************************************************************
6-
*
7-
* Given a sorted array, remove the duplicates in place such that each element appear
8-
* only once and return the new length.
9-
*
10-
* Do not allocate extra space for another array, you must do this in place with constant memory.
11-
*
12-
* For example,
13-
* Given input array A = [1,1,2],
14-
*
15-
* Your function should return length = 2, and A is now [1,2].
16-
*
17-
*
18-
**********************************************************************************/
5+
/**********************************************************************************
6+
*
7+
* Given a sorted array, remove the duplicates in place such that each element appear
8+
* only once and return the new length.
9+
*
10+
* Do not allocate extra space for another array, you must do this in place with constant memory.
11+
*
12+
* For example,
13+
* Given input array A = [1,1,2],
14+
*
15+
* Your function should return length = 2, and A is now [1,2].
16+
*
17+
*
18+
**********************************************************************************/
1919
// my implm
20+
// 双指针
2021
class Solution {
21-
public:
22-
int removeDuplicates(vector<int>& nums) {
23-
if(nums.empty()) return 0;
24-
int i = 0; //输出结果index
25-
for(int j = 1; j < nums.size(); ++j){
26-
if(nums[i] == nums[j]) continue;
27-
else nums[++i] = nums[j];
28-
}
29-
return i+1;
22+
public:
23+
int removeDuplicates(vector<int>& nums) {
24+
if (nums.empty()) {
25+
return 0;
3026
}
27+
int i = 0; //输出结果index
28+
for (int j = 1; j < nums.size(); ++j) {
29+
if (nums[i] == nums[j]) {
30+
continue;
31+
} else {
32+
nums[++i] = nums[j];
33+
}
34+
}
35+
return i + 1;
36+
}
3137
};
3238

39+
// 变种:如果数组未排序,输出结果数组。[1,3,4,2,2,2,1,5] ->[1, 3, 4, 2, 5]
40+
// 哈希+双指针
41+
void removeDuplicates(vector<int>& nums) {
42+
unordered_set<int> st;
43+
44+
int i = 0;
45+
for (int j = 0; j < nums.size(); ++j) {
46+
if (st.find(nums[j]) != st.end()) {
47+
continue;
48+
} else {
49+
st.insert(nums[j]);
50+
nums[i++] = nums[j];
51+
}
52+
}
53+
nums.erase(nums.begin() + i, nums.end());
54+
// for (const auto &x : nums) {
55+
// cout << x << ", ";
56+
// }
57+
}
58+
3359
class Solution {
34-
public:
35-
int removeDuplicates(int A[], int n) {
36-
37-
if (n<=1) return n;
38-
39-
int pos=0;
40-
for(int i=0; i<n-1; i++){
41-
if (A[i]!=A[i+1]){
42-
A[++pos] = A[i+1];
43-
}
44-
}
45-
return pos+1;
60+
public:
61+
int removeDuplicates(int A[], int n) {
62+
if (n <= 1) return n;
63+
64+
int pos = 0;
65+
for (int i = 0; i < n - 1; i++) {
66+
if (A[i] != A[i + 1]) {
67+
A[++pos] = A[i + 1];
68+
}
4669
}
70+
return pos + 1;
71+
}
4772
};

0 commit comments

Comments
 (0)
0