zoukankan      html  css  js  c++  java
  • 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

     1 class Solution {
     2 public:
     3     vector<int> twoSum(vector<int> &numbers, int target) {
     4         bool find = false;
     5         int index1,index2;
     6         auto it = numbers.begin();
     7         auto tmp = it;
     8         while(!find && it != numbers.end() ){
     9             tmp = it+1;
    10             while(!find && tmp!=numbers.end()  ){
    11                 if(*it+*tmp ==target){
    12                     find = true;
    13                     index2 = tmp - numbers.begin()+1;
    14                 }
    15                 ++tmp;
    16             }
    17             ++it;
    18         }
    19         index1 = it-numbers.begin();
    20         vector<int> temp;
    21         if(it != numbers.end()){
    22             temp.push_back(index1);
    23             temp.push_back(index2);
    24         }
    25 
    26         return temp;
    27     }
    28 };

    这段代码时间复杂度为O(n^2),,在本地测试通过,但是oj不能ac,错误信息:Time Limit Exceeded超时了。

    使用sort (algorithm)函数 先对 numbers的拷贝 排序(O(NlogN)),找出 add number ,存放在 vector<int>中。

    在走一遍 vector<int> numbers (O(N)),记下索引,有些小细节注意一下就OK了。

    class Solution {
    public:
        vector<int> twoSum(vector<int> &numbers, int target) {
            vector<int> index;
            vector<int> copy(numbers);
            sort(copy.begin(),copy.end());//O(nlogn)
            vector<int> returnnu(SumAlgorithm(copy,target));
            int i=0,j=0,flag=-1;//flag 用于标记 index里边是否有数了,防止 两数一样,如果flag初始化0 ,会出错。
            while(j<2 && !returnnu.empty() && i<= numbers.size()-1){
                if(i != flag && returnnu[j] == numbers[i]){
                    index.push_back(1+i);
                    flag = i;
                    i=-1;//非常重要,纠结这里好久了,numbers没排序,i需要置0
                    ++j;
                }
                ++i;
            }
            if( index[0] > index[1]){
                index[1] = index[1]^index[0];
                index[0] = index[0]^index[1];
                index[1] = index[1]^index[0];
            }
            return index;
        }
        //返回 两个 add的数 ,有序
        vector<int> SumAlgorithm(vector<int>& numbers,int target){
            vector<int> tmp;
            int j = numbers.size()-1;
            int i=0,sum =0;
            bool find = false;
            while(!find && i<j){
                sum = numbers[i] + numbers[j];
                if(sum == target)
                    find = true;
                else if(sum < target)
                    ++i;
                else
                    --j;
            }
            if(find){
                tmp.push_back(numbers[i]);
                tmp.push_back(numbers[j]);
            }
            return tmp;   
        }
    };
  • 相关阅读:
    口袋摸球
    后渗透(四)数据包捕获
    后渗透(二)权限提升
    文件上传11-21
    MySQL之UDF提权
    《白帽子讲Web安全》读书笔记(一)
    XSS盲打获取cookies&&XSS钓鱼&&XSS键盘记录
    nginx目录穿越漏洞复现&&nginx目录遍历漏洞复现
    python教程(一)·简介
    DataPipeline如何实现数据质量管理?
  • 原文地址:https://www.cnblogs.com/ittinybird/p/4083524.html
Copyright © 2011-2022 走看看