力扣第一题(两数之和)完整代码

方法1

#include 
#include 
using namespace std;

class Solution {

public:
   vector twoSum(vector& nums,int target)
	{
		vector ans;
		for(int i=0;i ans;
	int target=9;
	vector nums={2,7,11,17};

	ans=sol.twoSum(nums,target);
	 for (auto i:ans)
        cout << i << endl;
	return 0;
}

方法2

//twosum.h/

#ifndef TWOSUM_H_
#define TWOSUM_H_
#include 
#include 
#include 
using namespace std;
class Solution{
	public:
		vector twoSum(vector& nums,int target);
		
	};
#endif

//twosum.cpp/

#include 
#include 
#include 
#include "twosum.h"


vector Solution::twoSum(vector& nums,int target)
	{
		vector ans;
		for(int i=0;i

//sumtwonums.cpp/

#include 
#include 
#include 
#include "twosum.h"

int main(int argc, char **argv)
{
Solution sol;
	vector ans;
	int target=9;
	vector nums={2,7,11,17};

	ans=sol.twoSum(nums,target);
	 for (auto i:ans)
        cout << i << endl;
	return 0;
}

你可能感兴趣的:(C++学习之路,leetcode,算法,职场和发展)