Advertisement
Guest User

Untitled

a guest
Oct 8th, 2015
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  1. /**
  2.  * Returns a^b, as the standard mathematical exponentiation function
  3.  */
  4.  double pow(double a, int b) {
  5.     bool neg = b < 0;
  6.     b = abs(b);
  7.     double result = 1;
  8.     while (b > 0) {
  9.         if (b%2 == 1) {
  10.             result *= a;
  11.             --b;
  12.         }
  13.         a *= a;
  14.         b /= 2;
  15.     }
  16.     if (neg) return 1/result;
  17.     return result;
  18. }  
  19.  
  20.  
  21.  
  22. public interface TwoSum {
  23.    
  24.     private ArrayList<Integer> elems;
  25.     private HashMap<Integer, Integer> count;
  26.    
  27.     public TwoSum() {
  28.         elems = new ArrayList();
  29.         count = new HashMap();
  30.     }
  31.  
  32.  
  33.     /**
  34.      * Stores @param input in an internal data structure.
  35.      */
  36.     void store(int input) {
  37.         elems.add(input);
  38.         if (count.contains(input)) {
  39.             int e = count.get(input);
  40.             cout.set(input,e+1);
  41.         } else {
  42.             count.set(input,1);
  43.         }
  44.     }
  45.  
  46.     /**
  47.      * Returns true if there is any pair of numbers in the internal data structure which
  48.      * have sum @param val, and false otherwise.
  49.      * For example, if the numbers 1, -2, 3, 6, and 6 had been stored,
  50.      * the method should return true for 4, -1, and 9, but false for 10, 5, and 0
  51.      */
  52.     boolean test(int val) {
  53.         for (int e : elems) {
  54.             int search = val - e;
  55.             int c = count.get(search);
  56.             if (c == null) {
  57.                 continue;
  58.             } else {
  59.                 if (search == e && c > 1) {
  60.                         return true;
  61.                     }
  62.                 } else {
  63.                     return true;
  64.                 }
  65.             }
  66.         }
  67.          return false;
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement