Skip to content

Commit ffa288e

Browse files
committed
Added tasks 191-219
1 parent 51c2c3d commit ffa288e

File tree

12 files changed

+843
-6
lines changed

12 files changed

+843
-6
lines changed

LeetCodeNet/G0101_0200/S0133_clone_graph/readme.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,27 +95,31 @@ public class Node {
9595

9696
public override string ToString() {
9797
var result = new System.Text.StringBuilder();
98-
result.Append("[");
98+
result.Append('[');
9999
bool first = true;
100100
foreach (var node in neighbors) {
101-
if (!first) result.Append(",");
101+
if (!first) {
102+
result.Append(',');
103+
}
102104
if (node.neighbors == null || node.neighbors.Count == 0) {
103105
result.Append(node.val);
104106
} else {
105107
var inner = new System.Text.StringBuilder();
106-
inner.Append("[");
108+
inner.Append('[');
107109
bool innerFirst = true;
108110
foreach (var nodeItem in node.neighbors) {
109-
if (!innerFirst) inner.Append(",");
111+
if (!innerFirst) {
112+
inner.Append(',');
113+
}
110114
inner.Append(nodeItem.val);
111115
innerFirst = false;
112116
}
113-
inner.Append("]");
117+
inner.Append(']');
114118
result.Append(inner.ToString());
115119
}
116120
first = false;
117121
}
118-
result.Append("]");
122+
result.Append(']');
119123
return result.ToString();
120124
}
121125
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 191\. Number of 1 Bits
5+
6+
Easy
7+
8+
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)).
9+
10+
**Note:**
11+
12+
* 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.
13+
* 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`.
14+
15+
**Example 1:**
16+
17+
**Input:** n = 00000000000000000000000000001011
18+
19+
**Output:** 3
20+
21+
**Explanation:** The input binary string **00000000000000000000000000001011** has a total of three '1' bits.
22+
23+
**Example 2:**
24+
25+
**Input:** n = 00000000000000000000000010000000
26+
27+
**Output:** 1
28+
29+
**Explanation:** The input binary string **00000000000000000000000010000000** has a total of one '1' bit.
30+
31+
**Example 3:**
32+
33+
**Input:** n = 11111111111111111111111111111101
34+
35+
**Output:** 31
36+
37+
**Explanation:** The input binary string **11111111111111111111111111111101** has a total of thirty one '1' bits.
38+
39+
**Constraints:**
40+
41+
* The input must be a **binary string** of length `32`.
42+
43+
**Follow up:** If this function is called many times, how would you optimize it?
44+
45+
## Solution
46+
47+
```csharp
48+
public class Solution {
49+
public int HammingWeight(int n) {
50+
int sum = 0;
51+
bool flag = false;
52+
if (n < 0) {
53+
flag = true;
54+
n = n - int.MinValue;
55+
}
56+
while (n > 0) {
57+
int k = n % 2;
58+
sum += k;
59+
n /= 2;
60+
}
61+
return flag ? sum + 1 : sum;
62+
}
63+
}
64+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 199\. Binary Tree Right Side View
5+
6+
Medium
7+
8+
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_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/02/14/tree.jpg)
13+
14+
**Input:** root = [1,2,3,null,5,null,4]
15+
16+
**Output:** [1,3,4]
17+
18+
**Example 2:**
19+
20+
**Input:** root = [1,null,3]
21+
22+
**Output:** [1,3]
23+
24+
**Example 3:**
25+
26+
**Input:** root = []
27+
28+
**Output:** []
29+
30+
**Constraints:**
31+
32+
* The number of nodes in the tree is in the range `[0, 100]`.
33+
* `-100 <= Node.val <= 100`
34+
35+
## Solution
36+
37+
```csharp
38+
using System.Collections.Generic;
39+
using LeetCodeNet.Com_github_leetcode;
40+
41+
/**
42+
* Definition for a binary tree node.
43+
* public class TreeNode {
44+
* public int val;
45+
* public TreeNode left;
46+
* public TreeNode right;
47+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
48+
* this.val = val;
49+
* this.left = left;
50+
* this.right = right;
51+
* }
52+
* }
53+
*/
54+
public class Solution {
55+
public IList<int> RightSideView(TreeNode root) {
56+
var list = new List<int>();
57+
Recurse(root, 0, list);
58+
return list;
59+
}
60+
61+
private void Recurse(TreeNode node, int level, List<int> list) {
62+
if (node != null) {
63+
if (list.Count < level + 1) {
64+
list.Add(node.val.Value);
65+
}
66+
Recurse(node.right, level + 1, list);
67+
Recurse(node.left, level + 1, list);
68+
}
69+
}
70+
}
71+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 201\. Bitwise AND of Numbers Range
5+
6+
Medium
7+
8+
Given two integers `left` and `right` that represent the range `[left, right]`, return _the bitwise AND of all numbers in this range, inclusive_.
9+
10+
**Example 1:**
11+
12+
**Input:** left = 5, right = 7
13+
14+
**Output:** 4
15+
16+
**Example 2:**
17+
18+
**Input:** left = 0, right = 0
19+
20+
**Output:** 0
21+
22+
**Example 3:**
23+
24+
**Input:** left = 1, right = 2147483647
25+
26+
**Output:** 0
27+
28+
**Constraints:**
29+
30+
* <code>0 <= left <= right <= 2<sup>31</sup> - 1</code>
31+
32+
## Solution
33+
34+
```csharp
35+
public class Solution {
36+
public int RangeBitwiseAnd(int left, int right) {
37+
var shift = 0;
38+
for (; left != right; left >>= 1, right >>= 1, shift++) {
39+
40+
}
41+
return left << shift;
42+
}
43+
}
44+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 202\. Happy Number
5+
6+
Easy
7+
8+
Write an algorithm to determine if a number `n` is happy.
9+
10+
A **happy number** is a number defined by the following process:
11+
12+
* Starting with any positive integer, replace the number by the sum of the squares of its digits.
13+
* Repeat the process until the number equals 1 (where it will stay), or it **loops endlessly in a cycle** which does not include 1.
14+
* Those numbers for which this process **ends in 1** are happy.
15+
16+
Return `true` _if_ `n` _is a happy number, and_ `false` _if not_.
17+
18+
**Example 1:**
19+
20+
**Input:** n = 19
21+
22+
**Output:** true
23+
24+
**Explanation:**
25+
26+
1<sup>2</sup> + 9<sup>2</sup> = 82
27+
28+
8<sup>2</sup> + 2<sup>2</sup> = 68
29+
30+
6<sup>2</sup> + 8<sup>2</sup> = 100
31+
32+
1<sup>2</sup> + 0<sup>2</sup> + 0<sup>2</sup> = 1
33+
34+
**Example 2:**
35+
36+
**Input:** n = 2
37+
38+
**Output:** false
39+
40+
**Constraints:**
41+
42+
* <code>1 <= n <= 2<sup>31</sup> - 1</code>
43+
44+
## Solution
45+
46+
```csharp
47+
public class Solution {
48+
public bool IsHappy(int n) {
49+
bool happy;
50+
int a = n;
51+
int rem;
52+
int sum = 0;
53+
if (a == 1 || a == 7) {
54+
happy = true;
55+
} else if (a > 1 && a < 10) {
56+
happy = false;
57+
} else {
58+
while (a != 0) {
59+
rem = a % 10;
60+
sum = sum + (rem * rem);
61+
a = a / 10;
62+
}
63+
if (sum != 1) {
64+
happy = IsHappy(sum);
65+
} else {
66+
happy = true;
67+
}
68+
}
69+
return happy;
70+
}
71+
}
72+
```
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 205\. Isomorphic Strings
5+
6+
Easy
7+
8+
Given two strings `s` and `t`, _determine if they are isomorphic_.
9+
10+
Two strings `s` and `t` are isomorphic if the characters in `s` can be replaced to get `t`.
11+
12+
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
13+
14+
**Example 1:**
15+
16+
**Input:** s = "egg", t = "add"
17+
18+
**Output:** true
19+
20+
**Example 2:**
21+
22+
**Input:** s = "foo", t = "bar"
23+
24+
**Output:** false
25+
26+
**Example 3:**
27+
28+
**Input:** s = "paper", t = "title"
29+
30+
**Output:** true
31+
32+
**Constraints:**
33+
34+
* <code>1 <= s.length <= 5 * 10<sup>4</sup></code>
35+
* `t.length == s.length`
36+
* `s` and `t` consist of any valid ascii character.
37+
38+
## Solution
39+
40+
```csharp
41+
public class Solution {
42+
public bool IsIsomorphic(string s, string t) {
43+
int[] map = new int[128];
44+
char[] str = s.ToCharArray();
45+
char[] tar = t.ToCharArray();
46+
int n = str.Length;
47+
for (int i = 0; i < n; i++) {
48+
if (map[tar[i]] == 0) {
49+
if (Search(map, str[i], tar[i]) != -1) {
50+
return false;
51+
}
52+
map[tar[i]] = str[i];
53+
} else {
54+
if (map[tar[i]] != str[i]) {
55+
return false;
56+
}
57+
}
58+
}
59+
return true;
60+
}
61+
62+
private int Search(int[] map, int tar, int skip) {
63+
for (int i = 0; i < 128; i++) {
64+
if (i == skip) {
65+
continue;
66+
}
67+
if (map[i] != 0 && map[i] == tar) {
68+
return i;
69+
}
70+
}
71+
return -1;
72+
}
73+
}
74+
```

0 commit comments

Comments
 (0)