Two numbers found in the array and for the given value

Source: Internet
Author: User

Problem 167 & from Leetcode;

https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/

https://leetcode.com/problems/two-sum-iii-data-structure-design/

Let's take a look at the simple 167: Given a sorted array, and a value, find out the number of two numbers in the array that are equal to that value, assuming that such a value always exists;

This simply iterates through the array, assuming that the current value is x, you only need to find the Value-x in the array, if present, return directly, if it does not exist, check the next value, because the array is sorted, so the second step is to find only the time of log n; the worst case is n * log n;

Public int[] twosum (int[] numbers, int target)  {    int[]  result = new int[2];    for (int i = 1; i  <= numbers.length; i++)  {        int x =  numbers[i - 1];        int y = target  - x;        int j = arrays.binarysearch ( Numbers, i, numbers.length, y);         if (j >=  i)  {            result[0] = i ;             result[1] = j + 1 ;            break;         } &Nbsp;  }    return result;} 

Topic 170 requires designing a data structure that supports add and find operations, here's an example:

Add (1); Add (3); Add (5); Find (4)-Truefind (7)-False

The first thing I had to think about was using the 167 solution, adding the input to an array and ordering it, then you could use the 167 find directly, but a lot of add and find will be tle when the input is very long, and a simple analysis of this way, (I use insertion sort), each time the add sort, to O (n), then the total time is O (n * n);

Later I used the AVL tree, because the AVL tree supports Logn's insert/find operation; The following is the code for the final AC:

Public class twosum {    list<integer> numbers = new  ArrayList<> ();     avl avl = new avl ();     public void add (Int number)  {        avl.add (number);         numbers.add (number);    }     public boolean find (Int value)  {         for  (int x : numbers)  {             int y = value - x;             node node = avl.find (y);             if  (node == null)  {                 continue;             }            //when these two  numbers equal, need to make sure there at least two  numbers added;            if  (x == &NBSP;Y&NBSP;&AMP;&AMP;&NBSP;NODE.COUNT&NBSP;==&NBSP;1)  {                 continue;             }            return  True;        }        return  false;    }    public static void main (String[]  args)  {        twosum twosum = new twosum ();         twosum.add (1);         twosum.add (2);         system.out.println (Twosum.find (1));    }     class AVL {        Node root;         private int height (node root)  {             if  (root == null)  {                 return -1;             } else {                 return root.height;             }        }         private node insert (Node root, int value)  {             if  (root == null)  {                 root = new  Node (value);            } else if  ( Root.value == value)  {                 root.count += 1;             } else if  (Root.value < value)  {                 //go right;         &nBsp;       root.right = insert (Root.right, value);                 if  (Height ( Root.right)  - height (root.left)  == 2)  {                     if  (value >  Root.right.value)  {                         root = singlerotatewithright (root);                      } else {                         root = doublerotatewithright (Root) ;                     }                 }             } else {                 //go left;                 root.left = insert (Root.left, value);                 if  ( Height (root.left)  - height (root.right)  == 2)  {                     if  (value <  root.left.value)  {                    &nbsP;    root = singlerotatewithleft (Root);                     } else {                          root = doublerotatewithleft (Root);                     }                 }             }             Root.height = math.max (height (root.left),  height (root.right))  + 1;             return root;         }        private node doublerotatewithright (NODE&NBSP;K3)  {             k3.right = singlerotatewithleft ( K3.right);            return  Singlerotatewithright (K3);        }         private node singlerotatewithright (NODE&NBSP;K2)  {             Node k1 = k2.right;             k2.right = k1.left;             k1.left = k2;             k2.height = math.max (height (k2.left),  height (k2.right))  +  1;       &nBsp;    k1.height = math.max (height (k1.left),  height (k1.right))  +  1;            return k1;         }        private Node  Doublerotatewithleft (NODE&NBSP;K3)  {             k3.left = singlerotatewithright (K3.left);             return singlerotatewithleft (K3);         }         private node singlerotatewithleft (NODE&NBSP;K2)  {            Node k1 =  k2.left;            k2.left = k1.right;             k1.right = k2;             k2.height = math.max (height (k2.left),  height (k2.right))  + 1;             k1.height = math.max (Height ( K1.left),  height (k1.right))  + 1;             return k1;        }         public void add (Int value)  {             root = insert (Root, value);         }        private node find (Node root,  Int value)  {            if  (root  == null)  {                return null;             }             if  (Root.value == value && root.count  == 0)  {                 return null;            }             if  (Root.value == value)  {                return  root;            }             if  (Value > root.value)  {        &nBsp;        return find (Root.right, value);             } else {                 return find (Root.left, value);             }         }        public node find (Int value)  {             return find (Root, value);         }        public  Node getroot ()  {            return  Root;        }    }    static  class Node {        final int value;         int count, height;        node left,  right;        node (Int value)  {             this.value = value;             count = 1;             height = 0;        }     }}

Sure enough, this method is very effective and seems to be the fastest implementation in Java;

BTW, the realization of AVL is from the textbook found out, directly to write, really no such ability;


Two numbers found in the array and for the given value

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.