LeetCode第一題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.

Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
查看原題;
查看我的Github項目地址;
題目很簡單:給定一個int數(shù)組和一個目標值,返回數(shù)組中兩個數(shù)之和等于目標值的這兩個數(shù)的索引。

二、解題

2.1 窮舉法

這是首先想到的方法,也是最簡單直接的方法。
遍歷所有兩個數(shù)之和,找到等于目標值的兩個數(shù)即可。
代碼如下:

public static int[] twoSumByExhaustive(int[] arr, int target) {
    int[] result = new int[2];
    for (int i = 0; i < arr.length; i++) {
        for (int j = i+1; j < arr.length; j++) {
            if (arr[i] + arr[j] == target) {
                result[0] = i;
                result[1] = j;
                break;
            }
        }
    }
    return result;
}

很多算法都可以用窮舉法解決,但是作為一個有理想有追求的程序猿,怎可滿足于此。

2.2 以空間換時間

問:求兩個數(shù)之和等于目標值有其他的描述方式么?
答:有,也可以說是求目標值和其中一個數(shù)之差在數(shù)組中的位置。
于是,很自然我們想到了以空間換取時間,用HashMap存儲之差,然后循環(huán)數(shù)組找到等于差的數(shù)。
代碼如下:

public static int[] twoSumByMap(int[] nums, int target) {
    int[] result = new int[2];
    Map<Integer, Integer> map = new HashMap();
    for (int i = 0; i < nums.length; i++) {
        if (map.containsKey(nums[i])) {
            result[0] = map.get(nums[i]);
            result[1] = i;
        } else {
            map.put(target - nums[i], i);
        }
    }
    return result;
}

2.3 排序后再查找

問:假如數(shù)組是有序的,那么是不是可以更快的找到結(jié)果?
答:如果是有序數(shù)組,那就可以一趟循環(huán)二路查找結(jié)果,自然更快。
但是這會有幾個問題:
1.排序的消耗?
可以采用快排等高性能排序算法
2.排序后如何返回正確的索引?
可以HashMap存鍵值對,然后查找索引。但是如果鍵是相同的數(shù),索引就會出錯。
可以copy一份原數(shù)組,查找索引。
代碼如下:

public static int[] twoSumBySort(int[] nums, int target) {
    int[] result = new int[2];
//  int[] copyNums = nums.clone();
    int[] copyNums = new int[nums.length];
    System.arraycopy(nums, 0, copyNums, 0, nums.length);
//  可以自己寫排序算法
    Arrays.sort(nums);
    int sIndex = 0;
    int eIndex = nums.length - 1;
    for (int i = 0; i < nums.length; i++) {
        if (nums[sIndex] + nums[eIndex] == target) {
            result[0] = nums[sIndex];
            result[1] = nums[eIndex];
            break;
        } else if (nums[sIndex] + nums[eIndex] > target) {
            eIndex--;
        } else {
            sIndex++;
        }

    }

    boolean fGit = false;
    for (int i = 0; i < copyNums.length; i++) {
        if (!fGit && copyNums[i] == result[0]) {
            sIndex = i;
            fGit = true;
        } else if (copyNums[i] == result[1]) {
            eIndex = i;
        }
    }
    if (sIndex > eIndex) {
        result[0] = eIndex;
        result[1] = sIndex;
    } else {
        result[0] = sIndex;
        result[1] = eIndex;
    }

    return result;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 題目 題目的意思是在一個整形數(shù)組中查找連個數(shù)字,使其和等于給定的目標。并返回給出這兩個數(shù)出現(xiàn)的位置。 分析 初讀題...
    baixiaoshuai閱讀 551評論 0 0
  • title: Leet Code TwoSumdate: 2017-07-08 23:18:54tags:- Le...
    楊柳岸小鵬殘月閱讀 508評論 0 0
  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗。 張土汪:刷leetcod...
    土汪閱讀 12,899評論 0 33
  • 年前,檔案局和地方志辦公室聯(lián)合出了一本書《知青紀實文學》,單位里的一位老同事特意托我:能不能想辦法給我弄一本。...
    蘇州的薔薇花開閱讀 576評論 2 4
  • 在我看來,人生不過是一段長長的旅途,風景再美,終不是自己停留的地方。能成為你旅途上的風景,我很幸運,但我知道我不是...
    公子不世閱讀 417評論 0 0

友情鏈接更多精彩內(nèi)容