Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.jbjares;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import org.apache.commons.lang3.math.NumberUtils;
- import org.junit.Test;
- import com.jbjares.GlassdoorQ3.WordDistanceFinder;
- public class GlassdoorQ4 {
- // Question 3 public interface TwoSum {
- /** * Stores @param input in an internal data structure. */
- //void store(int input);
- /** * 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, 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); }
- @Test
- public void test() {
- TwoSumImpl twoSumImpl = new GlassdoorQ4().new TwoSumImpl();
- Map map = new HashMap();
- twoSumImpl.store(1);
- twoSumImpl.store(-2);
- twoSumImpl.store(3);
- twoSumImpl.store(6);
- org.junit.Assert.assertEquals(Boolean.TRUE,twoSumImpl.test(3));
- }
- public class TwoSumImpl implements TwoSum{
- private Map<Integer,Integer> input = new HashMap<Integer,Integer>();
- @Override
- public void store(int input) {
- if(this.input.isEmpty()){
- this.input.put(0,input);
- }else{
- this.input.put(this.input.size()+1,input);
- }
- }
- @Override
- public boolean test(int val) {
- for (Map.Entry<Integer,Integer> entry : input.entrySet()) {
- if(entry.getValue()==val){
- continue;
- }
- if(entry.getValue()+val>0){
- return Boolean.TRUE;
- }
- }
- return Boolean.FALSE;
- }
- }
- public interface TwoSum{
- void store(int input);
- boolean test(int val);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement