[LeetCode] 001. Two Sum (Medium) (C++/Java/Python),leetcodepython

來源:互聯網
上載者:User

[LeetCode] 001. Two Sum (Medium) (C++/Java/Python),leetcodepython

索引:[LeetCode] Leetcode 題解索引 (C++/Java/Python/Sql)
Github: https://github.com/illuz/leetcode

001.Two_Sum (Medium)連結:

題目:https://oj.leetcode.com/problems/two-sum/
代碼(github):https://github.com/illuz/leetcode

題意:

一個數組中兩個位置上的數的和恰為 target,求這兩個位置。

分析:

暴力找過去複雜度是 O(n^2),會 TLE。

代碼:

方法一:雙指標

class Solution {    public:        vector<int> twoSum(vector<int> &numbers, int target) {            int sz = numbers.size();            int left = 0, right = sz - 1, sum = 0;            vector<int> sorted (numbers);            std::sort(sorted.begin(), sorted.end());            vector<int> index;            while (left < right) {                sum = sorted[left] + sorted[right];                if (sum == target) {                    // find the answer                    for (int i = 0; i < sz; i++) {                        if (numbers[i] == sorted[left])                            index.push_back(i + 1);                        else if (numbers[i] == sorted[right])                            index.push_back(i + 1);                        if (index.size() == 2)                            return index;                    }                } else if (sum > target) {                    right--;                } else {                    left++;                }            }            // Program never go here, because            // "each input would have exactly one solution"        }};

Java:

public class Solution {    static class Pair implements Comparable<Pair> {        int value, index;        public Pair(int v, int id) {            value = v;            index = id;        }        @Override        public int compareTo(Pair b) {            return this.value - b.value;        }    }    public static int[] twoSum(int[] numbers, int target) {        int[] res = new int[2];        Pair[] pairs = new Pair[numbers.length];        // get pairs and sort        for (int i = 0; i < numbers.length; ++i) {            pairs[i] = new Pair(numbers[i], i + 1);        }        Arrays.sort(pairs);        // two points        int left = 0, right = numbers.length - 1, sum = 0;        while (left < right) {            sum = pairs[left].value + pairs[right].value;            if (sum == target) {                res[0] = pairs[left].index;                res[1] = pairs[right].index;                if (res[0] > res[1]) {                    // swap them                    res[0] ^= res[1];                    res[1] ^= res[0];                    res[0] ^= res[1];                }                break;            } else if (sum > target) {                --right;            } else {                ++left;            }        }        return res;    }}

Python:

class Solution:    # @return a tuple, (index1, index2)    def twoSum(self, num, target):        # sort        sorted_num = sorted(num)        # two points        left = 0        right = len(num) - 1        res = []        while (left < right):            sum = sorted_num[left] + sorted_num[right]            if sum == target:                # find out index                break;            elif sum > target:                right -= 1            else:                left += 1        if left == right:            return -1, -1        else:            pos1 = num.index(sorted_num[left]) + 1            pos2 = num.index(sorted_num[right]) + 1            if pos1 == pos2:    # find again                pos2 = num[pos1:].index(sorted_num[right]) + pos1 + 1            return min(pos1, pos2), max(pos1, pos2)

方法二: Map

C++ :

const int N = 200002;const int OFFSET = 100000;class Solution {    private:        int idOfNum[N];    public:        vector<int> twoSum(vector<int> &numbers, int target) {            vector<int> index;            memset(idOfNum, 0, sizeof(idOfNum));            int sz = numbers.size();            for (int i = 0; i < sz; i++) {                int rest = target - numbers[i];                if (idOfNum[rest + OFFSET]) {                    index.push_back(idOfNum[rest + OFFSET]);                    index.push_back(i + 1);                    return index;                }                idOfNum[numbers[i] + OFFSET] = i + 1;            }            // Program never go here, because            // "each input would have exactly one solution"        }};

Java:

public class Solution {    public static int[] twoSum(int[] numbers, int target) {        int[] res = new int[2];        HashMap<Integer, Integer> nums = new HashMap<Integer, Integer>();        for (int i = 0; i < numbers.length; ++i) {            // add i-th number            Integer a = nums.get(numbers[i]);            if (a == null)                nums.put(numbers[i], i);            // find (target - numbers[i])            a = nums.get(target - numbers[i]);            if (a != null && a < i) {                res[0] = a + 1;                res[1] = i + 1;                break;            }        }        return res;    }}

Python:

class Solution:    # @return a tuple, (index1, index2)    def twoSum(self, num, target):        dictMap = {}        for index, value in enumerate(num):            if target - value in dictMap:                return dictMap[target - value] + 1, index + 1            dictMap[value] = index

方法三:數組

C++: (與方法二的對比)

const int N = 200002;const int OFFSET = 100000;class Solution {    private:        int idOfNum[N];    public:        vector<int> twoSum(vector<int> &numbers, int target) {            vector<int> index;            memset(idOfNum, 0, sizeof(idOfNum));            int sz = numbers.size();            for (int i = 0; i < sz; i++) {                int rest = target - numbers[i];                if (idOfNum[rest + OFFSET]) {                    index.push_back(idOfNum[rest + OFFSET]);                    index.push_back(i + 1);                    return index;                }                idOfNum[numbers[i] + OFFSET] = i + 1;            }            // Program never go here, because            // "each input would have exactly one solution"        }};

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.