zoukankan      html  css  js  c++  java
  • [LeetCode]1. 2Sum 数组中和为特定值的两个数

    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

    解法一:暴力搜索法,时间复杂度O(n^2)

    class Solution {
    public:
        vector<int> twoSum(vector<int> &numbers, int target) {
            vector<int> res;
            for (int i = 0; i < numbers.size(); ++i) {
                for (int j = i + 1; j < numbers.size(); ++j) {
                    if (numbers[j] + numbers[i] == target) {
                        res.push_back(i + 1);
                        res.push_back(j + 1);
                    }
                }
            }
            return res;
        }
    };

    解法二:考虑先将数组arr排序,利用两个指针left和right指向排好序数组的某两个值,初始化left=0,right=len-1,其中len为数组长度。当arr[left]+arr[right]=sum时,则找到答案返回;当arr[left]+arr[right]>sum,时,right--;当arr[left]+arr[right]<sum时,left++。循环上述过程,若找到则返回,否则直至left=right说明没有解。最后在原数组中找到符合要求的两个数的位置即可。时间复杂度O(nlogn)。

    class Solution {
    public:
        vector<int> twoSum(vector<int>& nums, int target) {
            vector<int> vi(nums.begin(), nums.end());
            sort(vi.begin(), vi.end());
            
            int index1 = 0;
            int index2 = nums.size() - 1;
            while(index1 < index2)
            {
                if(vi[index1] + vi[index2] == target)
                    break;
                else if(vi[index1] + vi[index2] < target)
                    index1++;
                else
                    index2--;
            }
            
            vector<int> res(2, 0);
            int i = 0;
            for (; i < nums.size(); i++)
            {
                if (nums[i] == vi[index1])
                {
                    res[0] = i + 1;
                    break;
                }
            }
            if (vi[index1] == vi[index2])
                i++;
            else
                i = 0;
            for (; i < nums.size(); i++)
            {
                if (nums[i] == vi[index2])
                {
                    res[1] = i + 1;
                    break;
                }
            }
            sort(res.begin(), res.end());
            
            return res;
        }
    };

    解法三:考虑使用STL的Map进行查找。先遍历一次数组,键为元素值,值为元素在数组中的下标。然后再从头开始遍历数组,在Map中找target=sum-arr[i]的元素,若存在则返回,否则直至i=len-1说明没有解。最后找到target在arr中的位置即可。时间复杂度O(n)。

    class Solution {
    public:
        vector<int> twoSum(vector<int> &numbers, int target) {
            vector<int> res;
            map<int, int> numMap;
            for (int i = 0; i < numbers.size(); ++i) {
                numMap[numbers[i]] = i;
            }
            for (int i = 0; i < numbers.size(); ++i) {
                int tmp = target - numbers[i];
                if (numMap.find(tmp) != numMap.end() && numMap[tmp] != i) {
                    res.push_back(i + 1);
                    res.push_back(numMap[tmp] + 1);
                    break;
                }
            }
            return res;
        }
    };

     

  • 相关阅读:
    决策树
    训练/测试
    机器学习 标准差
    pandas01
    提交参数说明
    pandas_series02
    Yarn 模式
    词云图
    软件需求与分析课堂综合测试
    课堂测试10
  • 原文地址:https://www.cnblogs.com/aprilcheny/p/4823576.html
Copyright © 2011-2022 走看看