zoukankan      html  css  js  c++  java
  • Two Sum III

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

    For example,
    add(1); add(3); add(5);
    find(4) -> true
    find(7) -> false

    The trade off should be considered: In fact, there has to be one operation's time complexity is O(n) and the other is O(1), no matter add or find. So clearly there's trade off when solve this problem, prefer quick find or quick add.

    if there are more find, add can be pre-done

    接口到实现类--

    这个不对,要用下面的hashmap实习add--O(1)

    interface TwoSumInterface{
        public void store(int value);
        public boolean test(int value);
    }
    
    public class TwoSumInterfaceImpl implements TwoSumInterface {
        private Set<Integer> localStore = new HashSet<Integer>();
        
        @Override
        public void store(int value) {
            localStore.add(value);
        }
    
        @Override
        public boolean test(int newValue) {
            for(int currentNumber:localStore){
                if(localStore.contains(newValue - currentNumber)) {
                    return true;
                }
            }
            return false;
        }
    }
    

      输入是否是int? or double? float?

    public class TwoSum {
            Set<Integer> sum;
            Set<Integer> num;
            
            TwoSum(){
                sum = new HashSet<Integer>();
                num = new HashSet<Integer>();
            }
            // Add the number to an internal data structure.
            public void add(int number) {
                if(num.contains(number)){
                    sum.add(number * 2);
                }else{
                    Iterator<Integer> iter = num.iterator();
                    while(iter.hasNext()){
                        sum.add(iter.next() + number);
                    }
                    num.add(number);
                }
            }
        
            // Find if there exists any pair of numbers which sum is equal to the value.
            public boolean find(int value) {
                return sum.contains(value);
            }
        }
    

      more add:

    public class TwoSum {
        HashMap<Integer, Integer> map;
        public TwoSum() {
            map = new HashMap<Integer, Integer>();
        }
        
        public void add(int x) {
            if (map.containsKey(x)) {
                map.put(x, map.get(x)+1);
            }
            else {
                map.put(x, 1);
            }
        }
        
        public boolean find(int target) {
            for (int i : map.keySet()) {
                if (map.containsKey(target-i)) {
                    if (target - i != i) return true;
                    else if (map.get(i) >= 2) return true;
                }
            }
            return false;
        }
    }
    

      注意17行的map.KeySet() return是一个Set形式的key的集合,Set是Collection的Subinterface, 所以这种for循环方法对Set也适用。而且即使key理论上是Integer, for循环前面的元素还是可以写成int:for(int i : map.keySet())

  • 相关阅读:
    java实现获取当前年月日 小时 分钟 秒 毫秒
    四种常见的 POST 提交数据方式(application/x-www-form-urlencoded,multipart/form-data,application/json,text/xml)
    Cannot send, channel has already failed:
    Java 枚举(enum) 详解7种常见的用法
    C语言指针详解(经典,非常详细)
    ActiveMQ进阶配置
    Frame size of 257 MB larger than max allowed 100 MB
    SpringJMS解析--监听器
    SpringJMS解析-JmsTemplate
    delphi 修改代码补全的快捷键(由Ctrl+Space 改为 Ctrl + alt + Space)
  • 原文地址:https://www.cnblogs.com/apanda009/p/7797171.html
Copyright © 2011-2022 走看看