Skip to content

Commit a2a6d20

Browse files
authored
Added tasks 222-274
1 parent c28598a commit a2a6d20

File tree

15 files changed

+440
-0
lines changed

15 files changed

+440
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace LeetCodeNet.G0201_0300.S0222_count_complete_tree_nodes {
2+
3+
using LeetCodeNet.Com_github_leetcode;
4+
using Xunit;
5+
6+
public class SolutionTest {
7+
[Fact]
8+
public void CountNodes() {
9+
var leftNodeLeftNode = new TreeNode(4);
10+
var leftNodeRightNode = new TreeNode(5);
11+
var leftNode = new TreeNode(2, leftNodeLeftNode, leftNodeRightNode);
12+
var rightNodeLeftNode = new TreeNode(6);
13+
var rightNode = new TreeNode(3, rightNodeLeftNode, null);
14+
var root = new TreeNode(1, leftNode, rightNode);
15+
Assert.Equal(6, new Solution().CountNodes(root));
16+
}
17+
18+
[Fact]
19+
public void CountNodes2() {
20+
Assert.Equal(0, new Solution().CountNodes(null));
21+
}
22+
23+
[Fact]
24+
public void CountNodes3() {
25+
Assert.Equal(1, new Solution().CountNodes(new TreeNode(1)));
26+
}
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LeetCodeNet.G0201_0300.S0224_basic_calculator {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void Calculate() {
8+
Assert.Equal(2, new Solution().Calculate("1 + 1"));
9+
}
10+
11+
[Fact]
12+
public void Calculate2() {
13+
Assert.Equal(3, new Solution().Calculate(" 2-1 + 2 "));
14+
}
15+
16+
[Fact]
17+
public void Calculate3() {
18+
Assert.Equal(23, new Solution().Calculate("(1+(4+5+2)-3)+(6+8)"));
19+
}
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace LeetCodeNet.G0201_0300.S0228_summary_ranges {
2+
3+
using System.Collections.Generic;
4+
using Xunit;
5+
6+
public class SolutionTest {
7+
[Fact]
8+
public void SummaryRanges() {
9+
var expected = new List<string> { "0->2", "4->5", "7" };
10+
Assert.Equal(expected, new Solution().SummaryRanges(new[] { 0, 1, 2, 4, 5, 7 }));
11+
}
12+
13+
[Fact]
14+
public void SummaryRanges2() {
15+
var expected = new List<string> { "0", "2->4", "6", "8->9" };
16+
Assert.Equal(expected, new Solution().SummaryRanges(new[] { 0, 2, 3, 4, 6, 8, 9 }));
17+
}
18+
}
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace LeetCodeNet.G0201_0300.S0242_valid_anagram {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void IsAnagram() {
8+
Assert.True(new Solution().IsAnagram("anagram", "nagaram"));
9+
}
10+
11+
[Fact]
12+
public void IsAnagram2() {
13+
Assert.False(new Solution().IsAnagram("rat", "car"));
14+
}
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace LeetCodeNet.G0201_0300.S0274_h_index {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void HIndex() {
8+
Assert.Equal(3, new Solution().HIndex(new[] { 3, 0, 6, 1, 5 }));
9+
}
10+
11+
[Fact]
12+
public void HIndex2() {
13+
Assert.Equal(1, new Solution().HIndex(new[] { 1, 3, 1 }));
14+
}
15+
}
16+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
namespace LeetCodeNet.G0201_0300.S0222_count_complete_tree_nodes {
2+
3+
// #Easy #Depth_First_Search #Tree #Binary_Search #Binary_Tree #Binary_Search_II_Day_10
4+
// #Top_Interview_150_Binary_Tree_General #2025_07_15_Time_0_ms_(100.00%)_Space_50.72_MB_(19.76%)
5+
6+
using LeetCodeNet.Com_github_leetcode;
7+
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* public int val;
12+
* public TreeNode left;
13+
* public TreeNode right;
14+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
15+
* this.val = val;
16+
* this.left = left;
17+
* this.right = right;
18+
* }
19+
* }
20+
*/
21+
public class Solution {
22+
public int CountNodes(TreeNode root) {
23+
if (root == null) {
24+
return 0;
25+
}
26+
int leftHeight = LeftHeight(root);
27+
int rightHeight = RightHeight(root);
28+
if (leftHeight == rightHeight) {
29+
return (1 << leftHeight) - 1;
30+
} else {
31+
return 1 + CountNodes(root.left) + CountNodes(root.right);
32+
}
33+
}
34+
35+
private int LeftHeight(TreeNode root) {
36+
if (root == null) {
37+
return 0;
38+
}
39+
return 1 + LeftHeight(root.left);
40+
}
41+
42+
private int RightHeight(TreeNode root) {
43+
if (root == null) {
44+
return 0;
45+
}
46+
return 1 + RightHeight(root.right);
47+
}
48+
}
49+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
222\. Count Complete Tree Nodes
2+
3+
Medium
4+
5+
Given the `root` of a **complete** binary tree, return the number of the nodes in the tree.
6+
7+
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`.
8+
9+
Design an algorithm that runs in less than `O(n)` time complexity.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/01/14/complete.jpg)
14+
15+
**Input:** root = [1,2,3,4,5,6]
16+
17+
**Output:** 6
18+
19+
**Example 2:**
20+
21+
**Input:** root = []
22+
23+
**Output:** 0
24+
25+
**Example 3:**
26+
27+
**Input:** root = [1]
28+
29+
**Output:** 1
30+
31+
**Constraints:**
32+
33+
* The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.
34+
* <code>0 <= Node.val <= 5 * 10<sup>4</sup></code>
35+
* The tree is guaranteed to be **complete**.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace LeetCodeNet.G0201_0300.S0224_basic_calculator {
2+
3+
// #Hard #String #Math #Stack #Recursion #Top_Interview_150_Stack
4+
// #2025_07_15_Time_3_ms_(96.26%)_Space_42.26_MB_(95.95%)
5+
6+
public class Solution {
7+
public int Calculate(string s) {
8+
Stack<int> st = new Stack<int>();
9+
int ans = 0;
10+
int sum = 0;
11+
int sign = 1;
12+
st.Push(1);
13+
foreach (char ch in s) {
14+
if (ch >= '0' && ch <= '9') {
15+
sum = sum * 10 + ch - '0';
16+
} else if (ch == '(') {
17+
st.Push(sign);
18+
} else if (ch == ')') {
19+
st.Pop();
20+
} else if (ch == '-' || ch == '+') {
21+
ans += sum * sign;
22+
if (ch == '-') {
23+
sign = -1 * st.Peek();
24+
} else {
25+
sign = st.Peek();
26+
}
27+
sum = 0;
28+
}
29+
}
30+
ans += sum * sign;
31+
return ans;
32+
}
33+
}
34+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
224\. Basic Calculator
2+
3+
Hard
4+
5+
Given a string `s` representing a valid expression, implement a basic calculator to evaluate it, and return _the result of the evaluation_.
6+
7+
**Note:** You are **not** allowed to use any built-in function which evaluates strings as mathematical expressions, such as `eval()`.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "1 + 1"
12+
13+
**Output:** 2
14+
15+
**Example 2:**
16+
17+
**Input:** s = " 2-1 + 2 "
18+
19+
**Output:** 3
20+
21+
**Example 3:**
22+
23+
**Input:** s = "(1+(4+5+2)-3)+(6+8)"
24+
25+
**Output:** 23
26+
27+
**Constraints:**
28+
29+
* <code>1 <= s.length <= 3 * 10<sup>5</sup></code>
30+
* `s` consists of digits, `'+'`, `'-'`, `'('`, `')'`, and `' '`.
31+
* `s` represents a valid expression.
32+
* `'+'` is **not** used as a unary operation (i.e., `"+1"` and `"+(2 + 3)"` is invalid).
33+
* `'-'` could be used as a unary operation (i.e., `"-1"` and `"-(2 + 3)"` is valid).
34+
* There will be no two consecutive operators in the input.
35+
* Every number and running calculation will fit in a signed 32-bit integer.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace LeetCodeNet.G0201_0300.S0228_summary_ranges {
2+
3+
// #Easy #Array #Top_Interview_150_Intervals #2025_07_15_Time_0_ms_(100.00%)_Space_47.48_MB_(50.51%)
4+
5+
public class Solution {
6+
public IList<string> SummaryRanges(int[] nums) {
7+
List<string> result = new List<string>();
8+
if (nums.Length == 0) {
9+
return result;
10+
}
11+
int startIndex = 0;
12+
for (int i = 1; i < nums.Length; ++i) {
13+
if (nums[i] != nums[i - 1] + 1) {
14+
if (startIndex != i - 1) {
15+
result.Add(nums[startIndex] + "->" + (nums[i - 1]).ToString());
16+
} else {
17+
result.Add(nums[startIndex].ToString());
18+
}
19+
startIndex = i;
20+
}
21+
}
22+
if (startIndex != nums.Length - 1) {
23+
result.Add(nums[startIndex] + "->" + (nums[nums.Length - 1]).ToString());
24+
} else {
25+
result.Add(nums[startIndex].ToString());
26+
}
27+
return result;
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)