Skip to content

Commit 86a56ae

Browse files
authored
Added tasks 191-205
1 parent 9b714c1 commit 86a56ae

File tree

15 files changed

+414
-0
lines changed

15 files changed

+414
-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.S0191_number_of_1_bits {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void HammingWeight() {
8+
Assert.Equal(3, new Solution().HammingWeight(0b00000000000000000000000000001011));
9+
}
10+
11+
[Fact]
12+
public void HammingWeight2() {
13+
Assert.Equal(1, new Solution().HammingWeight(0b00000000000000000000000010000000));
14+
}
15+
}
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace LeetCodeNet.G0101_0200.S0199_binary_tree_right_side_view {
2+
3+
using Xunit;
4+
using System.Collections.Generic;
5+
using LeetCodeNet.Com_github_leetcode;
6+
7+
public class SolutionTest {
8+
[Fact]
9+
public void RightSideView() {
10+
var left = new TreeNode(2, null, new TreeNode(5));
11+
var right = new TreeNode(3, null, new TreeNode(4));
12+
var root = new TreeNode(1, left, right);
13+
Assert.Equal(new List<int> {1, 3, 4}, new Solution().RightSideView(root));
14+
}
15+
16+
[Fact]
17+
public void RightSideView2() {
18+
var root = new TreeNode(1, null, new TreeNode(3));
19+
Assert.Equal(new List<int> {1, 3}, new Solution().RightSideView(root));
20+
}
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LeetCodeNet.G0201_0300.S0201_bitwise_and_of_numbers_range {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void RangeBitwiseAnd() {
8+
Assert.Equal(4, new Solution().RangeBitwiseAnd(5, 7));
9+
}
10+
11+
[Fact]
12+
public void RangeBitwiseAnd2() {
13+
Assert.Equal(0, new Solution().RangeBitwiseAnd(0, 0));
14+
}
15+
16+
[Fact]
17+
public void RangeBitwiseAnd3() {
18+
Assert.Equal(0, new Solution().RangeBitwiseAnd(1, 2147483647));
19+
}
20+
}
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace LeetCodeNet.G0201_0300.S0202_happy_number {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void IsHappy() {
8+
Assert.True(new Solution().IsHappy(19));
9+
}
10+
11+
[Fact]
12+
public void IsHappy2() {
13+
Assert.False(new Solution().IsHappy(2));
14+
}
15+
}
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LeetCodeNet.G0201_0300.S0205_isomorphic_strings {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void IsIsomorphic() {
8+
Assert.True(new Solution().IsIsomorphic("egg", "add"));
9+
}
10+
11+
[Fact]
12+
public void IsIsomorphic2() {
13+
Assert.False(new Solution().IsIsomorphic("foo", "bar"));
14+
}
15+
16+
[Fact]
17+
public void IsIsomorphic3() {
18+
Assert.True(new Solution().IsIsomorphic("paper", "title"));
19+
}
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace LeetCodeNet.G0101_0200.S0191_number_of_1_bits {
2+
3+
// #Easy #Top_Interview_Questions #Bit_Manipulation #Algorithm_I_Day_13_Bit_Manipulation
4+
// #Programming_Skills_I_Day_2_Operator #Udemy_Bit_Manipulation #Top_Interview_150_Bit_Manipulation
5+
// #2025_07_13_Time_0_ms_(100.00%)_Space_28.91_MB_(71.79%)
6+
7+
public class Solution {
8+
public int HammingWeight(int n) {
9+
int sum = 0;
10+
bool flag = false;
11+
if (n < 0) {
12+
flag = true;
13+
n = n - int.MinValue;
14+
}
15+
while (n > 0) {
16+
int k = n % 2;
17+
sum += k;
18+
n /= 2;
19+
}
20+
return flag ? sum + 1 : sum;
21+
}
22+
}
23+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
191\. Number of 1 Bits
2+
3+
Easy
4+
5+
Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
6+
7+
**Note:**
8+
9+
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
10+
* In Java, the compiler represents the signed integers using [2's complement notation](https://en.wikipedia.org/wiki/Two%27s_complement). Therefore, in **Example 3**, the input represents the signed integer. `-3`.
11+
12+
**Example 1:**
13+
14+
**Input:** n = 00000000000000000000000000001011
15+
16+
**Output:** 3
17+
18+
**Explanation:** The input binary string **00000000000000000000000000001011** has a total of three '1' bits.
19+
20+
**Example 2:**
21+
22+
**Input:** n = 00000000000000000000000010000000
23+
24+
**Output:** 1
25+
26+
**Explanation:** The input binary string **00000000000000000000000010000000** has a total of one '1' bit.
27+
28+
**Example 3:**
29+
30+
**Input:** n = 11111111111111111111111111111101
31+
32+
**Output:** 31
33+
34+
**Explanation:** The input binary string **11111111111111111111111111111101** has a total of thirty one '1' bits.
35+
36+
**Constraints:**
37+
38+
* The input must be a **binary string** of length `32`.
39+
40+
**Follow up:** If this function is called many times, how would you optimize it?
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace LeetCodeNet.G0101_0200.S0199_binary_tree_right_side_view {
2+
3+
// #Medium #Top_100_Liked_Questions #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
4+
// #LeetCode_75_Binary_Tree/BFS #Data_Structure_II_Day_16_Tree #Level_2_Day_15_Tree
5+
// #Top_Interview_150_Binary_Tree_BFS #2025_07_13_Time_0_ms_(100.00%)_Space_47.47_MB_(41.96%)
6+
7+
using System.Collections.Generic;
8+
using LeetCodeNet.Com_github_leetcode;
9+
10+
/**
11+
* Definition for a binary tree node.
12+
* public class TreeNode {
13+
* public int val;
14+
* public TreeNode left;
15+
* public TreeNode right;
16+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
17+
* this.val = val;
18+
* this.left = left;
19+
* this.right = right;
20+
* }
21+
* }
22+
*/
23+
public class Solution {
24+
public IList<int> RightSideView(TreeNode root) {
25+
var list = new List<int>();
26+
Recurse(root, 0, list);
27+
return list;
28+
}
29+
30+
private void Recurse(TreeNode node, int level, List<int> list) {
31+
if (node != null) {
32+
if (list.Count < level + 1) {
33+
list.Add(node.val.Value);
34+
}
35+
Recurse(node.right, level + 1, list);
36+
Recurse(node.left, level + 1, list);
37+
}
38+
}
39+
}
40+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
199\. Binary Tree Right Side View
2+
3+
Medium
4+
5+
Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return _the values of the nodes you can see ordered from top to bottom_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/02/14/tree.jpg)
10+
11+
**Input:** root = [1,2,3,null,5,null,4]
12+
13+
**Output:** [1,3,4]
14+
15+
**Example 2:**
16+
17+
**Input:** root = [1,null,3]
18+
19+
**Output:** [1,3]
20+
21+
**Example 3:**
22+
23+
**Input:** root = []
24+
25+
**Output:** []
26+
27+
**Constraints:**
28+
29+
* The number of nodes in the tree is in the range `[0, 100]`.
30+
* `-100 <= Node.val <= 100`
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace LeetCodeNet.G0201_0300.S0201_bitwise_and_of_numbers_range {
2+
3+
// #Medium #Bit_Manipulation #Algorithm_II_Day_19_Bit_Manipulation
4+
// #Top_Interview_150_Bit_Manipulation #2025_07_13_Time_1_ms_(96.12%)_Space_29.94_MB_(51.46%)
5+
6+
public class Solution {
7+
public int RangeBitwiseAnd(int left, int right) {
8+
var shift = 0;
9+
for (; left != right; left >>= 1, right >>= 1, shift++) {
10+
11+
}
12+
return left << shift;
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)