Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Returns a^b, as the standard mathematical exponentiation function
- */
- double pow(double a, int b) {
- bool neg = b < 0;
- b = abs(b);
- double result = 1;
- while (b > 0) {
- if (b%2 == 1) {
- result *= a;
- --b;
- }
- a *= a;
- b /= 2;
- }
- if (neg) return 1/result;
- return result;
- }
- public interface TwoSum {
- private ArrayList<Integer> elems;
- private HashMap<Integer, Integer> count;
- public TwoSum() {
- elems = new ArrayList();
- count = new HashMap();
- }
- /**
- * Stores @param input in an internal data structure.
- */
- void store(int input) {
- elems.add(input);
- if (count.contains(input)) {
- int e = count.get(input);
- cout.set(input,e+1);
- } else {
- count.set(input,1);
- }
- }
- /**
- * Returns true if there is any pair of numbers in the internal data structure which
- * have sum @param val, and false otherwise.
- * For example, if the numbers 1, -2, 3, 6, and 6 had been stored,
- * the method should return true for 4, -1, and 9, but false for 10, 5, and 0
- */
- boolean test(int val) {
- for (int e : elems) {
- int search = val - e;
- int c = count.get(search);
- if (c == null) {
- continue;
- } else {
- if (search == e && c > 1) {
- return true;
- }
- } else {
- return true;
- }
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement