diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index 9571a7737b..459a66ad8c 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.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.\345\277\253\344\271\220\346\225\260.md" "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" index 741a735a5d..51a79aff96 100644 --- "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" +++ "b/problems/0202.\345\277\253\344\271\220\346\225\260.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.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" index 080166fd34..878b24662c 100644 --- "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" +++ "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.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.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" index 45f19b6ef2..7f8958d232 100644 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.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.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index 5d9e8295c6..6d6efc552b 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -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; + } +``` -----------------------
diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" index a6cd413b19..962fe7a541 100644 --- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" +++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.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; + } +``` -----------------------