public void TestMethod1() { // [[1,1],2,[1,1]] List <NestedInteger> list = new List <NestedInteger>(); NestedInteger ni = new NestedInteger(); ni.Add(new NestedInteger(1)); ni.Add(new NestedInteger(1)); list.Add(ni); list.Add(new NestedInteger(2)); ni = new NestedInteger(); ni.Add(new NestedInteger(1)); ni.Add(new NestedInteger(1)); list.Add(ni); NestedIterator nIterator = new NestedIterator(list); LinkedList <int> elements = new LinkedList <int>(); while (nIterator.HasNext()) { elements.AddLast(nIterator.Next()); } CollectionAssert.AreEquivalent(elements.ToArray(), new int[] { 1, 1, 2, 1, 1 }); }
public void Test2() { var integers = new List <NestedInteger> { new NestedInteger(new List <NestedInteger>()) }; var sut = new NestedIterator(integers); Assert.IsFalse(sut.HasNext()); }
// 這篇有Iterator pattern 的非常詳細解釋 public static void Test() { Solution s = new Solution(); //Console.WriteLine(s.NumPoints()); List <NestedInteger> ls = new List <NestedInteger>() { new NestedIntegerImpl(null, 1), new NestedIntegerImpl(new List <NestedInteger>() { new NestedIntegerImpl(null, 2), new NestedIntegerImpl(null, 3), new NestedIntegerImpl(new List <NestedInteger>() { new NestedIntegerImpl(null, 31), new NestedIntegerImpl(null, 32), new NestedIntegerImpl(new List <NestedInteger>() { new NestedIntegerImpl(null, 311), new NestedIntegerImpl(null, 312), }, null), }, null), }, null), new NestedIntegerImpl(null, 4), new NestedIntegerImpl(new List <NestedInteger>() { new NestedIntegerImpl(null, 5), new NestedIntegerImpl(null, 6), }, null), }; NestedIterator it = new NestedIterator(ls); List <NestedInteger> ls2 = new List <NestedInteger>() { new NestedIntegerImpl(null, 1), new NestedIntegerImpl(new List <NestedInteger>() { new NestedIntegerImpl(null, 4), new NestedIntegerImpl(new List <NestedInteger>() { new NestedIntegerImpl(null, 6), }, null), }, null), }; //NestedIterator_Sol2 it2 = new NestedIterator_Sol2(ls); NestedIterator_Sol5 it2 = new NestedIterator_Sol5(ls); while (it2.HasNext()) { Console.WriteLine(it2.Next()); } //Console.WriteLine(s.SpecialArray(new int[] { 3, 5 })); }
private void Move() { while (cur < NestedList.Count) { if (NestedList[cur].IsInteger()) { break; } Iterator = new NestedIterator(NestedList[cur].GetList()); if (Iterator.HasNext()) { break; } cur += 1; } }
public void Test1() { var expected = new[] { 1, 2, 3, 4 }; var index = 0; var integers = new List <NestedInteger> { new NestedInteger(1), new NestedInteger(new List <NestedInteger> { new NestedInteger(2), new NestedInteger(3), }), new NestedInteger(4) }; var sut = new NestedIterator(integers); while (sut.HasNext()) { Assert.AreEqual(expected[index++], sut.Next()); } }
public void TestMethod2() { List <NestedInteger> list = new List <NestedInteger>(); NestedInteger ni = new NestedInteger(1); list.Add(ni); ni = new NestedInteger(); ni.Add(new NestedInteger(4)); var ni2 = new NestedInteger(); ni2.Add(new NestedInteger(6)); ni.Add(ni2); list.Add(ni); NestedIterator nIterator = new NestedIterator(list); LinkedList <int> elements = new LinkedList <int>(); while (nIterator.HasNext()) { elements.AddLast(nIterator.Next()); } CollectionAssert.AreEquivalent(elements.ToArray(), new int[] { 1, 4, 6 }); }
public bool HasNext() { while (Index < NestedList.Count) { if (Sub != null) { if (Sub.HasNext()) { return(true); } else { Sub = null; } } var curr = NestedList[Index++]; if (curr.IsInteger()) { return(true); } Sub = new NestedIterator(curr.GetList()); } return(Sub != null && Sub.HasNext()); }
public NestedIterator(IList <NestedInteger> nestedList) { NestedList = nestedList; Index = 0; Sub = null; }