Skip to content

Commit 5d1baf2

Browse files
committed
Added tasks 222-383
1 parent ffa288e commit 5d1baf2

File tree

11 files changed

+725
-0
lines changed

11 files changed

+725
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 222\. Count Complete Tree Nodes
5+
6+
Medium
7+
8+
Given the `root` of a **complete** binary tree, return the number of the nodes in the tree.
9+
10+
According to **[Wikipedia](http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees)**, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between `1` and <code>2<sup>h</sup></code> nodes inclusive at the last level `h`.
11+
12+
Design an algorithm that runs in less than `O(n)` time complexity.
13+
14+
**Example 1:**
15+
16+
![](https://assets.leetcode.com/uploads/2021/01/14/complete.jpg)
17+
18+
**Input:** root = [1,2,3,4,5,6]
19+
20+
**Output:** 6
21+
22+
**Example 2:**
23+
24+
**Input:** root = []
25+
26+
**Output:** 0
27+
28+
**Example 3:**
29+
30+
**Input:** root = [1]
31+
32+
**Output:** 1
33+
34+
**Constraints:**
35+
36+
* The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.
37+
* <code>0 <= Node.val <= 5 * 10<sup>4</sup></code>
38+
* The tree is guaranteed to be **complete**.
39+
40+
## Solution
41+
42+
```csharp
43+
using LeetCodeNet.Com_github_leetcode;
44+
45+
/**
46+
* Definition for a binary tree node.
47+
* public class TreeNode {
48+
* public int val;
49+
* public TreeNode left;
50+
* public TreeNode right;
51+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
52+
* this.val = val;
53+
* this.left = left;
54+
* this.right = right;
55+
* }
56+
* }
57+
*/
58+
public class Solution {
59+
public int CountNodes(TreeNode root) {
60+
if (root == null) {
61+
return 0;
62+
}
63+
int leftHeight = LeftHeight(root);
64+
int rightHeight = RightHeight(root);
65+
if (leftHeight == rightHeight) {
66+
return (1 << leftHeight) - 1;
67+
} else {
68+
return 1 + CountNodes(root.left) + CountNodes(root.right);
69+
}
70+
}
71+
72+
private int LeftHeight(TreeNode root) {
73+
if (root == null) {
74+
return 0;
75+
}
76+
return 1 + LeftHeight(root.left);
77+
}
78+
79+
private int RightHeight(TreeNode root) {
80+
if (root == null) {
81+
return 0;
82+
}
83+
return 1 + RightHeight(root.right);
84+
}
85+
}
86+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 224\. Basic Calculator
5+
6+
Hard
7+
8+
Given a string `s` representing a valid expression, implement a basic calculator to evaluate it, and return _the result of the evaluation_.
9+
10+
**Note:** You are **not** allowed to use any built-in function which evaluates strings as mathematical expressions, such as `eval()`.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "1 + 1"
15+
16+
**Output:** 2
17+
18+
**Example 2:**
19+
20+
**Input:** s = " 2-1 + 2 "
21+
22+
**Output:** 3
23+
24+
**Example 3:**
25+
26+
**Input:** s = "(1+(4+5+2)-3)+(6+8)"
27+
28+
**Output:** 23
29+
30+
**Constraints:**
31+
32+
* <code>1 <= s.length <= 3 * 10<sup>5</sup></code>
33+
* `s` consists of digits, `'+'`, `'-'`, `'('`, `')'`, and `' '`.
34+
* `s` represents a valid expression.
35+
* `'+'` is **not** used as a unary operation (i.e., `"+1"` and `"+(2 + 3)"` is invalid).
36+
* `'-'` could be used as a unary operation (i.e., `"-1"` and `"-(2 + 3)"` is valid).
37+
* There will be no two consecutive operators in the input.
38+
* Every number and running calculation will fit in a signed 32-bit integer.
39+
40+
## Solution
41+
42+
```csharp
43+
public class Solution {
44+
public int Calculate(string s) {
45+
Stack<int> st = new Stack<int>();
46+
int ans = 0;
47+
int sum = 0;
48+
int sign = 1;
49+
st.Push(1);
50+
foreach (char ch in s) {
51+
if (ch >= '0' && ch <= '9') {
52+
sum = sum * 10 + ch - '0';
53+
} else if (ch == '(') {
54+
st.Push(sign);
55+
} else if (ch == ')') {
56+
st.Pop();
57+
} else if (ch == '-' || ch == '+') {
58+
ans += sum * sign;
59+
if (ch == '-') {
60+
sign = -1 * st.Peek();
61+
} else {
62+
sign = st.Peek();
63+
}
64+
sum = 0;
65+
}
66+
}
67+
ans += sum * sign;
68+
return ans;
69+
}
70+
}
71+
```
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 228\. Summary Ranges
5+
6+
Easy
7+
8+
You are given a **sorted unique** integer array `nums`.
9+
10+
Return _the **smallest sorted** list of ranges that **cover all the numbers in the array exactly**_. That is, each element of `nums` is covered by exactly one of the ranges, and there is no integer `x` such that `x` is in one of the ranges but not in `nums`.
11+
12+
Each range `[a,b]` in the list should be output as:
13+
14+
* `"a->b"` if `a != b`
15+
* `"a"` if `a == b`
16+
17+
**Example 1:**
18+
19+
**Input:** nums = [0,1,2,4,5,7]
20+
21+
**Output:** ["0->2","4->5","7"]
22+
23+
**Explanation:** The ranges are: [0,2] --> "0->2" [4,5] --> "4->5" [7,7] --> "7"
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [0,2,3,4,6,8,9]
28+
29+
**Output:** ["0","2->4","6","8->9"]
30+
31+
**Explanation:** The ranges are: [0,0] --> "0" [2,4] --> "2->4" [6,6] --> "6" [8,9] --> "8->9"
32+
33+
**Example 3:**
34+
35+
**Input:** nums = []
36+
37+
**Output:** []
38+
39+
**Example 4:**
40+
41+
**Input:** nums = [-1]
42+
43+
**Output:** ["-1"]
44+
45+
**Example 5:**
46+
47+
**Input:** nums = [0]
48+
49+
**Output:** ["0"]
50+
51+
**Constraints:**
52+
53+
* `0 <= nums.length <= 20`
54+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
55+
* All the values of `nums` are **unique**.
56+
* `nums` is sorted in ascending order.
57+
58+
## Solution
59+
60+
```csharp
61+
public class Solution {
62+
public IList<string> SummaryRanges(int[] nums) {
63+
List<string> result = new List<string>();
64+
if (nums.Length == 0) {
65+
return result;
66+
}
67+
int startIndex = 0;
68+
for (int i = 1; i < nums.Length; ++i) {
69+
if (nums[i] != nums[i - 1] + 1) {
70+
if (startIndex != i - 1) {
71+
result.Add(nums[startIndex] + "->" + (nums[i - 1]).ToString());
72+
} else {
73+
result.Add(nums[startIndex].ToString());
74+
}
75+
startIndex = i;
76+
}
77+
}
78+
if (startIndex != nums.Length - 1) {
79+
result.Add(nums[startIndex] + "->" + (nums[nums.Length - 1]).ToString());
80+
} else {
81+
result.Add(nums[startIndex].ToString());
82+
}
83+
return result;
84+
}
85+
}
86+
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 242\. Valid Anagram
5+
6+
Easy
7+
8+
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_.
9+
10+
**Example 1:**
11+
12+
**Input:** s = "anagram", t = "nagaram"
13+
14+
**Output:** true
15+
16+
**Example 2:**
17+
18+
**Input:** s = "rat", t = "car"
19+
20+
**Output:** false
21+
22+
**Constraints:**
23+
24+
* <code>1 <= s.length, t.length <= 5 * 10<sup>4</sup></code>
25+
* `s` and `t` consist of lowercase English letters.
26+
27+
**Follow up:** What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
28+
29+
## Solution
30+
31+
```csharp
32+
public class Solution {
33+
public bool IsAnagram(string s, string t) {
34+
if (s.Length != t.Length) {
35+
return false;
36+
}
37+
int[] charFreqMap = new int[26];
38+
foreach (char c in s) {
39+
charFreqMap[c - 'a']++;
40+
}
41+
foreach (char c in t) {
42+
if (charFreqMap[c - 'a'] == 0) {
43+
return false;
44+
}
45+
charFreqMap[c - 'a']--;
46+
}
47+
return true;
48+
}
49+
}
50+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 274\. H-Index
5+
6+
Medium
7+
8+
Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper, return compute the researcher's `h`**\-index**.
9+
10+
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): A scientist has an index `h` if `h` of their `n` papers have at least `h` citations each, and the other `n − h` papers have no more than `h` citations each.
11+
12+
If there are several possible values for `h`, the maximum one is taken as the `h`**\-index**.
13+
14+
**Example 1:**
15+
16+
**Input:** citations = [3,0,6,1,5]
17+
18+
**Output:** 3
19+
20+
**Explanation:**
21+
22+
[3,0,6,1,5] means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively.
23+
Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, their h-index is 3.
24+
25+
**Example 2:**
26+
27+
**Input:** citations = [1,3,1]
28+
29+
**Output:** 1
30+
31+
**Constraints:**
32+
33+
* `n == citations.length`
34+
* `1 <= n <= 5000`
35+
* `0 <= citations[i] <= 1000`
36+
37+
## Solution
38+
39+
```csharp
40+
public class Solution {
41+
public int HIndex(int[] citations) {
42+
int len = citations.Length;
43+
int[] freqArray = new int[len + 1];
44+
foreach (int citation in citations) {
45+
freqArray[Math.Min(citation, len)]++;
46+
}
47+
int totalSoFar = 0;
48+
for (int k = len; k >= 0; k--) {
49+
totalSoFar += freqArray[k];
50+
if (totalSoFar >= k) {
51+
return k;
52+
}
53+
}
54+
return 0;
55+
}
56+
}
57+
```

0 commit comments

Comments
 (0)