|
| 1 | +/** |
| 2 | +given an IntegerIterator (which implements hasNext, next, remove), |
| 3 | +implement a PositiveIterator that has hasNext, next, remove |
| 4 | +
|
| 5 | +Solution: |
| 6 | +The key point is when we calling HasNext(), we must using integer iterator to try to get next positive |
| 7 | +element, and after getting the next positive element, we must store it somewhere. So that if we keep |
| 8 | +calling HasNext() won't affect the result of calling Next(). |
| 9 | +We can store the next positive to an integer variable prev, each time when calling HasNext(), |
| 10 | +we firstly check prev, if prev>0, directly return true. When calling Next(), |
| 11 | +we also firstly check prev by calling HasNext(), then return the value of prev and set prev to -1 again. |
| 12 | +
|
| 13 | +
|
| 14 | +*/ |
| 15 | + |
| 16 | +using System; |
| 17 | +using System.Collections.Generic; |
| 18 | + |
| 19 | +//Suppose this is an working integer iterator |
| 20 | +public class IntegerIterator{ |
| 21 | + private int[] array; |
| 22 | + private int index; |
| 23 | + public IntegerIterator(int[] a){ |
| 24 | + this.array = a; |
| 25 | + index = 0; |
| 26 | + } |
| 27 | + //check if there is next integer |
| 28 | + public bool HasNext(){ |
| 29 | + if(index>array.Length-1) |
| 30 | + return false; |
| 31 | + return true; |
| 32 | + } |
| 33 | + |
| 34 | + //return next integer |
| 35 | + public int Next(){ |
| 36 | + if(index>array.Length-1) |
| 37 | + throw new ArgumentException("no next element!"); |
| 38 | + int res = array[index]; |
| 39 | + index++; |
| 40 | + return res; |
| 41 | + } |
| 42 | + |
| 43 | + //remove last integer returned by iterator |
| 44 | + public void Remove(){ |
| 45 | + if(index==0) |
| 46 | + throw new ArgumentException("no element to remove!"); |
| 47 | + List<int> list = new List<int>(array); |
| 48 | + list.RemoveAt(index-1); |
| 49 | + array = list.ToArray(); |
| 50 | + } |
| 51 | + |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +public class PositiveIterator{ |
| 56 | + private IntegerIterator iter; |
| 57 | + private int prev; //hold the previous positive number |
| 58 | + public PositiveIterator(IntegerIterator i){ |
| 59 | + this.iter = i; |
| 60 | + prev = -1; |
| 61 | + } |
| 62 | + |
| 63 | + //check if there is next positive integer |
| 64 | + public bool HasNext(){ |
| 65 | + if(prev>0) //first look up prev |
| 66 | + return true; |
| 67 | + while(iter.HasNext()){ |
| 68 | + prev = iter.Next(); |
| 69 | + if(prev>0) |
| 70 | + return true; |
| 71 | + } |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + //return next positive integer |
| 76 | + public int Next(){ |
| 77 | + // if(prev>0){ |
| 78 | + // int num = prev; |
| 79 | + // prev = -1; |
| 80 | + // return num; |
| 81 | + // } |
| 82 | + if(HasNext()){ //will firstly check prev in HasNext() |
| 83 | + int num = prev; |
| 84 | + prev = -1; |
| 85 | + return num; |
| 86 | + } |
| 87 | + throw new ArgumentException("no next positive number!"); |
| 88 | + } |
| 89 | + |
| 90 | + //remove last returned positive integer |
| 91 | + public void Remove(){ |
| 92 | + if(prev<0) |
| 93 | + iter.Remove(); |
| 94 | + else |
| 95 | + throw new Exception("can't remove!"); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +public class Test |
| 100 | +{ |
| 101 | + public static void Main() |
| 102 | + { |
| 103 | + //Test self Integer Iterator |
| 104 | + int[] a = new int[]{1,-2,-3,4,5,-6,7,-8,9}; |
| 105 | + IntegerIterator it = new IntegerIterator(a); |
| 106 | + PositiveIterator iter = new PositiveIterator(it); |
| 107 | + Console.WriteLine(iter.HasNext()); //true |
| 108 | + Console.WriteLine(iter.HasNext()); //true |
| 109 | + Console.WriteLine(iter.HasNext()); //true |
| 110 | + Console.WriteLine(iter.Next()); //1 |
| 111 | + Console.WriteLine(iter.Next()); //4 |
| 112 | + Console.WriteLine(iter.Next()); //5 |
| 113 | + |
| 114 | + |
| 115 | + } |
| 116 | +} |
0 commit comments