• Leetcode--Two Sum


    Problem Description:

    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

    分析:依据题意,在一个无序数组中要找到两个数之和等于target的两个数的下标,easy想到假设是有序数组的话直接用两个变量一头一尾遍历数组就可以找到所需的两个数,所以这里一个问题是怎样处理无序的数的问题。能想到的是定义一个结构体,把每一个元素和下标一起记录下来,然后依照元素值大小排序,,然后一头一尾遍历能够得到结果,时间复杂度主要在排序,能够达到O(nlog(n))。详细代码例如以下:

        
       struct node{
            int val;
            int indexs;
        };  
    
        bool compare(node a,node b)
        {
            return a.val<b.val;
        }    
    
    class Solution {
    public:
    
        vector<int> twoSum(vector<int> &numbers, int target) {
            vector<int> res;
            if(numbers.size()<2) 
                return res;
            vector<node> nums;
            for(vector<int>::size_type index=0;index!=numbers.size();++index)
            {
                node temp;
                temp.val=numbers[index];
                temp.indexs=index+1;
                nums.push_back(temp);
            }
            
            sort(nums.begin(),nums.end(),compare);
            
            vector<node>::iterator p=nums.begin();
            vector<node>::iterator q=nums.end()-1;
            while(p<q)
            {
                if((p->val+q->val)>target)
                    q--;
                else 
                if((p->val+q->val)<target)
                    p++;
                else
                {
                    if(p->indexs<q->indexs)
                    {
                        res.push_back(p->indexs);
                        res.push_back(q->indexs);
                    }
                    else
                    {
                        res.push_back(q->indexs);
                        res.push_back(p->indexs);
                    }
                    break;
                }
            }
            return res;
        }
    };

    在discuss中看到有利用hash_map的性质来做的,实质就是利用map记录下每一个元素的下标,然后固定一个元素查找还有一个元素是否存在,时间复杂度能够达到O(n),详细实现例如以下: 

    class Solution {
    public:
        vector<int> twoSum(vector<int> &numbers, int target) {
            vector<int> res;
            if(numbers.size()<2)
                return res;
            unordered_map<int,int> numsmap;
            
            for(vector<int>::size_type index=0;index!=numbers.size();++index)
                numsmap[numbers[index]]=index;
            unordered_map<int,int>::iterator flag=numsmap.end();
            
            for(vector<int>::size_type index=0;index!=numbers.size();++index)
            {
                int temp=target-numbers[index];
                flag=numsmap.find(temp);
                if(flag!=numsmap.end()&&flag->second!=index)
                {
                    if(index<flag->second)
                    {
                        res.push_back(index+1);
                        res.push_back(flag->second+1);
                    }
                    else
                    {
                        res.push_back(flag->second+1);
                        res.push_back(index+1);
                    }
                    break;
                }
            }
            return res;
        }
    };


  • 相关阅读:
    promise异步获取数据
    解决浏览器保存密码自动填充问题
    函数柯里化
    JS高级函数的用法
    Object.assign替代写法
    tree-shaking
    假如参数是对象或者函数,如何处理
    宏任务与微任务 event-loop
    Linux常用命令大全
    Image Processing
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5390580.html
Copyright © 2020-2023  润新知