zoukankan      html  css  js  c++  java
  • LeetCode1-Two Sum

    题目:

    Given an array of integers, find two numbers such that they add up to a specific target number.

    The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

    You may assume that each input would have exactly one solution.

    Input: numbers={2, 7, 11, 15}, target=9
    Output: index1=1, index2=2

    分析:

      这题一开始拿到,一看会觉得很简单,会想到一个很简单的想法:两层for循环,判断每个数与之后的每一个数相加是否为target,如果得到,则终止循环得到结果。可是当你敲完之后,会得到超时的提醒,分析指导这个想法的时间复杂度为O(mn)。

      所以,我们必须用一个时间复杂度更优的算法来解这道题。其实,从题目中给的例子可以看到,先将数排序,排序之后从序列头和尾同时遍历判断,这里,我们必须创建一个新的结构,保存序列的值以及原始位置,假设结构体为a,以从小到大排序为例,i表示从头开始的下标,j表示从尾开始的下标,如果a[i].val+a[j].val==target,则返回对应下标;若小于target,则i++,因为若小于,则比a[j].val小的任何元素和i之前的元素相加都是不可能得到target的,所以i++;若大于target,则j--,因为若大于,则比a[i].val大的任何元素和j之后的元素相加都是不可能得到target的,所以j++。该算法的时间复杂度为快排的时间复杂度O(nlogn)。

    参考代码:

    struct Node
    {
        int val;
        int index;
        Node(){}
        Node(int v, int idx):val(v), index(idx){}
    };
    
    bool compare(const Node &lhs, const Node &rhs)
    {
        return lhs.val < rhs.val;
    }
    
    class Solution {
    public:
        vector<int> twoSum(vector<int> &numbers, int target) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            vector<Node> a;
            for(int i = 0; i < numbers.size(); i++)
                a.push_back(Node(numbers[i], i + 1));
            sort(a.begin(), a.end(), compare);
            
            int i = 0;
            int j = numbers.size() - 1;
            while(i < j)
            {
                int sum = a[i].val + a[j].val;
                if (sum == target)
                {
                    vector<int> ret;
                    int minIndex = min(a[i].index, a[j].index);
                    int maxIndex = max(a[i].index, a[j].index);
                    ret.push_back(minIndex);
                    ret.push_back(maxIndex);
                    return ret;
                }
                else if (sum < target)
                    i++;
                else
                    j--;
            }
        }
    };

     

  • 相关阅读:
    (40)C#windows服务控制
    (6)C#事务处理
    (1)sqlite基础
    bzoj 2456 mode
    bzoj 2763 [JLOI2011]飞行路线
    bzoj1010[HNOI2008] 玩具装箱toy
    bzoj1034 [ZJOI2008]泡泡堂BNB
    [bzoj1059] [ZJOI2007] 矩阵游戏 (二分图匹配)
    [bzoj1066] [SCOI2007] 蜥蜴
    bzoj1072 [SCOI2007]排列perm
  • 原文地址:https://www.cnblogs.com/yanqi0124/p/4244957.html
Copyright © 2011-2022 走看看