LeetCode 03 Two_Sum_III___Data_structure_design

question:

Design and implement a TwoSum class. It should support the following operations:add and find.

add - Add the number to an internal data structure.
find - Find if there exists any pair of numbers which sum is equal to the value.

For example,
add(1); add(3); add(5);
find(4) -> true
find(7) -> false

Title: Design and implement a TwoSum class that supports the following operations: add and search.

Add: Add members to a data structure of type int.

Find: Find if there is a pair of members whose sum is equal to value.

E.g:

add(1); add(3); add(5);

find(4) returns true; find(7) returns false

Idea: It is obviously not feasible to directly copy the previous solutions of TwoSum and TwoSum2, because it is necessary to consider the situation of increasing the same value, so it needs to be changed. The dictionary is still used, the key is the value of the number, and the value is the number of the number. If target-key==key, judge whether the value of key is greater than or equal to 2, else, whether there is a key of target-key in the dictionary.

code show as below:

class TwoSum
    {
        //Because Add is tested later to see if there is a problem, so declare it as public static
        public static Dictionary<int, int> dictionary = new Dictionary<int, int>();
        //Get all the keys and store them in the collection
        Dictionary<int, int>.KeyCollection keyCollection = dictionary.Keys;
        public void Add(int number)
        {
            if (dictionary.ContainsKey(number))
                dictionary[number] += 1;
            else
                dictionary.Add(number, 1);
        }
        public bool Find(int target)
        {
            for(int j=0;j<dictionary.Count;j++)
            {
                int temp = target - keyCollection.ElementAt(j);
                if (temp == keyCollection.ElementAt(j) && dictionary[j] >= 2)
                    return true;
                else if (dictionary.ContainsKey(temp))
                    return true;
            }
            return false;
        }
    }

Main method:

static void Main(string[] args)
        {
            TwoSum twoSum = new TwoSum();
            twoSum.Add(1);
            twoSum.Add(3);
            twoSum.Add(1);
            twoSum.Add(5);
            // print all elements in the dictionary
            foreach(KeyValuePair<int,int> i in TwoSum.dictionary)
            {
                Console.WriteLine("{0,-5}{1,-9}", i.Key, i.Value);
            }
            Console.WriteLine("Find(4):{0},Find(7):{1}", twoSum.Find(4), twoSum.Find(7));
            Console.ReadKey();
        }
Summary: I checked the dictionary API for this question for a long time. Because there is no c# code on the Internet Wronged , I have read all the APIs once or twice, and the idea is still relatively clear. If there are other solutions, please feel free to communicate with me.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326659651&siteId=291194637