Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cb042be

Browse files
Merge pull request youngyangyang04#1310 from UndeadSheep/master
新增 哈希表部分的 C#版
2 parents f6456c3 + 8a562b7 commit cb042be

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed

‎problems/0001.两数之和.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,24 @@ class Solution {
274274
}
275275
}
276276
```
277+
C#:
278+
```csharp
279+
public class Solution {
280+
public int[] TwoSum(int[] nums, int target) {
281+
Dictionary<int ,int> dic= new Dictionary<int,int>();
282+
for(int i=0;i<nums.Length;i++){
283+
int imp= target-nums[i];
284+
if(dic.ContainsKey(imp)&&dic[imp]!=i){
285+
return new int[]{i, dic[imp]};
286+
}
287+
if(!dic.ContainsKey(nums[i])){
288+
dic.Add(nums[i],i);
289+
}
290+
}
291+
return new int[]{0, 0};
292+
}
293+
}
294+
```
277295

278296
-----------------------
279297
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

‎problems/0202.快乐数.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,5 +385,28 @@ bool isHappy(int n){
385385
return bHappy;
386386
}
387387
```
388+
389+
C#:
390+
```csharp
391+
public class Solution {
392+
private int getSum(int n) {
393+
int sum = 0;
394+
//每位数的换算
395+
while (n > 0) {
396+
sum += (n % 10) * (n % 10);
397+
n /= 10;
398+
}
399+
return sum;
400+
}
401+
public bool IsHappy(int n) {
402+
HashSet <int> set = new HashSet<int>();
403+
while(n != 1 && !set.Contains(n)) { //判断避免循环
404+
set.Add(n);
405+
n = getSum(n);
406+
}
407+
return n == 1;
408+
}
409+
}
410+
```
388411
-----------------------
389412
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

‎problems/0242.有效的字母异位词.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,25 @@ impl Solution {
307307
}
308308
}
309309
```
310+
311+
C#:
312+
```csharp
313+
public bool IsAnagram(string s, string t) {
314+
int sl=s.Length,tl=t.Length;
315+
if(sl!=tl) return false;
316+
int[] a = new int[26];
317+
for(int i = 0; i < sl; i++){
318+
a[s[i] - 'a']++;
319+
a[t[i] - 'a']--;
320+
}
321+
foreach (int i in a)
322+
{
323+
if (i != 0)
324+
return false;
325+
}
326+
return true;
327+
}
328+
```
310329
## 相关题目
311330

312331
* 383.赎金信

‎problems/0349.两个数组的交集.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,24 @@ int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* re
313313
}
314314
```
315315
316+
C#:
317+
```csharp
318+
public int[] Intersection(int[] nums1, int[] nums2) {
319+
if(nums1==null||nums1.Length==0||nums2==null||nums1.Length==0)
320+
return new int[0]; //注意数组条件
321+
HashSet<int> one = Insert(nums1);
322+
HashSet<int> two = Insert(nums2);
323+
one.IntersectWith(two);
324+
return one.ToArray();
325+
}
326+
public HashSet<int> Insert(int[] nums){
327+
HashSet<int> one = new HashSet<int>();
328+
foreach(int num in nums){
329+
one.Add(num);
330+
}
331+
return one;
332+
}
333+
```
316334
## 相关题目
317335

318336
* 350.两个数组的交集 II

‎problems/0383.赎金信.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,5 +363,22 @@ impl Solution {
363363
}
364364
```
365365

366+
C#:
367+
```csharp
368+
public bool CanConstruct(string ransomNote, string magazine) {
369+
if(ransomNote.Length > magazine.Length) return false;
370+
int[] letters = new int[26];
371+
foreach(char c in magazine){
372+
letters[c-'a']++;
373+
}
374+
foreach(char c in ransomNote){
375+
letters[c-'a']--;
376+
if(letters[c-'a']<0){
377+
return false;
378+
}
379+
}
380+
return true;
381+
}
382+
```
366383
-----------------------
367384
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

‎problems/0454.四数相加II.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,5 +318,32 @@ impl Solution {
318318
}
319319
```
320320

321+
C#:
322+
```csharp
323+
public int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
324+
Dictionary<int, int> dic = new Dictionary<int, int>();
325+
foreach(var i in nums1){
326+
foreach(var j in nums2){
327+
int sum = i + j;
328+
if(dic.ContainsKey(sum)){
329+
dic[sum]++;
330+
}else{
331+
dic.Add(sum, 1);
332+
}
333+
334+
}
335+
}
336+
int res = 0;
337+
foreach(var a in nums3){
338+
foreach(var b in nums4){
339+
int sum = a+b;
340+
if(dic.TryGetValue(-sum, out var result)){
341+
res += result;
342+
}
343+
}
344+
}
345+
return res;
346+
}
347+
```
321348
-----------------------
322349
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /