Leetcode 170:two Sum iii-data structure Design

Source: Internet
Author: User

The Sum iii-data structure designTotal Accepted: 311 Total Submissions: 1345

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 are equal to the value.

For example,

public class Twosum {    Map<integer, integer> Map = new Hashmap<integer, integer> ();    public void Add (int number) {    if (Map.containskey (number)) {        map.put (number, Map.get (number) + 1);    } else { C5/>map.put (number, 1);}    } public boolean find (int value) {for    (int key:map.keySet ()) {        int another = Value-key;        if (another = = key && map.get (key) > 1) {            return true;        } else if (another! = key && Map.contain SKey (another)) {            return true;        }    }    return false;}}


Leetcode 170:two Sum iii-data structure Design

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.