C++代码只能写在他提供的类里吗?还需要自己写一个main函数吗?
leetcode吧
全部回复
仅看楼主
level 1
C++代码只能写在他提供的类里吗?还需要自己写一个main函数吗?
刚刚使用leetcode,不了解他们的规定。大神帮助帮助。感激不尽!
2016年10月01日 14点10分 1
level 1
问题是:
1. Two Sum QuestionEditorial Solution My Submissions
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
我是这样写的:
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target)
{
vector<int>vv;
int a=nums.size();
int i;
int j;
for(i=0;i<a;i++)
{
for(j=i+1;j<a;j++)
{
if((nums[i]+nums[j])==target)
{
vv.push_back(nums[i]);
vv.push_back(nums[j]);
return vv;
}
}
}
return vv;
}
};
int main()
{
Solution solution;
vector<int>vv1;
vector<int>vv2;
vector<int>::iterator ite;
vv1.push_back(1);
vv1.push_back(2);
vv1.push_back(3);
vv1.push_back(4);
int taget=4;
vv2=solution.twoSum(vv1,taget);
return 0;
}
是在leetcode这样这样写吗?
2016年10月01日 14点10分 2
level 13
不需要写main函数
只写function,按照给定的名字和返回
2016年10月04日 07点10分 3
1