Skip to content

Commit 11e9d28

Browse files
authored
Added tasks-137-162
1 parent 54f2d27 commit 11e9d28

File tree

15 files changed

+409
-0
lines changed

15 files changed

+409
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace LeetCodeNet.G0101_0200.S0137_single_number_ii {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void SingleNumber() {
8+
Assert.Equal(3, new Solution().SingleNumber(new int[] {2, 2, 3, 2}));
9+
}
10+
11+
[Fact]
12+
public void SingleNumber2() {
13+
Assert.Equal(99, new Solution().SingleNumber(new int[] {0, 1, 0, 1, 0, 1, 99}));
14+
}
15+
}
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace LeetCodeNet.G0101_0200.S0149_max_points_on_a_line {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void MaxPoints() {
8+
int[][] input = new int[][] {new int[] {1, 1}, new int[] {2, 2}, new int[] {3, 3}};
9+
Assert.Equal(3, new Solution().MaxPoints(input));
10+
}
11+
12+
[Fact]
13+
public void MaxPoints2() {
14+
int[][] input = new int[][] {new int[] {1, 1}, new int[] {3, 2}, new int[] {5, 3}, new int[] {4, 1}, new int[] {2, 3}, new int[] {1, 4}};
15+
Assert.Equal(4, new Solution().MaxPoints(input));
16+
}
17+
}
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace LeetCodeNet.G0101_0200.S0150_evaluate_reverse_polish_notation {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void EvalRPN() {
8+
Assert.Equal(9, new Solution().EvalRPN(new string[] {"2", "1", "+", "3", "*"}));
9+
}
10+
11+
[Fact]
12+
public void EvalRPN2() {
13+
Assert.Equal(6, new Solution().EvalRPN(new string[] {"4", "13", "5", "/", "+"}));
14+
}
15+
16+
[Fact]
17+
public void EvalRPN3() {
18+
Assert.Equal(22, new Solution().EvalRPN(new string[] {
19+
"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"
20+
}));
21+
}
22+
}
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LeetCodeNet.G0101_0200.S0151_reverse_words_in_a_string {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void ReverseWords() {
8+
Assert.Equal("blue is sky the", new Solution().ReverseWords("the sky is blue"));
9+
}
10+
11+
[Fact]
12+
public void ReverseWords2() {
13+
Assert.Equal("world hello", new Solution().ReverseWords(" hello world "));
14+
}
15+
16+
[Fact]
17+
public void ReverseWords3() {
18+
Assert.Equal("example good a", new Solution().ReverseWords("a good example"));
19+
}
20+
}
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace LeetCodeNet.G0101_0200.S0162_find_peak_element {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void FindPeakElement() {
8+
Assert.Equal(2, new Solution().FindPeakElement(new int[] {1, 2, 3, 1}));
9+
}
10+
11+
[Fact]
12+
public void FindPeakElement2() {
13+
Assert.Equal(5, new Solution().FindPeakElement(new int[] {1, 2, 1, 3, 5, 6, 4}));
14+
}
15+
}
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace LeetCodeNet.G0101_0200.S0137_single_number_ii {
2+
3+
// #Medium #Array #Bit_Manipulation #Top_Interview_150_Bit_Manipulation
4+
// #2025_07_12_Time_0_ms_(100.00%)_Space_43.58_MB_(81.90%)
5+
6+
public class Solution {
7+
public int SingleNumber(int[] nums) {
8+
int ones = 0;
9+
int twos = 0;
10+
foreach (int num in nums) {
11+
ones = (ones ^ num) & (~twos);
12+
twos = (twos ^ num) & (~ones);
13+
}
14+
return ones;
15+
}
16+
}
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
137\. Single Number II
2+
3+
Medium
4+
5+
Given an integer array `nums` where every element appears **three times** except for one, which appears **exactly once**. _Find the single element and return it_.
6+
7+
You must implement a solution with a linear runtime complexity and use only constant extra space.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [2,2,3,2]
12+
13+
**Output:** 3
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [0,1,0,1,0,1,99]
18+
19+
**Output:** 99
20+
21+
**Constraints:**
22+
23+
* <code>1 <= nums.length <= 3 * 10<sup>4</sup></code>
24+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
25+
* Each element in `nums` appears exactly **three times** except for one element which appears **once**.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace LeetCodeNet.G0101_0200.S0149_max_points_on_a_line {
2+
3+
// #Hard #Top_Interview_Questions #Array #Hash_Table #Math #Geometry #Algorithm_II_Day_21_Others
4+
// #Top_Interview_150_Math #2025_07_12_Time_5_ms_(100.00%)_Space_41.59_MB_(98.57%)
5+
6+
public class Solution {
7+
public int MaxPoints(int[][] points) {
8+
if (points.Length < 2) {
9+
return points.Length;
10+
}
11+
int max = 0;
12+
for (int i = 0; i < points.Length; i++) {
13+
for (int j = i + 1; j < points.Length; j++) {
14+
int x = points[j][0] - points[i][0];
15+
int y = points[j][1] - points[i][1];
16+
int c = x * points[j][1] - y * points[j][0];
17+
int count = 2;
18+
for (int k = j + 1; k < points.Length; k++) {
19+
if (c == x * points[k][1] - y * points[k][0]) {
20+
count++;
21+
}
22+
}
23+
max = System.Math.Max(count, max);
24+
}
25+
}
26+
return max;
27+
}
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
149\. Max Points on a Line
2+
3+
Hard
4+
5+
Given an array of `points` where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the **X-Y** plane, return _the maximum number of points that lie on the same straight line_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg)
10+
11+
**Input:** points = [[1,1],[2,2],[3,3]]
12+
13+
**Output:** 3
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg)
18+
19+
**Input:** points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
20+
21+
**Output:** 4
22+
23+
**Constraints:**
24+
25+
* `1 <= points.length <= 300`
26+
* `points[i].length == 2`
27+
* <code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code>
28+
* All the `points` are **unique**.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace LeetCodeNet.G0101_0200.S0150_evaluate_reverse_polish_notation {
2+
3+
// #Medium #Top_Interview_Questions #Array #Math #Stack #Programming_Skills_II_Day_3
4+
// #Top_Interview_150_Stack #2025_07_12_Time_11_ms_(77.03%)_Space_44.57_MB_(49.39%)
5+
6+
using System.Collections.Generic;
7+
8+
public class Solution {
9+
public int EvalRPN(string[] tokens) {
10+
var st = new Stack<int>();
11+
foreach (string token in tokens) {
12+
if (!char.IsDigit(token[token.Length - 1])) {
13+
int second = st.Pop();
14+
int first = st.Pop();
15+
st.Push(Eval(first, second, token));
16+
} else {
17+
st.Push(int.Parse(token));
18+
}
19+
}
20+
return st.Pop();
21+
}
22+
23+
private int Eval(int first, int second, string op) {
24+
return op switch {
25+
"+" => first + second,
26+
"-" => first - second,
27+
"*" => first * second,
28+
_ => first / second
29+
};
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)