• 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--;
            }
        }
    };

     

  • 相关阅读:
    数据库创建索引的缺点,和什么时候不该创建索引
    创建数据库,表,索引,删除索引,查看表中的索引和如何使用表中的索引
    java容器中 哪些是线程安全的
    java中集合类详解
    高并发 问题怎么解决
    数据库20个问题(关于事务、存储引擎、索引、悲观锁乐观锁)
    数据库事务(什么是事务)
    Application对象详解
    get和post 两种基本请求方式的区别
    BZOJ1003物流運輸 DP + SPFA
  • 原文地址:https://www.cnblogs.com/yanqi0124/p/4244957.html
Copyright © 2020-2023  润新知