Skip to content

Commit

Permalink
新增 哈希表部分的 C#版
Browse files Browse the repository at this point in the history
  • Loading branch information
UndeadSheep committed May 11, 2022
1 parent cf4c25b commit 8a562b7
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 0 deletions.
18 changes: 18 additions & 0 deletions problems/0001.两数之和.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,24 @@ class Solution {
}
}
```
C#:
```csharp
public class Solution {
public int[] TwoSum(int[] nums, int target) {
Dictionary<int ,int> dic= new Dictionary<int,int>();
for(int i=0;i<nums.Length;i++){
int imp= target-nums[i];
if(dic.ContainsKey(imp)&&dic[imp]!=i){
return new int[]{i, dic[imp]};
}
if(!dic.ContainsKey(nums[i])){
dic.Add(nums[i],i);
}
}
return new int[]{0, 0};
}
}
```

-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
23 changes: 23 additions & 0 deletions problems/0202.快乐数.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,5 +385,28 @@ bool isHappy(int n){
return bHappy;
}
```
C#:
```csharp
public class Solution {
private int getSum(int n) {
int sum = 0;
//每位数的换算
while (n > 0) {
sum += (n % 10) * (n % 10);
n /= 10;
}
return sum;
}
public bool IsHappy(int n) {
HashSet <int> set = new HashSet<int>();
while(n != 1 && !set.Contains(n)) { //判断避免循环
set.Add(n);
n = getSum(n);
}
return n == 1;
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
19 changes: 19 additions & 0 deletions problems/0242.有效的字母异位词.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,25 @@ impl Solution {
}
}
```

C#:
```csharp
public bool IsAnagram(string s, string t) {
int sl=s.Length,tl=t.Length;
if(sl!=tl) return false;
int[] a = new int[26];
for(int i = 0; i < sl; i++){
a[s[i] - 'a']++;
a[t[i] - 'a']--;
}
foreach (int i in a)
{
if (i != 0)
return false;
}
return true;
}
```
## 相关题目

* 383.赎金信
Expand Down
18 changes: 18 additions & 0 deletions problems/0349.两个数组的交集.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,24 @@ int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* re
}
```
C#:
```csharp
public int[] Intersection(int[] nums1, int[] nums2) {
if(nums1==null||nums1.Length==0||nums2==null||nums1.Length==0)
return new int[0]; //注意数组条件
HashSet<int> one = Insert(nums1);
HashSet<int> two = Insert(nums2);
one.IntersectWith(two);
return one.ToArray();
}
public HashSet<int> Insert(int[] nums){
HashSet<int> one = new HashSet<int>();
foreach(int num in nums){
one.Add(num);
}
return one;
}
```
## 相关题目

* 350.两个数组的交集 II
Expand Down
17 changes: 17 additions & 0 deletions problems/0383.赎金信.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,5 +361,22 @@ impl Solution {
}
```

C#:
```csharp
public bool CanConstruct(string ransomNote, string magazine) {
if(ransomNote.Length > magazine.Length) return false;
int[] letters = new int[26];
foreach(char c in magazine){
letters[c-'a']++;
}
foreach(char c in ransomNote){
letters[c-'a']--;
if(letters[c-'a']<0){
return false;
}
}
return true;
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
27 changes: 27 additions & 0 deletions problems/0454.四数相加II.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,5 +318,32 @@ impl Solution {
}
```

C#:
```csharp
public int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Dictionary<int, int> dic = new Dictionary<int, int>();
foreach(var i in nums1){
foreach(var j in nums2){
int sum = i + j;
if(dic.ContainsKey(sum)){
dic[sum]++;
}else{
dic.Add(sum, 1);
}

}
}
int res = 0;
foreach(var a in nums3){
foreach(var b in nums4){
int sum = a+b;
if(dic.TryGetValue(-sum, out var result)){
res += result;
}
}
}
return res;
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 comments on commit 8a562b7

Please sign in to comment.