LeeCode_01_Two sum

  1. Two Sum
    Given an array of integers, return indices of the two numbers such that they add up to a specific target.
    You may assume that each input would have exactly one solution, and you may not use the same element twice.
    Example:
    Given nums = [2, 7, 11, 15], target = 9,
    Because nums[0] + nums[1] = 2 + 7 = 9,
    return [0, 1].

翻译:
Sum : 和
indices:索引
assume:假设

给定一系列数,以及一个指定的值
现在要选出两个数,使其和等于指定数。
假设:1.必有唯一解;2.给出的数字仅能使用一次。

解法
a.循环的去求解。
时间复杂度n(n-1)

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int [] result =new int[2];
        int result_01;
        int result_02;

        int numsSize = nums.length;
        for(int i = 0; i < numsSize; i++){
            result_01 = nums[i];
            result_02 = target - result_01;
            for(int j =i+1; j < numsSize; j++ ) {
                if(nums[j] == result_02){
                    result[0] = i;
                    result[1] = j;
                    return result;
                }
            }
        }
        return  result;
    }
}

19 / 19 test cases passed.
Status: Accepted
Runtime: 38 ms

b.需要对数组有更多的理解。。。。
自己觉得这道题的意义应该不是想上面的解法那样,单纯是对数组循环。
数组
下标:偏移量。
值:数组中存放的值
偏移量和值一一对应。
求解:两个特定值,值得和等于指定数。
数组中对对应值的查找方式: 遍历,二分等。应用场景是有区分的。。。。
现成的方法? 对方法内部实现的理解?对下标或值的操作的封装。

提高点:
1.map/字典/hashmap
应该可以把字典转换一下形式。利用高效率的方法。

------------------------------------2017/09/20--------------------------------------
我的其他解法:

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];
        Integer result_01;
        Integer result_02;
        int i;


        // 数值 ,,位置
        List<Integer> indexList;
        HashMap<Integer, List<Integer>> dataHashMap = new HashMap<>();
        for (i = 0; i < nums.length; i++) {
            result_01 = nums[i];
            if (dataHashMap.containsKey(result_01)) {
                indexList = dataHashMap.get(result_01);
                indexList.add(i);
            } else {
                indexList = new ArrayList<>();
                indexList.add(i);
            }
            dataHashMap.put(result_01, indexList);
        }

        for (i = 0; i < nums.length; i++) {
            result_01 = nums[i];
            result_02 = target - result_01;
            if (dataHashMap.containsKey(result_02)) {
                indexList = dataHashMap.get(result_02);
                if (indexList.get(0) != i) {
                    result[0] = i;
                    result[1] = indexList.get(0);
                    return result;
                } else if (indexList.size() > 1) {
                    result[0] = i;
                    result[1] = indexList.get(1);
                    return result;
                }
            }
        }
        return result;
    }
}

19 / 19 test cases passed.
Status: Accepted
Runtime: 13 ms

从第二种解法来看比之前单独循环来说要好很多了。
但是还是有缺点,先总结:

  1. 以空间换时间。
  2. 第二次解法中的思路是利用hashmap去重新记录 值以及值的索引。利用高效率的hash算法得到数据。
  3. 缺点: 看了下其他人的算法,整理:
    a.没必要去纯粹所有的数据的索引,有浪费。
    b.使用hash去提高效率是ok,但自己开始有些地方没想明白,钻牛角尖了。。。。

i. 数是由数组中的数据相加得到。
ii.能利用已知数得到另一个需要求的数,,,,,利用的数,不能一开始就假设是第一个。。。。
如果假设利用的数是第二数,那么第一个数必定在 我们的hashmap里面 :只需要去检测hashmap,如果没有则放入,然后循环。

好僵!!!!!!!!!!!!!!

and by the way:
3ms 那个代码,没看懂。。。。。。。。。

posted on 2017-09-18 00:57  mChineseCoder996ICU  阅读(192)  评论(0编辑  收藏  举报

导航