Skip to content

Commit d013307

Browse files
committed
Add Similar Questions or Next challenges
1 parent 84d34e5 commit d013307

File tree

11 files changed

+250
-59
lines changed

11 files changed

+250
-59
lines changed

CodeClearWar/1. Two Sum.cs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,42 @@
1+
/*
2+
Solution tip:
3+
Just use dictionary. For each cur = nums[i], check if complement = target-nums[i] exists in the dictionary.
4+
if exists, return (dic[complement], i);
5+
if not, put cur into dictionary.
6+
7+
The core approach for all the two sums, is re-mapping the <key,value>.
8+
9+
1. Before re-mapping, if we need find a value(target - cur) in the nums[i], we should iterate all the items, O(n);
10+
2. For re-mapping, we create a dictinary with sapce O(n), then set the value as key, the index i as value.
11+
3. After re-mapping, if we need find the key, value(target - cur), in dictionary, we just need O(1) time
12+
to get the value, index.
13+
14+
That's it! Just do re-mapping, trade off is O(n) space vs O(n) time.
15+
16+
*/
117
public class Solution {
218
public int[] TwoSum(int[] nums, int target) {
3-
int[] result = null;
4-
if (nums == null || nums.Length < 2) {
5-
return result;
6-
}
719
var dict = new Dictionary<int, int>();
820
for (int i = 0; i < nums.Length; i++) {
921
var cur = nums[i];
10-
var completement = target - cur;
11-
if (dict.ContainsKey(completement)) {
12-
result = new int[] { dict[completement], i };
13-
break;
22+
var complement = target - cur;
23+
if (dict.ContainsKey(complement)) {
24+
return result = new int[] { dict[complement], i };
1425
}
1526
if (!dict.ContainsKey(cur)) {
1627
dict[cur] = i;
1728
}
1829
}
19-
return result;
30+
throw new Exception("No two sum solution");
2031
}
21-
}
32+
}
33+
34+
/*
35+
Similar Questions or Next challenges:
36+
3Sum
37+
4Sum
38+
Two Sum II - Input array is sorted
39+
Two Sum III - Data structure design
40+
Subarray Sum Equals K
41+
Two Sum IV - Input is a BST
42+
*/

CodeClearWar/15. 3Sum.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// sort array, then loop each index, do 2Sum in [index + 1, ..., end]
2+
public class Solution {
3+
public IList<IList<int>> ThreeSum(int[] nums) {
4+
IList<IList<int>> res = new List<IList<int>>();
5+
if (nums == null || nums.Length < 3) return res;
6+
Array.Sort(nums);
7+
for (int i = 0; i < nums.Length - 2; i++) {
8+
if (i > 0 && nums[i] == nums[i - 1]) continue;
9+
int l = i + 1, r = nums.Length - 1;
10+
int target = 0 - nums[i];
11+
while (l < r) {
12+
int v = nums[l] + nums[r];
13+
if (v < target) {
14+
l++;
15+
} else if (v > target) {
16+
r--;
17+
} else {
18+
res.Add(new List<int>() { nums[i], nums[l], nums[r] });
19+
while (l < r && nums[l] == nums[l + 1]) l++;
20+
while (l < r && nums[r] == nums[r - 1]) r--;
21+
l++;
22+
r--;
23+
}
24+
}
25+
}
26+
return res;
27+
}
28+
}
29+
30+
// abstract TwoSum method
31+
public class Solution {
32+
public IList<IList<int>> ThreeSum(int[] nums) {
33+
var results = new List<IList<int>>();
34+
if (nums == null || nums.Length < 3) return results;
35+
Array.Sort(nums);
36+
for (int i = 0; i < nums.Length - 2; i++) {
37+
if (nums[i] > 0) return results;
38+
if (i > 0 && nums[i] == nums[i - 1]) continue;
39+
var left = i + 1;
40+
var right = nums.Length - 1;
41+
var target = 0 - nums[i];
42+
TwoSum(nums, left, right, target, results);
43+
}
44+
return results;
45+
}
46+
47+
private void TwoSum(int[] nums, int left, int right, int target, List<IList<int>> results) {
48+
while (left < right) {
49+
var v = nums[left] + nums[right];
50+
if (v < target) left++;
51+
else if (v > target) right--;
52+
else {
53+
results.Add(new List<int>(){-target, nums[left], nums[right]});
54+
while (left < right && nums[left] == nums[left + 1]) left++;
55+
while (left < right && nums[right] == nums[right - 1]) right--;
56+
left++;
57+
right--;
58+
}
59+
}
60+
}
61+
}
62+
63+
/*
64+
Next challenges:
65+
3Sum Closest
66+
4Sum
67+
3Sum Smaller
68+
*/
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
/*
2-
Similar problems:
3-
1. Two Sum
4-
653. Two Sum IV - Input is a BST
2+
Solution tip:
3+
Just use two pointers.
4+
55
*/
66
public class Solution {
77
public int[] TwoSum(int[] nums, int target) {
8-
int[] result = null;
9-
if (nums == null || nums.Length < 2) {
10-
return result;
11-
}
128
for (int i = 0, j = nums.Length - 1; i < j;) {
139
var sum = nums[i] + nums[j];
1410
if (sum == target) {
15-
result = new int[] { i + 1, j + 1 };
16-
break;
11+
return new int[] { i + 1, j + 1 };
1712
}
1813
if (sum < target) {
1914
i++;
2015
} else {
2116
j--;
2217
}
2318
}
24-
return result;
19+
throw new Exception("No two sum solution");
2520
}
26-
}
21+
}
22+
23+
/*
24+
Next challenges:
25+
Find the Duplicate Number
26+
Maximum Product of Three Numbers
27+
Maximum Swap
28+
*/
Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
Tip:
3+
See 1. Two sums. for the intrinsic thought and approch. re-Mapping <key, value>.
4+
1. Add time O(1), Find time O(n);
5+
*/
16
public class TwoSum {
27

38
private List<int> list;
@@ -12,14 +17,11 @@ public void Add(int number) {
1217
}
1318
/** Find if there exists any pair of numbers which sum is equal to the value. */
1419
public bool Find(int value) {
15-
if (list == null || list.Count < 2) {
16-
return false;
17-
}
1820
var set = new HashSet<int>();
1921
for (int i = 0; i < list.Count; i++) {
2022
var cur = list[i];
21-
var completement = value - cur;
22-
if (set.Contains(completement)) {
23+
var complement = value - cur;
24+
if (set.Contains(complement)) {
2325
return true;
2426
}
2527
set.Add(cur);
@@ -28,9 +30,39 @@ public bool Find(int value) {
2830
}
2931
}
3032

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);
33+
/*
34+
2. Add time O(n), Find time O(1);
35+
*/
36+
public class TwoSum {
37+
private List<int> num;
38+
private HashSet<int> sum;
39+
40+
/** Initialize your data structure here. */
41+
public TwoSum() {
42+
num = new List<int>();
43+
sum = new HashSet<int>();
44+
}
45+
46+
/** Add the number to an internal data structure.. */
47+
public void Add(int number) {
48+
if (num.Contains(number)) {
49+
// no need to iterate other num for adding
50+
sum.Add(number * 2);
51+
} else {
52+
foreach(var v in num) {
53+
sum.Add(v + number);
54+
}
55+
num.Add(number);
56+
}
57+
}
58+
59+
/** Find if there exists any pair of numbers which sum is equal to the value. */
60+
public bool Find(int value) {
61+
return sum.Contains(value);
62+
}
63+
}
64+
65+
/*
66+
Next challenges:
67+
Unique Word Abbreviation
3668
*/

CodeClearWar/653. Two Sum IV - Input is a BST.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ public bool FindTarget(TreeNode root, int k) {
77
q.Enqueue(root);
88
while (q.Count > 0) {
99
var node = q.Dequeue();
10-
var completment = k - node.val;
11-
if (set.Contains(completment)) {
10+
var complement = k - node.val;
11+
if (set.Contains(complement)) {
1212
return true;
1313
}
1414
set.Add(node.val);
@@ -25,31 +25,35 @@ public bool FindTarget(TreeNode root, int k) {
2525
}
2626
private bool Find(TreeNode root, int k, HashSet<int> set) {
2727
if (root == null) return false;
28-
if (set.Contains(k - root.val)) return true;
28+
var complement = k - root.val;
29+
if (set.Contains(complement)) return true;
2930
set.Add(root.val);
3031
return Find(root.left, k, set) || Find(root.right, k, set);
3132
}
3233

3334
// 3 Transfer BST to ordering list, then use two pointers to do the twoSum
3435
public bool FindTarget(TreeNode root, int k) {
3536
if (root == null) return false;
36-
List<int> list = new List<int>();
37+
var list = new List<int>();
3738
InOrderTranverse(root, list);
3839
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-
}
40+
var v = list[i] + list[j];
41+
if (v == k) return true;
42+
else if (v < k) i++;
43+
else j--;
4644
}
4745
return false;
4846
}
4947
private void InOrderTranverse(TreeNode root, List<int> list) {
50-
if (root == null) return;
51-
InOrderTranverse(root.left, list);
48+
if (root.left != null) InOrderTranverse(root.left, list);
5249
list.Add(root.val);
53-
InOrderTranverse(root.right, list);
50+
if (root.right != null) InOrderTranverse(root.right, list);
5451
}
55-
}
52+
}
53+
54+
/*
55+
Next challenges:
56+
Serialize and Deserialize BST
57+
Maximum Binary Tree
58+
Print Binary Tree
59+
*/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": ".NET Core Launch (console)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/LeetcodeCShaprDotNetCore.dll",
13+
"args": [],
14+
"cwd": "${workspaceFolder}",
15+
"console": "internalConsole",
16+
"stopAtEntry": false,
17+
"internalConsoleOptions": "openOnSessionStart"
18+
},
19+
{
20+
"name": ".NET Core Attach",
21+
"type": "coreclr",
22+
"request": "attach",
23+
"processId": "${command:pickProcess}"
24+
}
25+
]
26+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/LeetcodeCShaprDotNetCore.csproj"
11+
],
12+
"problemMatcher": "$msCompile"
13+
}
14+
]
15+
}

LeetcodeCShaprDotNetCore/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
namespace LeetcodeCShaprDotNetCore {
44
class Program {
55
static void Main(string[] args) {
6-
6+
77
}
8+
89
}
910
}

LeetcodeCShaprDotNetCore/Solution.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@
44

55
namespace LeetcodeCShaprDotNetCore
66
{
7-
public class Solution
8-
{
9-
10-
}
7+
118
}

LeetcodeCShaprDotNetCore/TwoSum.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)