It can pass in visual studio, but can't be AC here


  • 0
    J
    #include<iostream>
    

    #include<vector>
    using namespace std;
    int twoSum(vector<int> &numbers, int target) {
    int index1=1,index2=2;

        for(vector<int>::iterator iter1=numbers.begin();iter1!=numbers.end();++iter1)
        {
            for(vector<int>::iterator iter2=iter1+1;iter2!=numbers.end();++iter2)
            {
                if(*iter1+*iter2==target)
                {
                    cout<<"index1="<<index1<<", index2="<<index2<<endl;
    				break;
                }
                index2++;
            }
            index1++;
        }
    	return 0;
    }

  • 0
    S

    Your code runs in O(n^2) whereas this problem is expected to be solved in O(n). Also you cannot use any output statement on leetcode. And you must return a meaningful value (please read the problem description again). By the way, please show us the EXACT error message next time you ask a question.


  • 0
    J

    Thank you for your answer!
    I'll read the description again and try it again.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.