Skip to content

Commit 84d34e5

Browse files
committed
Add Code Clear War for starting
1 parent ad70f76 commit 84d34e5

File tree

7 files changed

+159
-8
lines changed

7 files changed

+159
-8
lines changed

Algorithms Basics/Chapter 1. Array_String/1. Two Sum.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ public int[] TwoSum(int[] nums, int target) {
77
var dict = new Dictionary<int, int>();
88
for (int i = 0; i < nums.Length; i++) {
99
var cur = nums[i];
10-
var complement = target - cur;
11-
if (dict.ContainsKey(complement)) {
12-
result = new int[] { dict[complement], i };
10+
var completement = target - cur;
11+
if (dict.ContainsKey(completement)) {
12+
result = new int[] { dict[completement], i };
1313
break;
1414
}
15-
// If using dict.Add(cur, i) will get exception when value cur already exists in dict.
16-
dict[cur] = i;
15+
if (!dict.ContainsKey(cur))
16+
{
17+
dict[cur] = i;
18+
}
1719
}
1820
return result;
1921
}

CodeClearWar/1. Two Sum.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Solution {
2+
public int[] TwoSum(int[] nums, int target) {
3+
int[] result = null;
4+
if (nums == null || nums.Length < 2) {
5+
return result;
6+
}
7+
var dict = new Dictionary<int, int>();
8+
for (int i = 0; i < nums.Length; i++) {
9+
var cur = nums[i];
10+
var completement = target - cur;
11+
if (dict.ContainsKey(completement)) {
12+
result = new int[] { dict[completement], i };
13+
break;
14+
}
15+
if (!dict.ContainsKey(cur)) {
16+
dict[cur] = i;
17+
}
18+
}
19+
return result;
20+
}
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Similar problems:
3+
1. Two Sum
4+
653. Two Sum IV - Input is a BST
5+
*/
6+
public class Solution {
7+
public int[] TwoSum(int[] nums, int target) {
8+
int[] result = null;
9+
if (nums == null || nums.Length < 2) {
10+
return result;
11+
}
12+
for (int i = 0, j = nums.Length - 1; i < j;) {
13+
var sum = nums[i] + nums[j];
14+
if (sum == target) {
15+
result = new int[] { i + 1, j + 1 };
16+
break;
17+
}
18+
if (sum < target) {
19+
i++;
20+
} else {
21+
j--;
22+
}
23+
}
24+
return result;
25+
}
26+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
public class TwoSum {
2+
3+
private List<int> list;
4+
5+
/** Initialize your data structure here. */
6+
public TwoSum() {
7+
this.list = new List<int>();
8+
}
9+
/** Add the number to an internal data structure.. */
10+
public void Add(int number) {
11+
list.Add(number);
12+
}
13+
/** Find if there exists any pair of numbers which sum is equal to the value. */
14+
public bool Find(int value) {
15+
if (list == null || list.Count < 2) {
16+
return false;
17+
}
18+
var set = new HashSet<int>();
19+
for (int i = 0; i < list.Count; i++) {
20+
var cur = list[i];
21+
var completement = value - cur;
22+
if (set.Contains(completement)) {
23+
return true;
24+
}
25+
set.Add(cur);
26+
}
27+
return false;
28+
}
29+
}
30+
31+
/**
32+
* Your TwoSum object will be instantiated and called as such:
33+
* TwoSum obj = new TwoSum();
34+
* obj.Add(number);
35+
* bool param_2 = obj.Find(value);
36+
*/
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
public class Solution {
2+
// 1 BFS and HashSet, iteration
3+
public bool FindTarget(TreeNode root, int k) {
4+
if (root == null) return false;
5+
var set = new HashSet<int>();
6+
var q = new Queue<TreeNode>();
7+
q.Enqueue(root);
8+
while (q.Count > 0) {
9+
var node = q.Dequeue();
10+
var completment = k - node.val;
11+
if (set.Contains(completment)) {
12+
return true;
13+
}
14+
set.Add(node.val);
15+
if (node.left != null) q.Enqueue(node.left);
16+
if (node.right != null) q.Enqueue(node.right);
17+
}
18+
return false;
19+
}
20+
21+
// 2 just HashSet, recursive
22+
public bool FindTarget(TreeNode root, int k) {
23+
var set = new HashSet<int>();
24+
return Find(root, k, set);
25+
}
26+
private bool Find(TreeNode root, int k, HashSet<int> set) {
27+
if (root == null) return false;
28+
if (set.Contains(k - root.val)) return true;
29+
set.Add(root.val);
30+
return Find(root.left, k, set) || Find(root.right, k, set);
31+
}
32+
33+
// 3 Transfer BST to ordering list, then use two pointers to do the twoSum
34+
public bool FindTarget(TreeNode root, int k) {
35+
if (root == null) return false;
36+
List<int> list = new List<int>();
37+
InOrderTranverse(root, list);
38+
for (int i = 0, j = list.Count - 1; i < j;) {
39+
var sum = list[i] + list[j];
40+
if (sum == k) return true;
41+
if (sum < k) {
42+
i++;
43+
} else {
44+
j--;
45+
}
46+
}
47+
return false;
48+
}
49+
private void InOrderTranverse(TreeNode root, List<int> list) {
50+
if (root == null) return;
51+
InOrderTranverse(root.left, list);
52+
list.Add(root.val);
53+
InOrderTranverse(root.right, list);
54+
}
55+
}

LeetcodeCShaprDotNetCore/Solution.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
using System.Collections.Generic;
33
using System.Text;
44

5-
6-
namespace LeetcodeCShaprDotNetCore {
7-
5+
namespace LeetcodeCShaprDotNetCore
6+
{
7+
public class Solution
8+
{
9+
10+
}
811
}

LeetcodeCShaprDotNetCore/TwoSum.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace LeetcodeCShaprDotNetCore
2+
{
3+
using System.Collections.Generic;
4+
public class TwoSum
5+
{
6+
7+
}
8+
}

0 commit comments

Comments
 (0)