diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 9571a773..459a66ad 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -274,6 +274,24 @@ class Solution { } } ``` +C#: +```csharp +public class Solution { + public int[] TwoSum(int[] nums, int target) { + Dictionary dic= new Dictionary(); + for(int i=0;i diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index 741a735a..51a79aff 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -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 set = new HashSet(); + while(n != 1 && !set.Contains(n)) { //判断避免循环 + set.Add(n); + n = getSum(n); + } + return n == 1; + } +} +``` -----------------------
diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 080166fd..878b2466 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -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.赎金信 diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index 45f19b6e..7f8958d2 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -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 one = Insert(nums1); + HashSet two = Insert(nums2); + one.IntersectWith(two); + return one.ToArray(); + } + public HashSet Insert(int[] nums){ + HashSet one = new HashSet(); + foreach(int num in nums){ + one.Add(num); + } + return one; + } +``` ## 相关题目 * 350.两个数组的交集 II diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 6177cc41..933a7c31 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -363,5 +363,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; + } +``` -----------------------
diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md index a6cd413b..962fe7a5 100644 --- a/problems/0454.四数相加II.md +++ b/problems/0454.四数相加II.md @@ -318,5 +318,32 @@ impl Solution { } ``` +C#: +```csharp +public int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { + Dictionary dic = new Dictionary(); + 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; + } +``` -----------------------