예제 #1
0
        private static void Run()
        {
            var randomizedSet = new RandomizedSet();

            randomizedSet.Insert(1);   // true
            randomizedSet.Remove(2);   // false
            randomizedSet.Insert(2);   // true
            randomizedSet.GetRandom(); // 1
            randomizedSet.Remove(1);   // true
            randomizedSet.Insert(2);   // false
            randomizedSet.GetRandom(); // 2
        }
예제 #2
0
        public void RandomizedSetTest()
        {
            RandomizedSet rs = new RandomizedSet();
            bool          result;

            result = rs.Insert(1);
            result = rs.Remove(2);
            result = rs.Insert(2);
            int randonValue = rs.GetRandom();

            result      = rs.Remove(1);
            result      = rs.Insert(2);
            randonValue = rs.GetRandom();
        }
        public void TestMethod()
        {
            RandomizedSet obj = new RandomizedSet();

            Assert.IsTrue(obj.Insert(1));
            Assert.IsTrue(obj.Insert(2));

            Assert.IsFalse(obj.Insert(2));

            Assert.IsTrue(obj.Remove(1));
            Assert.IsFalse(obj.Remove(3));

            Assert.IsTrue(obj.Insert(1));
            Assert.IsTrue(obj.Insert(3));
            Assert.IsTrue(obj.Insert(4));
            Assert.IsTrue(obj.Insert(5));
            Assert.IsTrue(obj.Insert(6));

            int[] data = new int[6];
            for (int i = 0; i < 3000; i++)
            {
                data[obj.GetRandom() - 1]++;
            }
            Assert.IsNotNull(data);
        }
예제 #4
0
        public IEnumerable <string> Run(int a)
        {
            var set    = new RandomizedSet();
            var result = new List <string>();

            result.Add(set.Insert(1).ToString());
            result.Add(set.Insert(1).ToString());
            result.Add(set.Remove(1).ToString());
            result.Add(set.GetRandom().ToString());
            return(result);
        }
예제 #5
0
        public void RandomizedSetTest1()
        {
            RandomizedSet set = new RandomizedSet();

            Assert.IsTrue(set.Insert(3));
            Assert.IsTrue(set.Remove(3));
            Assert.IsFalse(set.Remove(0));
            Assert.IsTrue(set.Insert(3));
            Assert.IsTrue(set.GetRandom() == 3);
            Assert.IsFalse(set.Remove(0));
        }
예제 #6
0
        //https://leetcode.com/problems/insert-delete-getrandom-o1/
        static void Main(string[] args)
        {
            RandomizedSet rSet = new RandomizedSet();

            for (int i = 0; i <= 10; i++)
            {
                rSet.Insert(i);
            }
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine("Random Num: {0}", rSet.GetRandom());
            }
            for (int i = 0; i < 7; i++)
            {
                rSet.Remove(rSet.GetRandom());
            }
            Console.WriteLine("Removed 7 random numbers.");
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine("Random Num: {0}", rSet.GetRandom());
            }
        }
예제 #7
0
        public void RandomizedSetTest()
        {
            RandomizedSet set = new RandomizedSet();

            Assert.IsTrue(set.Insert(1));
            Assert.IsTrue(set.Insert(2));
            Assert.IsTrue(set.Insert(3));
            Assert.IsFalse(set.Insert(3));
            Assert.IsFalse(set.Remove(4));
            Assert.IsTrue(set.Remove(3));
            var result = set.GetRandom();

            Assert.IsTrue(result == 1 || result == 2);
        }
        public void Test1()
        {
            var result = solution.Insert(0);

            Assert.AreEqual(true, result);
            result = solution.Insert(0);
            Assert.AreEqual(false, result);
            result = solution.Insert(1);
            Assert.AreEqual(true, result);
            result = solution.Insert(2);
            Assert.AreEqual(true, result);
            result = solution.Insert(3);
            Assert.AreEqual(true, result);
            var randRes = solution.GetRandom();
        }
        public static void Run()
        {
            RandomizedSet randomizedSet = new RandomizedSet();

            //randomizedSet.Insert(1);
            //randomizedSet.Remove(2);
            //randomizedSet.Insert(2);
            //randomizedSet.GetRandom();
            //randomizedSet.Remove(1);
            //randomizedSet.Insert(2);
            //randomizedSet.GetRandom();

            string[] operations   = new string[] { "RandomizedSet", "insert", "getRandom", "getRandom", "getRandom", "insert", "insert", "insert", "insert", "insert", "getRandom", "getRandom", "insert", "getRandom", "insert", "insert", "getRandom", "getRandom", "getRandom", "getRandom", "remove", "insert", "getRandom", "getRandom", "insert", "remove", "remove", "insert", "getRandom", "getRandom", "insert", "insert", "getRandom", "remove", "remove", "insert", "remove", "getRandom", "getRandom", "remove", "getRandom", "insert", "insert", "getRandom", "remove", "remove", "remove", "getRandom", "insert", "insert", "insert", "insert", "getRandom", "insert", "getRandom", "remove", "insert", "insert", "insert", "getRandom", "getRandom", "insert", "getRandom", "insert", "insert", "getRandom", "getRandom", "remove", "getRandom", "remove", "insert", "getRandom", "insert", "insert", "insert", "getRandom", "insert", "insert", "getRandom", "insert", "getRandom", "insert", "getRandom", "getRandom", "getRandom", "insert", "getRandom", "getRandom", "insert", "insert", "insert", "getRandom", "remove", "insert", "insert", "getRandom", "insert", "insert", "insert", "insert" };
            string   valuesString = "[],[-7],[],[],[],[6],[7],[10],[3],[8],[],[],[-8],[],[6],[-8],[],[],[],[],[2],[2],[],[],[5],[-5],[-8],[-8],[],[],[-4],[10],[],[7],[-1],[8],[-6],[],[],[9],[],[7],[0],[],[-10],[-4],[-3],[],[-4],[-5],[8],[-2],[],[-9],[],[7],[-2],[7],[4],[],[],[-6],[],[-8],[2],[],[],[9],[],[-1],[3],[],[0],[-3],[-1],[],[-8],[-10],[],[3],[],[4],[],[],[],[-10],[],[],[0],[-2],[5],[],[-2],[5],[10],[],[9],[0],[7],[-2]";

            string[] values = valuesString.Replace("[", "").Replace("]", "").Split(',');

            for (int i = 0; i < operations.Length; i++)
            {
                switch (operations[i])
                {
                case "RandomizedSet":
                    randomizedSet = new RandomizedSet();
                    Console.Write("null");
                    break;

                case "insert":
                    Console.Write(randomizedSet.Insert(int.Parse(values[i])));
                    break;

                case "remove":
                    Console.Write(randomizedSet.Remove(int.Parse(values[i])));
                    break;

                case "getRandom":
                    Console.Write(randomizedSet.GetRandom());
                    break;

                default:
                    break;
                }
                Console.Write(",");
            }
        }