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 3436486

Browse files
Add C# solutions
1 parent 98d7b74 commit 3436486

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Solution {
2+
public string GetHint(string secret, string guess) {
3+
var bulls = 0;
4+
var cows = 0;
5+
int[] secretarr = new int[10];
6+
int[] guessarr = new int[10];
7+
for(int i=0; i<secret.Length; i++){
8+
if(secret[i] == guess[i]){
9+
bulls++;
10+
}else {
11+
++secretarr[secret[i] - '0'];
12+
++guessarr[guess[i] - '0'];
13+
}
14+
}
15+
for (int i = 0; i < 10; ++i) {
16+
cows += Math.Min(secretarr[i], guessarr[i]);
17+
}
18+
return bulls + "A" + cows + "B";
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class Solution {
2+
public int MaxProduct(int[] nums)
3+
{
4+
int s = nums[0];
5+
int r = nums[0];
6+
7+
for(int i=1, max = s, min = s; i<nums.Length; i++){
8+
int temp = max;
9+
max = Math.Max(Math.Max(nums[i] * max, nums[i] * min), nums[i]);
10+
min = Math.Min(Math.Min(nums[i] * temp, nums[i] * min), nums[i]);
11+
12+
r = Math.Max(max, r);
13+
}
14+
15+
return r;
16+
}
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class Solution {
2+
public IList<IList<int>> CombinationSum3(int k, int n)
3+
{
4+
var res = new List<IList<int>>();
5+
var tmp = new List<int>();
6+
Backtrack(res, tmp, k, n, 1);
7+
return res;
8+
}
9+
10+
public void Backtrack(List<IList<int>> res, List<int> tmp, int k, int n, int idx)
11+
{
12+
if (k == tmp.Count && n == 0)
13+
{
14+
res.Add(new List<int>(tmp)); //Deep clone
15+
return;
16+
}
17+
18+
for (int i = idx; i <= n && k > 0 && n > 0; i++)
19+
{
20+
if(i>9) break;
21+
tmp.Add(i);
22+
Backtrack(res, tmp, k, n - i, i + 1);
23+
tmp.RemoveAt(tmp.Count - 1); //Remove last item when it doesn't find the correct combo
24+
}
25+
}
26+
}

0 commit comments

Comments
(0)

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