Skip to content

Commit 93ef5bf

Browse files
Find the Duplicate Number
1 parent b8fc212 commit 93ef5bf

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

23.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
3+
Count Complete Tree Nodes
4+
-------------------------
5+
6+
Given a complete binary tree, count the number of nodes.
7+
8+
Note:
9+
10+
Definition of a complete binary tree from Wikipedia:
11+
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
12+
13+
Example:
14+
15+
Input:
16+
1
17+
/ \
18+
2 3
19+
/ \ /
20+
4 5 6
21+
22+
Output: 6
23+
24+
*/
25+
26+
class Solution {
27+
public:
28+
int countNodes(TreeNode* root) {
29+
if(root == NULL) return 0;
30+
return 1 + countNodes(root->left) + countNodes(root->right);
31+
}
32+
};

24.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
3+
Unique Binary Search Trees
4+
--------------------------
5+
6+
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n?
7+
8+
Example:
9+
10+
Input: 3
11+
Output: 5
12+
Explanation:
13+
Given n = 3, there are a total of 5 unique BST's:
14+
15+
1 3 3 2 1
16+
\ / / / \ \
17+
3 2 1 1 3 2
18+
/ / \ \
19+
2 1 2 3
20+
21+
*/
22+
23+
class Solution {
24+
public:
25+
int rec(int n, map<int,int> &dp){
26+
if(n==0){
27+
dp[0] = 1;
28+
return 1;
29+
}
30+
if(dp.find(n)!=dp.end())
31+
return dp[n];
32+
int sum = 0;
33+
for(int k=1;k<=n;k++)
34+
sum += rec(n-k,dp)*rec(k-1,dp);
35+
dp[n] = sum;
36+
return sum;
37+
}
38+
int numTrees(int n) {
39+
map<int,int> dp;
40+
return rec(n,dp);
41+
}
42+
};

25.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
3+
Find the Duplicate Number
4+
-------------------------
5+
6+
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
7+
8+
Example 1:
9+
10+
Input: [1,3,4,2,2]
11+
Output: 2
12+
Example 2:
13+
14+
Input: [3,1,3,4,2]
15+
Output: 3
16+
Note:
17+
18+
You must not modify the array (assume the array is read only).
19+
You must use only constant, O(1) extra space.
20+
Your runtime complexity should be less than O(n2).
21+
There is only one duplicate number in the array, but it could be repeated more than once.
22+
23+
*/
24+
25+
class Solution {
26+
public:
27+
int findDuplicate(vector<int>& nums) {
28+
sort(nums.begin(), nums.end());
29+
for (int i = 1; i < nums.size(); i++) {
30+
if (nums[i] == nums[i-1]) {
31+
return nums[i];
32+
}
33+
}
34+
return -1;
35+
}
36+
};

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@
2323
| 19 | Longest Duplicate Substring | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/19.cpp) |
2424
| 20 | Permutation Sequence | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/20.cpp) |
2525
| 21 | Dungeon Game | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/21.cpp) |
26-
| 22 | Single Number II | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/22.cpp) |
26+
| 22 | Single Number II | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/22.cpp) |
27+
| 23 | Count Complete Tree Nodes | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/23.cpp) |
28+
| 24 | Unique Binary Search Trees | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/24.cpp) |
29+
| 25 | Find the Duplicate Number | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/25.cpp) |

0 commit comments

Comments
 (0)