zoukankan      html  css  js  c++  java
  • twoSum

    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

     

    第一次的代码,思路很简单:

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    class Solution{
    public:
            vector<int> twoSum(vector<int>& nums, int target){
                    int i = 0;
                    int j = 0;
                    vector<int> result;
    
                    for(i=0; i<nums.size()-1; i++){
                            for(j=i+1; j<nums.size(); j++){
                                    if(nums[i] + nums[j] == target){
                                            result.push_back(i+1);
                                            result.push_back(j+1);
                                    }
                            }
                    }
                    return result;
            }
    };
    
    int main(){
            vector<int> src;
            int input;
            int target;
            int i =0;
    
            vector<int> result;
    
            do{
                    cin>>input;
                    src.push_back(input);
                    if(cin.get() == '
    '){
                            break;
                    }
            }while(1);
    
            cin>>target;
    
            Solution s;
            result = s.twoSum(src, target);
    
            for(i=0; i<result.size(); i++){
                    cout<<result[i]<<endl;
            }
    
            return 0;
    }
    

    最后发现,由于复杂度是O(n^2)系统无法接受,需要降低算法复杂度。。。

    现在看到别人用map来解决这个问题   

    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <map>
    
    using namespace std;
    
    class Solution {
    public:
    	vector<int> twoSum(vector<int> &numbers, int target) {
    		vector<int> result;
    		map<int, int> m;
    		if (numbers.size() < 2)
    			return result;
    		for (int i = 0; i < numbers.size(); i++)
    			m[numbers[i]] = i;
    
    		map<int, int>::iterator it;
    		for (int i = 0; i < numbers.size(); i++) {
    			if ((it = m.find(target - numbers[i])) != m.end())
    			{
    				if (i == it->second) continue;
    				result.push_back(i+1);
    				result.push_back(it->second+1);
    				return result;
    			}
    		}
    		return result;
    	}
    };
    
    int main(){
    	vector<int> src;
    	int input;
    	int target;
    	int i =0;
    
    	vector<int> result;
    
    	do{
    		cin>>input;
    		src.push_back(input);
    		if(cin.get() == '
    '){
    			break;
    		}
    	}while(1);
    
    	cin>>target;
    
    	Solution s;
    	result = s.twoSum(src, target);
    
    	for(i=0; i<result.size(); i++){
    		cout<<result[i]<<endl;
    	}
    
    	return 0;
    }
    
  • 相关阅读:
    Libcurl
    Inno Setup教程
    APICloud平台的融云2.0集成
    关于mysql建立索引 复合索引 索引类型
    linux恢复误删除文件-extundelete
    OpenStack QA
    Android之应用程序怎样调用支付宝接口
    NYOJ 22 素数求和问题
    Mycat(5):聊天消息表数据库按月分表实践,平滑扩展
    opencv对图像进行边缘及角点检測
  • 原文地址:https://www.cnblogs.com/fangying7/p/4620791.html
Copyright © 2011-2022 走看看