其他

1. 两数之和

class Solution {
    public int[] twoSum(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[j] == target - nums[i]) {
                    return new int[] { i, j };
                }
            }
        }
        throw new IllegalArgumentException("No two sum solution");
    }
}

18. 四数之和

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> result = new LinkedList<>();
        int a = 0;
        int b = 1;
        int c = 2;
        int d = 3;
        for (int i=nums[a];i<nums.length;++a){
            for (int j=nums[b];j<nums.length;++b){
                for (int k=nums[c];k<nums.length;++c){
                    for (int l=nums[d];l<nums.length;++d){
                        if (i+j+k+l == target){
                            result.add(new LinkedList<>(Arrays.asList(i, j, k, l)));
                        }break;
                    }
                }
            }
        }
        return result;
    }
}

###