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 NestedIterator_2() { var input3 = new NestedInteger(); input3.Add(new NestedInteger(6)); var input2 = new NestedInteger(); input2.Add(new NestedInteger(4)); input2.Add(input3); var input1 = new NestedInteger(1); var solution = new _0341_FlattenNestedListIterator(new List <NestedInteger>() { input1, input2 }); Assert.IsTrue(solution.HasNext()); Assert.AreEqual(1, solution.Next()); Assert.IsTrue(solution.HasNext()); Assert.AreEqual(4, solution.Next()); Assert.IsTrue(solution.HasNext()); Assert.AreEqual(6, solution.Next()); Assert.IsFalse(solution.HasNext()); }
private NestedInteger Recursive(string s, int curIndex, out int nextIndex) { nextIndex = curIndex + 1; var forReturn = new NestedInteger(); var numTemp = 0; for (int i = curIndex; i < s.Length; i++) { var curChar = s[i]; switch (curChar) { case '[': var r = Recursive(s, i + 1, out int ni); if (r.GetList() == null) { forReturn.Add(r); } else { foreach (var item in r.GetList()) { forReturn.Add(item); } } i = ni - 1; break; case ']': nextIndex = i + 1; return(forReturn); case ',': if (numTemp != 0) { forReturn.Add(new NestedInteger(numTemp)); numTemp = 0; } break; case '-': numTemp *= -1; break; default: numTemp = numTemp * 10 + int.Parse(curChar.ToString()); break; } } if (numTemp != 0) { forReturn.SetInteger(numTemp); } return(forReturn); }
public NestedInteger Deserialize(string s) { NestedInteger ret = new NestedInteger(); if (s == null || s.Length == 0) { return(ret); } if (s[0] != '[') { ret.SetInteger(int.Parse(s)); } else if (s.Length > 2) { int start = 1, count = 0; for (int i = 1; i < s.Length; i++) { char c = s[i]; if (count == 0 && (c == ',' || i == s.Length - 1)) { ret.Add(Deserialize(s.Substring(start, i - start))); start = i + 1; } else if (c == '[') { count++; } else if (c == ']') { count--; } } } return(ret); }
public NestedInteger Deserialize(string s) { var parent = new NestedInteger(); var stack = new Stack <NestedInteger>(); int?num = null; int sign = 1; foreach (char c in s) { if (c == '[') { var list = new NestedInteger(); parent.Add(list); stack.Push(parent); parent = list; } else if (c == ']' || c == ',') { if (num.HasValue) { parent.Add(new NestedInteger(num.Value * sign)); num = null; sign = 1; } if (c == ']') { parent = stack.Pop(); } } else if (c == '-') { sign = -1; } else { num ??= 0; num = num * 10 + (c - '0'); } } if (num.HasValue) { parent.Add(new NestedInteger(num.Value * sign)); } return(parent.GetList()[0]); }
public NestedInteger DeserializeImpl() { var ni = new NestedInteger(); int num = 0; bool isNegative = false; while (index < charArray.Length - 1) { index++; var c = charArray[index]; if (c == '[') { ni.Add(DeserializeImpl()); } else if (c == ']') { return(ni); } else if (c == ',') { continue; } else if (c == '-') { isNegative = true; } else { num = 10 * num + (c - '0'); if (!char.IsDigit(charArray[index + 1])) { if (isNegative) { num = -num; } ni.Add(new NestedInteger(num)); num = 0; isNegative = false; } } } return(null); }
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); Solution s = new Solution(); var result = s.DepthSumInverse(list); Assert.AreEqual(result, 17); }
public void DepthSum_2() { var input3 = new NestedInteger(); input3.Add(new NestedInteger(6)); var input2 = new NestedInteger(); input2.Add(new NestedInteger(4)); input2.Add(input3); var input1 = new NestedInteger(1); var solution = new _0339_NestedListWeightSum(); var result = solution.DepthSum(new List <NestedInteger>() { input1, input2 }); Assert.AreEqual(27, result); }
public void DepthSum_1() { var input1 = new NestedInteger(); input1.Add(new NestedInteger(1)); input1.Add(new NestedInteger(1)); var input2 = new NestedInteger(2); var input3 = new NestedInteger(); input3.Add(new NestedInteger(1)); input3.Add(new NestedInteger(1)); var solution = new _0339_NestedListWeightSum(); var result = solution.DepthSum(new List <NestedInteger>() { input1, input2, input3 }); Assert.AreEqual(10, result); }
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); Solution s = new Solution(); var result = s.DepthSumInverse(list); Assert.AreEqual(result, 8); }
private NestedInteger Dfs(object item, NestedInteger current) { if (item is int) { current.SetInteger((int)item); return(current); } foreach (var obj in (object[])item) { current.Add(Dfs(obj, new NestedInteger())); } return(current); }
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 NestedInteger Deserialize(string s) { if (s == "[]") { return(new NestedInteger()); } var rs = new NestedInteger(); if (s[0] == '[') { var splitList = SplitString(s); foreach (var str in splitList) { rs.Add(Deserialize(str)); } } else { rs.SetInteger(int.Parse(s)); } return(rs); }
public NestedInteger Deserialize(string s) { if (!s.StartsWith('[')) { int i = 0; int number = ReadNumber(s, ref i); return(new NestedInteger(number)); } s = "[" + s + "]"; Stack <char> ops = new Stack <char>(); Stack <int> operands = new Stack <int>(); int pos = 0; NestedInteger result = null; while (pos < s.Length) { if (s[pos] == '[') { ops.Push('['); pos++; } else if (IsDigit(s[pos]) || s[pos] == '-') { int number = ReadNumber(s, ref pos); operands.Push(number); } else if (s[pos] == ']') { if (operands.Count == 0) { var temp = new NestedInteger(); if (result == null) { result = temp; } else { temp.Add(result); result = temp; } pos++; continue; } int number = operands.Pop(); ops.Pop(); if (result == null) { result = new NestedInteger(number); } else { var temp = new NestedInteger(); temp.SetInteger(number); temp.Add(result); result = temp; } pos++; } else { pos++; } } return(result); }
public NestedInteger Deserialize(string s) { //Console.WriteLine(s); if (s.Length <= 0) { return(null); } if (s[0] != '[') { int i = 0; if (s[0] == '-') { i++; } int num = 0; for (; i < s.Length; i++) { num *= 10; num += s[i] - '0'; } if (s[0] == '-') { num = 0 - num; } return(new NestedInteger(num)); } else { NestedInteger res = new NestedInteger(); int b = 1, p = 1; while (p < s.Length) { if (s[p] == '[') { int pp = p; int counter = 1; while (counter > 0) { pp++; if (s[pp] == '[') { counter++; } if (s[pp] == ']') { counter--; } } NestedInteger ni = Deserialize(s.Substring(p, pp - p + 1)); if (ni != null) { res.Add(ni); } p = pp + 1; } else { int pp = p; while (pp < s.Length - 1 && s[pp] != ',') { pp++; } NestedInteger ni = Deserialize(s.Substring(p, pp - p)); if (ni != null) { res.Add(ni); } p = pp + 1; } } return(res); } }