LeetCode 170. Two Sum III - Data structure design(两数和)

原题网址:https://leetcode.com/problems/two-sum-iii-data-structure-design/

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

方法一:采用哈希映射保存数字。

public class TwoSum {
    private Map<Integer, Integer> nums = new HashMap<>();

    // Add the number to an internal data structure.
	public void add(int number) {
	    Integer count = nums.get(number);
	    if (count == null) count = 1; else count ++;
	    nums.put(number, count);
	}

    // Find if there exists any pair of numbers which sum is equal to the value.
	public boolean find(int value) {
	    for(Integer key: nums.keySet()) {
	        int diff = value - key;
	        Integer count = nums.get(diff);
	        if (count == null) continue;
	        if (key == diff && count >=2) return true;
	        if (key != diff) return true;
	    }
	    return false;
	}
}


// Your TwoSum object will be instantiated and called as such:
// TwoSum twoSum = new TwoSum();
// twoSum.add(number);
// twoSum.find(value);
可以如下优化:

public class TwoSum {
    private Map<Integer, Integer> nums = new HashMap<>();

    // Add the number to an internal data structure.
	public void add(int number) {
	    Integer count = nums.get(number);
	    if (count == null) nums.put(number, 1); else nums.put(number, count+1);
	}

    // Find if there exists any pair of numbers which sum is equal to the value.
	public boolean find(int value) {
	    for(Map.Entry<Integer, Integer> entry: nums.entrySet()) {
	        int key = entry.getKey();
	        int diff = value - key;
	        if (key == diff && entry.getValue() >= 2) return true;
	        if (key != diff && nums.containsKey(diff)) return true;
	    }
	    return false;
	}
}

// Your TwoSum object will be instantiated and called as such:
// TwoSum twoSum = new TwoSum();
// twoSum.add(number);
// twoSum.find(value);

方法二:对新加入的数字进行排序。

public class TwoSum {
    private int[] nums = new int[1024];
    private int[] merged = new int[1024];
    private int size = 0;
    private int sortedSize = 0;

    // Add the number to an internal data structure.
	public void add(int number) {
	    if (size >= nums.length) {
	        merged = new int[merged.length*2];
	        int[] temp = new int[nums.length*2];
	        for(int i=0; i<size; i++) temp[i] = nums[i];
	        nums = temp;
	    }
	    nums[size++] = number;
	}
	
	private void sort(int[] nums, int from, int to) {
	    if (from >= to) return;
	    int pos = from;
	    for(int i=from; i<to; i++) {
	        if (nums[i] >= nums[to]) continue;
	        if (pos < i) {
                int t = nums[pos];
                nums[pos] = nums[i];
                nums[i] = t;
	        }
            pos ++;
	    }
	    if (nums[pos] > nums[to]) {
	        int t = nums[pos];
	        nums[pos] = nums[to];
	        nums[to] = t;
	    }
	    sort(nums, from, pos-1);
	    sort(nums, pos+1, to);
	}
	
    // Find if there exists any pair of numbers which sum is equal to the value.
	public boolean find(int value) {
	    if (sortedSize < size) {
	        sort(nums, sortedSize, size-1);
    	    int pos = 0;
    	    int i=0, j=sortedSize;
    	    while (i<sortedSize || j<size) {
    	        if (i>=sortedSize) merged[pos++] = nums[j++];
    	        else if (j>=size) merged[pos++] = nums[i++];
    	        else if (nums[i] <= nums[j]) merged[pos++] = nums[i++];
    	        else merged[pos++] = nums[j++];
    	    }
    	    int[] temp = nums;
    	    nums = merged;
    	    merged = temp;
    	    sortedSize = size;
	    }
	    int i=0, j=size-1;
	    while (i<j) {
	        int sum = nums[i]+nums[j];
	        if (sum < value) i++;
	        else if (sum > value) j--;
	        else return true;
	    }
	    return false;
	}
}


// Your TwoSum object will be instantiated and called as such:
// TwoSum twoSum = new TwoSum();
// twoSum.add(number);
// twoSum.find(value);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值