Skip to content

Commit c963e93

Browse files
authored
Added tasks 167-190
1 parent 46ca43f commit c963e93

File tree

15 files changed

+434
-0
lines changed

15 files changed

+434
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LeetCodeNet.G0101_0200.S0167_two_sum_ii_input_array_is_sorted {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void TwoSum() {
8+
Assert.Equal(new int[] {1, 2}, new Solution().TwoSum(new int[] {2, 7, 11, 15}, 9));
9+
}
10+
11+
[Fact]
12+
public void TwoSum2() {
13+
Assert.Equal(new int[] {1, 3}, new Solution().TwoSum(new int[] {2, 3, 4}, 6));
14+
}
15+
16+
[Fact]
17+
public void TwoSum3() {
18+
Assert.Equal(new int[] {1, 2}, new Solution().TwoSum(new int[] {-1, 0}, -1));
19+
}
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LeetCodeNet.G0101_0200.S0172_factorial_trailing_zeroes {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void TrailingZeroes() {
8+
Assert.Equal(0, new Solution().TrailingZeroes(3));
9+
}
10+
11+
[Fact]
12+
public void TrailingZeroes2() {
13+
Assert.Equal(1, new Solution().TrailingZeroes(5));
14+
}
15+
16+
[Fact]
17+
public void TrailingZeroes3() {
18+
Assert.Equal(0, new Solution().TrailingZeroes(0));
19+
}
20+
}
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace LeetCodeNet.G0101_0200.S0173_binary_search_tree_iterator {
2+
3+
using Xunit;
4+
using System.Collections.Generic;
5+
using LeetCodeNet.Com_github_leetcode;
6+
7+
public class BSTIteratorTest {
8+
[Fact]
9+
public void IteratorBST() {
10+
var left = new TreeNode(3);
11+
var right = new TreeNode(15, new TreeNode(9), new TreeNode(20));
12+
var root = new TreeNode(7, left, right);
13+
var iterator = new BSTIterator(root);
14+
Assert.Equal(3, iterator.Next());
15+
Assert.Equal(7, iterator.Next());
16+
Assert.True(iterator.HasNext());
17+
Assert.Equal(9, iterator.Next());
18+
Assert.True(iterator.HasNext());
19+
Assert.Equal(15, iterator.Next());
20+
Assert.True(iterator.HasNext());
21+
Assert.Equal(20, iterator.Next());
22+
Assert.False(iterator.HasNext());
23+
}
24+
}
25+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace LeetCodeNet.G0101_0200.S0188_best_time_to_buy_and_sell_stock_iv {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void MaxProfit() {
8+
Assert.Equal(2, new Solution().MaxProfit(2, new int[] {2, 4, 1}));
9+
}
10+
11+
[Fact]
12+
public void MaxProfit2() {
13+
Assert.Equal(7, new Solution().MaxProfit(2, new int[] {3, 2, 6, 5, 0, 3}));
14+
}
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace LeetCodeNet.G0101_0200.S0190_reverse_bits {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void reverseBits() {
8+
Assert.Equal(0b00111001011110000010100101000000u, new Solution().reverseBits(0b00000010100101000001111010011100u));
9+
}
10+
11+
[Fact]
12+
public void reverseBits2() {
13+
Assert.Equal(0b10111111111111111111111111111111u, new Solution().reverseBits(0b11111111111111111111111111111101u));
14+
}
15+
}
16+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace LeetCodeNet.G0101_0200.S0167_two_sum_ii_input_array_is_sorted {
2+
3+
// #Medium #Array #Binary_Search #Two_Pointers #Algorithm_I_Day_3_Two_Pointers
4+
// #Binary_Search_I_Day_7 #Top_Interview_150_Two_Pointers
5+
// #2025_07_13_Time_0_ms_(100.00%)_Space_50.79_MB_(76.45%)
6+
7+
public class Solution {
8+
public int[] TwoSum(int[] numbers, int target) {
9+
int[] res = new int[2];
10+
int i = 0;
11+
int j = numbers.Length - 1;
12+
while (i < j) {
13+
int sum = numbers[i] + numbers[j];
14+
if (sum == target) {
15+
res[0] = i + 1;
16+
res[1] = j + 1;
17+
return res;
18+
} else if (sum < target) {
19+
i++;
20+
} else {
21+
j--;
22+
}
23+
}
24+
return res;
25+
}
26+
}
27+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
167\. Two Sum II - Input Array Is Sorted
2+
3+
Easy
4+
5+
Given a **1-indexed** array of integers `numbers` that is already **_sorted in non-decreasing order_**, find two numbers such that they add up to a specific `target` number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index<sub>2</sub>]</code> where <code>1 <= index<sub>1</sub> < index<sub>2</sub> <= numbers.length</code>.
6+
7+
Return _the indices of the two numbers,_ <code>index<sub>1</sub></code> _and_ <code>index<sub>2</sub></code>_, **added by one** as an integer array_ <code>[index<sub>1</sub>, index<sub>2</sub>]</code> _of length 2._
8+
9+
The tests are generated such that there is **exactly one solution**. You **may not** use the same element twice.
10+
11+
**Example 1:**
12+
13+
**Input:** numbers = [2,7,11,15], target = 9
14+
15+
**Output:** [1,2]
16+
17+
**Explanation:** The sum of 2 and 7 is 9. Therefore, index<sub>1</sub> = 1, index<sub>2</sub> = 2. We return [1, 2].
18+
19+
**Example 2:**
20+
21+
**Input:** numbers = [2,3,4], target = 6
22+
23+
**Output:** [1,3]
24+
25+
**Explanation:** The sum of 2 and 4 is 6. Therefore index<sub>1</sub> = 1, index<sub>2</sub> = 3. We return [1, 3].
26+
27+
**Example 3:**
28+
29+
**Input:** numbers = [\-1,0], target = -1
30+
31+
**Output:** [1,2]
32+
33+
**Explanation:** The sum of -1 and 0 is -1. Therefore index<sub>1</sub> = 1, index<sub>2</sub> = 2. We return [1, 2].
34+
35+
**Constraints:**
36+
37+
* <code>2 <= numbers.length <= 3 * 10<sup>4</sup></code>
38+
* `-1000 <= numbers[i] <= 1000`
39+
* `numbers` is sorted in **non-decreasing order**.
40+
* `-1000 <= target <= 1000`
41+
* The tests are generated such that there is **exactly one solution**.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace LeetCodeNet.G0101_0200.S0172_factorial_trailing_zeroes {
2+
3+
// #Medium #Top_Interview_Questions #Math #Udemy_Integers #Top_Interview_150_Math
4+
// #2025_07_13_Time_0_ms_(100.00%)_Space_29.27_MB_(31.68%)
5+
6+
public class Solution {
7+
public int TrailingZeroes(int n) {
8+
int baseN = 5;
9+
int count = 0;
10+
while (n >= baseN) {
11+
count += n / baseN;
12+
baseN = baseN * 5;
13+
}
14+
return count;
15+
}
16+
}
17+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
172\. Factorial Trailing Zeroes
2+
3+
Medium
4+
5+
Given an integer `n`, return _the number of trailing zeroes in_ `n!`.
6+
7+
Note that `n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1`.
8+
9+
**Example 1:**
10+
11+
**Input:** n = 3
12+
13+
**Output:** 0
14+
15+
**Explanation:** 3! = 6, no trailing zero.
16+
17+
**Example 2:**
18+
19+
**Input:** n = 5
20+
21+
**Output:** 1
22+
23+
**Explanation:** 5! = 120, one trailing zero.
24+
25+
**Example 3:**
26+
27+
**Input:** n = 0
28+
29+
**Output:** 0
30+
31+
**Constraints:**
32+
33+
* <code>0 <= n <= 10<sup>4</sup></code>
34+
35+
**Follow up:** Could you write a solution that works in logarithmic time complexity?
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
namespace LeetCodeNet.G0101_0200.S0173_binary_search_tree_iterator {
2+
3+
// #Medium #Tree #Binary_Tree #Stack #Design #Binary_Search_Tree #Iterator
4+
// #Data_Structure_II_Day_17_Tree #Programming_Skills_II_Day_16 #Level_2_Day_9_Binary_Search_Tree
5+
// #Top_Interview_150_Binary_Tree_General #2025_07_13_Time_1_ms_(100.00%)_Space_62.77_MB_(66.48%)
6+
7+
using LeetCodeNet.Com_github_leetcode;
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* public class TreeNode {
12+
* public int val;
13+
* public TreeNode left;
14+
* public TreeNode right;
15+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
16+
* this.val = val;
17+
* this.left = left;
18+
* this.right = right;
19+
* }
20+
* }
21+
*/
22+
public class BSTIterator {
23+
private TreeNode node;
24+
25+
public BSTIterator(TreeNode root) {
26+
node = root;
27+
}
28+
29+
public int Next() {
30+
int res = -1;
31+
while (node != null) {
32+
if (node.left != null) {
33+
TreeNode rightMost = node.left;
34+
while (rightMost.right != null) {
35+
rightMost = rightMost.right;
36+
}
37+
rightMost.right = node;
38+
TreeNode temp = node.left;
39+
node.left = null;
40+
node = temp;
41+
} else {
42+
res = node.val.Value;
43+
node = node.right;
44+
return res;
45+
}
46+
}
47+
return res;
48+
}
49+
50+
public bool HasNext() {
51+
return node != null;
52+
}
53+
}
54+
55+
/**
56+
* Your BSTIterator object will be instantiated and called as such:
57+
* BSTIterator obj = new BSTIterator(root);
58+
* int param_1 = obj.Next();
59+
* bool param_2 = obj.HasNext();
60+
*/
61+
}

0 commit comments

Comments
 (0)