Skip to content

Added tasks 191-205 #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace LeetCodeNet.G0101_0200.S0191_number_of_1_bits {

using Xunit;

public class SolutionTest {
[Fact]
public void HammingWeight() {
Assert.Equal(3, new Solution().HammingWeight(0b00000000000000000000000000001011));
}

[Fact]
public void HammingWeight2() {
Assert.Equal(1, new Solution().HammingWeight(0b00000000000000000000000010000000));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace LeetCodeNet.G0101_0200.S0199_binary_tree_right_side_view {

using Xunit;
using System.Collections.Generic;
using LeetCodeNet.Com_github_leetcode;

public class SolutionTest {
[Fact]
public void RightSideView() {
var left = new TreeNode(2, null, new TreeNode(5));
var right = new TreeNode(3, null, new TreeNode(4));
var root = new TreeNode(1, left, right);
Assert.Equal(new List<int> {1, 3, 4}, new Solution().RightSideView(root));
}

[Fact]
public void RightSideView2() {
var root = new TreeNode(1, null, new TreeNode(3));
Assert.Equal(new List<int> {1, 3}, new Solution().RightSideView(root));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace LeetCodeNet.G0201_0300.S0201_bitwise_and_of_numbers_range {

using Xunit;

public class SolutionTest {
[Fact]
public void RangeBitwiseAnd() {
Assert.Equal(4, new Solution().RangeBitwiseAnd(5, 7));
}

[Fact]
public void RangeBitwiseAnd2() {
Assert.Equal(0, new Solution().RangeBitwiseAnd(0, 0));
}

[Fact]
public void RangeBitwiseAnd3() {
Assert.Equal(0, new Solution().RangeBitwiseAnd(1, 2147483647));
}
}
}
16 changes: 16 additions & 0 deletions LeetCodeNet.Tests/G0201_0300/S0202_happy_number/SolutionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace LeetCodeNet.G0201_0300.S0202_happy_number {

using Xunit;

public class SolutionTest {
[Fact]
public void IsHappy() {
Assert.True(new Solution().IsHappy(19));
}

[Fact]
public void IsHappy2() {
Assert.False(new Solution().IsHappy(2));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace LeetCodeNet.G0201_0300.S0205_isomorphic_strings {

using Xunit;

public class SolutionTest {
[Fact]
public void IsIsomorphic() {
Assert.True(new Solution().IsIsomorphic("egg", "add"));
}

[Fact]
public void IsIsomorphic2() {
Assert.False(new Solution().IsIsomorphic("foo", "bar"));
}

[Fact]
public void IsIsomorphic3() {
Assert.True(new Solution().IsIsomorphic("paper", "title"));
}
}
}
23 changes: 23 additions & 0 deletions LeetCodeNet/G0101_0200/S0191_number_of_1_bits/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace LeetCodeNet.G0101_0200.S0191_number_of_1_bits {

// #Easy #Top_Interview_Questions #Bit_Manipulation #Algorithm_I_Day_13_Bit_Manipulation
// #Programming_Skills_I_Day_2_Operator #Udemy_Bit_Manipulation #Top_Interview_150_Bit_Manipulation
// #2025_07_13_Time_0_ms_(100.00%)_Space_28.91_MB_(71.79%)

public class Solution {
public int HammingWeight(int n) {
int sum = 0;
bool flag = false;
if (n < 0) {
flag = true;
n = n - int.MinValue;
}
while (n > 0) {
int k = n % 2;
sum += k;
n /= 2;
}
return flag ? sum + 1 : sum;
}
}
}
40 changes: 40 additions & 0 deletions LeetCodeNet/G0101_0200/S0191_number_of_1_bits/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
191\. Number of 1 Bits

Easy

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)).

**Note:**

* 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.
* 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`.

**Example 1:**

**Input:** n = 00000000000000000000000000001011

**Output:** 3

**Explanation:** The input binary string **00000000000000000000000000001011** has a total of three '1' bits.

**Example 2:**

**Input:** n = 00000000000000000000000010000000

**Output:** 1

**Explanation:** The input binary string **00000000000000000000000010000000** has a total of one '1' bit.

**Example 3:**

**Input:** n = 11111111111111111111111111111101

**Output:** 31

**Explanation:** The input binary string **11111111111111111111111111111101** has a total of thirty one '1' bits.

**Constraints:**

* The input must be a **binary string** of length `32`.

**Follow up:** If this function is called many times, how would you optimize it?
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace LeetCodeNet.G0101_0200.S0199_binary_tree_right_side_view {

// #Medium #Top_100_Liked_Questions #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
// #LeetCode_75_Binary_Tree/BFS #Data_Structure_II_Day_16_Tree #Level_2_Day_15_Tree
// #Top_Interview_150_Binary_Tree_BFS #2025_07_13_Time_0_ms_(100.00%)_Space_47.47_MB_(41.96%)

using System.Collections.Generic;
using LeetCodeNet.Com_github_leetcode;

/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public IList<int> RightSideView(TreeNode root) {
var list = new List<int>();
Recurse(root, 0, list);
return list;
}

private void Recurse(TreeNode node, int level, List<int> list) {
if (node != null) {
if (list.Count < level + 1) {
list.Add(node.val.Value);

Check warning on line 33 in LeetCodeNet/G0101_0200/S0199_binary_tree_right_side_view/Solution.cs

View workflow job for this annotation

GitHub Actions / build-windows

Nullable value type may be null.

Check warning on line 33 in LeetCodeNet/G0101_0200/S0199_binary_tree_right_side_view/Solution.cs

View workflow job for this annotation

GitHub Actions / build-windows

Nullable value type may be null.
}
Recurse(node.right, level + 1, list);
Recurse(node.left, level + 1, list);
}
}
}
}
30 changes: 30 additions & 0 deletions LeetCodeNet/G0101_0200/S0199_binary_tree_right_side_view/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
199\. Binary Tree Right Side View

Medium

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_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/02/14/tree.jpg)

**Input:** root = [1,2,3,null,5,null,4]

**Output:** [1,3,4]

**Example 2:**

**Input:** root = [1,null,3]

**Output:** [1,3]

**Example 3:**

**Input:** root = []

**Output:** []

**Constraints:**

* The number of nodes in the tree is in the range `[0, 100]`.
* `-100 <= Node.val <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace LeetCodeNet.G0201_0300.S0201_bitwise_and_of_numbers_range {

// #Medium #Bit_Manipulation #Algorithm_II_Day_19_Bit_Manipulation
// #Top_Interview_150_Bit_Manipulation #2025_07_13_Time_1_ms_(96.12%)_Space_29.94_MB_(51.46%)

public class Solution {
public int RangeBitwiseAnd(int left, int right) {
var shift = 0;
for (; left != right; left >>= 1, right >>= 1, shift++) {

}
return left << shift;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
201\. Bitwise AND of Numbers Range

Medium

Given two integers `left` and `right` that represent the range `[left, right]`, return _the bitwise AND of all numbers in this range, inclusive_.

**Example 1:**

**Input:** left = 5, right = 7

**Output:** 4

**Example 2:**

**Input:** left = 0, right = 0

**Output:** 0

**Example 3:**

**Input:** left = 1, right = 2147483647

**Output:** 0

**Constraints:**

* <code>0 <= left <= right <= 2<sup>31</sup> - 1</code>
32 changes: 32 additions & 0 deletions LeetCodeNet/G0201_0300/S0202_happy_number/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace LeetCodeNet.G0201_0300.S0202_happy_number {

// #Easy #Top_Interview_Questions #Hash_Table #Math #Two_Pointers #Algorithm_II_Day_21_Others
// #Programming_Skills_I_Day_4_Loop #Level_2_Day_1_Implementation/Simulation
// #Top_Interview_150_Hashmap #2025_07_13_Time_0_ms_(100.00%)_Space_30.91_MB_(85.61%)

public class Solution {
public bool IsHappy(int n) {
bool happy;
int a = n;
int rem;
int sum = 0;
if (a == 1 || a == 7) {
happy = true;
} else if (a > 1 && a < 10) {
happy = false;
} else {
while (a != 0) {
rem = a % 10;
sum = sum + (rem * rem);
a = a / 10;
}
if (sum != 1) {
happy = IsHappy(sum);
} else {
happy = true;
}
}
return happy;
}
}
}
39 changes: 39 additions & 0 deletions LeetCodeNet/G0201_0300/S0202_happy_number/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
202\. Happy Number

Easy

Write an algorithm to determine if a number `n` is happy.

A **happy number** is a number defined by the following process:

* Starting with any positive integer, replace the number by the sum of the squares of its digits.
* Repeat the process until the number equals 1 (where it will stay), or it **loops endlessly in a cycle** which does not include 1.
* Those numbers for which this process **ends in 1** are happy.

Return `true` _if_ `n` _is a happy number, and_ `false` _if not_.

**Example 1:**

**Input:** n = 19

**Output:** true

**Explanation:**

1<sup>2</sup> + 9<sup>2</sup> = 82

8<sup>2</sup> + 2<sup>2</sup> = 68

6<sup>2</sup> + 8<sup>2</sup> = 100

1<sup>2</sup> + 0<sup>2</sup> + 0<sup>2</sup> = 1

**Example 2:**

**Input:** n = 2

**Output:** false

**Constraints:**

* <code>1 <= n <= 2<sup>31</sup> - 1</code>
39 changes: 39 additions & 0 deletions LeetCodeNet/G0201_0300/S0205_isomorphic_strings/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace LeetCodeNet.G0201_0300.S0205_isomorphic_strings {

// #Easy #String #Hash_Table #Level_1_Day_2_String #Top_Interview_150_Hashmap
// #2025_07_13_Time_2_ms_(90.78%)_Space_44.12_MB_(18.33%)

public class Solution {
public bool IsIsomorphic(string s, string t) {
int[] map = new int[128];
char[] str = s.ToCharArray();
char[] tar = t.ToCharArray();
int n = str.Length;
for (int i = 0; i < n; i++) {
if (map[tar[i]] == 0) {
if (Search(map, str[i], tar[i]) != -1) {
return false;
}
map[tar[i]] = str[i];
} else {
if (map[tar[i]] != str[i]) {
return false;
}
}
}
return true;
}

private int Search(int[] map, int tar, int skip) {
for (int i = 0; i < 128; i++) {
if (i == skip) {
continue;
}
if (map[i] != 0 && map[i] == tar) {
return i;
}
}
return -1;
}
}
}
Loading
Loading