Skip to content

Commit afec845

Browse files
committed
Update code due to update of test cases and interfaces
1 parent 39cb397 commit afec845

24 files changed

+246
-237
lines changed

1.TwoSum.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public int[] TwoSum(int[] nums, int target) {
88
int index;
99
if (dict.TryGetValue(target - nums[i], out index))
1010
{
11-
return new [] { index + 1, i + 1};
11+
return new [] { index, i};
1212
}
1313
if (!dict.ContainsKey(nums[i]))
1414
{

127.WordLadder.cs

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,66 @@
33
using System.Linq;
44

55
public class Solution {
6-
public int LadderLength(string beginWord, string endWord, ISet<string> wordDict) {
7-
if (beginWord == endWord) return 1;
8-
wordDict.Remove(beginWord);
9-
wordDict.Remove(endWord);
10-
var words = new [] { beginWord, endWord }.Concat(wordDict.Where(word => word.Length == beginWord.Length)).Select((word, i) => new { Word = word, Index = i }).ToList();
11-
12-
var paths = new List<int>[words.Count];
13-
for (var i = 0; i < paths.Length; ++i)
14-
{
15-
paths[i] = new List<int>();
16-
}
17-
for (var i = 0; i < beginWord.Length; ++i)
18-
{
6+
public int LadderLength(string beginWord, string endWord, IList<string> wordList) {
7+
var words = Enumerable.Repeat(beginWord, 1).Concat(wordList).Select((word, i) => new { Word = word, Index = i }).ToList();
8+
var endWordIndex = words.Find(w => w.Word == endWord)?.Index;
9+
if (endWordIndex == null) {
10+
return 0;
11+
}
12+
13+
var paths = new List<int>[words.Count];
14+
for (var i = 0; i < paths.Length; ++i)
15+
{
16+
paths[i] = new List<int>();
17+
}
18+
for (var i = 0; i < beginWord.Length; ++i)
19+
{
1920
var hashMap = new Hashtable();
2021
foreach (var item in words)
2122
{
2223
var newWord = string.Format("{0}_{1}", item.Word.Substring(0, i), item.Word.Substring(i + 1));
23-
List<int> similars;
24+
List<int> similars;
2425
if (!hashMap.ContainsKey(newWord))
25-
{
26-
similars = new List<int>();
27-
hashMap.Add(newWord, similars);
28-
}
29-
else
30-
{
31-
similars = (List<int>)hashMap[newWord];
32-
}
33-
foreach (var similar in similars)
34-
{
35-
paths[similar].Add(item.Index);
36-
paths[item.Index].Add(similar);
37-
}
26+
{
27+
similars = new List<int>();
28+
hashMap.Add(newWord, similars);
29+
}
30+
else
31+
{
32+
similars = (List<int>)hashMap[newWord];
33+
}
34+
foreach (var similar in similars)
35+
{
36+
paths[similar].Add(item.Index);
37+
paths[item.Index].Add(similar);
38+
}
3839
similars.Add(item.Index);
3940
}
40-
}
41-
42-
var left = words.Count - 1;
43-
var lastRound = new List<int> { 0 };
44-
var visited = new bool[words.Count];
45-
visited[0] = true;
46-
for (var result = 2; left > 0; ++result)
47-
{
48-
var thisRound = new List<int>();
49-
foreach (var index in lastRound)
50-
{
51-
foreach (var next in paths[index])
52-
{
53-
if (!visited[next])
54-
{
55-
visited[next] = true;
56-
if (next == 1) return result;
57-
thisRound.Add(next);
58-
}
59-
}
60-
}
41+
}
42+
43+
var left = words.Count - 1;
44+
var lastRound = new List<int> { 0 };
45+
var visited = new bool[words.Count];
46+
visited[0] = true;
47+
for (var result = 2; left > 0; ++result)
48+
{
49+
var thisRound = new List<int>();
50+
foreach (var index in lastRound)
51+
{
52+
foreach (var next in paths[index])
53+
{
54+
if (!visited[next])
55+
{
56+
visited[next] = true;
57+
if (next == endWordIndex) return result;
58+
thisRound.Add(next);
59+
}
60+
}
61+
}
6162
if (thisRound.Count == 0) break;
62-
lastRound = thisRound;
63-
}
64-
65-
return 0;
63+
lastRound = thisRound;
64+
}
65+
66+
return 0;
6667
}
6768
}

130.SurroundedRegions.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
using System.Collections.Generic;
33

44
public class Solution {
5-
private static readonly int[,] directions = new int[4, 2] { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 }};
6-
public void Solve(char[,] board) {
7-
var lenI = board.GetLength(0);
8-
var lenJ = board.GetLength(1);
9-
5+
private static readonly int[,] directions = new int[4, 2] { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 }};
6+
public void Solve(char[][] board) {
7+
var lenI = board.Length;
8+
var lenJ = lenI == 0 ? 0 : board[0].Length;
9+
1010
for (var i = 0; i < lenI; ++i)
1111
{
1212
for (var j = 0; j < lenJ; ++j)
1313
{
14-
if (board[i, j] == 'O')
14+
if (board[i][j] == 'O')
1515
{
1616
var marked = new List<Tuple<int, int>>();
1717
marked.Add(Tuple.Create(i, j));
18-
board[i, j] = 'M';
18+
board[i][j] = 'M';
1919
bool escaped = false;
2020
for (var m = 0; m < marked.Count; ++m)
2121
{
@@ -27,32 +27,32 @@ public void Solve(char[,] board) {
2727
{
2828
escaped = true;
2929
}
30-
else if (board[newI, newJ] == 'O')
30+
else if (board[newI][newJ] == 'O')
3131
{
32-
board[newI, newJ] = 'M';
32+
board[newI][newJ] = 'M';
3333
marked.Add(Tuple.Create(newI, newJ));
3434
}
3535
}
3636
}
37-
37+
3838
if (!escaped)
3939
{
4040
foreach (var item in marked)
4141
{
42-
board[item.Item1, item.Item2] = 'X';
42+
board[item.Item1][item.Item2] = 'X';
4343
}
4444
}
4545
}
4646
}
4747
}
48-
48+
4949
for (var i = 0; i < lenI; ++i)
5050
{
5151
for (var j = 0; j < lenJ; ++j)
5252
{
53-
if (board[i, j] == 'M')
53+
if (board[i][j] == 'M')
5454
{
55-
board[i, j] = 'O';
55+
board[i][j] = 'O';
5656
}
5757
}
5858
}

133.CloneGraph.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
11
using System.Collections.Generic;
22

33
public class Solution {
4-
public UndirectedGraphNode CloneGraph(UndirectedGraphNode node) {
4+
public Node CloneGraph(Node node) {
55
if (node == null) return null;
6-
var dict = new Dictionary<int, UndirectedGraphNode>();
7-
var queue = new Queue<UndirectedGraphNode>();
8-
queue.Enqueue(CloneLabel(node));
9-
dict.Add(node.label, queue.Peek());
6+
var dict = new Dictionary<int, Node>();
7+
var queue = new Queue<Node>();
8+
queue.Enqueue(CloneVal(node));
9+
dict.Add(node.val, queue.Peek());
1010
while (queue.Count > 0)
1111
{
1212
var current = queue.Dequeue();
13-
var newNeighbors = new List<UndirectedGraphNode>(current.neighbors.Count);
13+
var newNeighbors = new List<Node>(current.neighbors.Count);
1414
foreach (var oldNeighbor in current.neighbors)
1515
{
16-
UndirectedGraphNode newNeighbor;
17-
if (!dict.TryGetValue(oldNeighbor.label, out newNeighbor))
16+
Node newNeighbor;
17+
if (!dict.TryGetValue(oldNeighbor.val, out newNeighbor))
1818
{
19-
newNeighbor = CloneLabel(oldNeighbor);
19+
newNeighbor = CloneVal(oldNeighbor);
2020
queue.Enqueue(newNeighbor);
21-
dict.Add(newNeighbor.label, newNeighbor);
21+
dict.Add(newNeighbor.val, newNeighbor);
2222
}
2323
newNeighbors.Add(newNeighbor);
2424
}
2525
current.neighbors = newNeighbors;
2626
}
27-
return dict[node.label];
27+
return dict[node.val];
2828
}
2929

30-
private UndirectedGraphNode CloneLabel(UndirectedGraphNode node)
30+
private Node CloneVal(Node node)
3131
{
32-
return new UndirectedGraphNode(node.label)
33-
{
34-
neighbors = new List<UndirectedGraphNode>(node.neighbors)
35-
};
32+
return new Node(node.val, new List<Node>(node.neighbors));
3633
}
3734
}

138.CopyListWithRandomPointer.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,30 @@
22
using System.Collections.Generic;
33

44
public class Solution {
5-
public RandomListNode CopyRandomList(RandomListNode head) {
6-
var dict = new Dictionary<int, Tuple<int?, int?>>();
5+
public Node CopyRandomList(Node head) {
6+
if (head == null) {
7+
return null;
8+
}
9+
10+
var map = new Dictionary<Node, Node>();
711
var current = head;
8-
while (current != null)
9-
{
10-
dict.Add(current.label, Tuple.Create(current.next == null ? (int?) null : current.next.label, current.random == null ? (int?) null : current.random.label));
12+
while (current != null) {
13+
var newCurrent = new Node(current.val);
14+
map.Add(current, newCurrent);
1115
current = current.next;
1216
}
1317

14-
var dict2 = new Dictionary<int, RandomListNode>();
15-
foreach (var label in dict.Keys)
16-
{
17-
dict2.Add(label, new RandomListNode(label));
18-
}
19-
foreach (var pair in dict)
20-
{
21-
var next = pair.Value.Item1;
22-
if (next.HasValue) dict2[pair.Key].next = dict2[next.Value];
23-
var random = pair.Value.Item2;
24-
if (random.HasValue) dict2[pair.Key].random = dict2[random.Value];
18+
foreach (var entry in map) {
19+
var oldNode = entry.Key;
20+
var newNode = entry.Value;
21+
if (oldNode.next != null) {
22+
newNode.next = map[oldNode.next];
23+
}
24+
if (oldNode.random != null) {
25+
newNode.random = map[oldNode.random];
26+
}
2527
}
2628

27-
return head == null ? null : dict2[head.label];
29+
return map[head];
2830
}
2931
}

139.WordBreak.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Linq;
33

44
public class Solution {
5-
public bool WordBreak(string s, ISet<string> wordDict) {
5+
public bool WordBreak(string s, IList<string> wordDict) {
66
var f = new bool[s.Length + 1];
77
f[0] = true;
88
var wordDictGroup = wordDict.GroupBy(word => word.Length);

140.WordBreakII.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Node
1010
}
1111

1212
public class Solution {
13-
public IList<string> WordBreak(string s, ISet<string> wordDict) {
13+
public IList<string> WordBreak(string s, IList<string> wordDict) {
1414
var paths = new List<Tuple<int, string>>[s.Length + 1];
1515
paths[s.Length] = new List<Tuple<int, string>> { Tuple.Create(-1, (string)null) };
1616
var wordDictGroup = wordDict.GroupBy(word => word.Length);
@@ -32,14 +32,14 @@ public IList<string> WordBreak(string s, ISet<string> wordDict) {
3232
}
3333
}
3434
}
35-
35+
3636
return GenerateResults(paths);
3737
}
38-
38+
3939
private IList<string> GenerateResults(List<Tuple<int, string>>[] paths)
4040
{
4141
var results = new List<string>();
42-
var sb = new StringBuilder();
42+
var sb = new StringBuilder();
4343
var stack = new Stack<Node>();
4444
stack.Push(new Node());
4545
while (stack.Count > 0)

146.LRUCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public int Get(int key)
3434
return -1;
3535
}
3636

37-
public void Set(int key, int value)
37+
public void Put(int key, int value)
3838
{
3939
Node node;
4040
if (keyMap.TryGetValue(key, out node))

0 commit comments

Comments
 (0)