My AC code,How can I get better


  • 1
    J

    vector<int> twoSum(vector<int> &number, int target)
    {
    vector<int>result;
    int twosum = 0;
    if (number.size() <= 1)
    return result;
    vector<int>numbers(number);
    sort(numbers.begin(), numbers.end());
    int beg = 0, end = numbers.size() - 1;
    while (beg <= end)
    {
    twosum = numbers[beg] + numbers[end];
    if (twosum == target)
    {
    break;
    }
    else if (twosum > target)
    end--;
    else
    beg++;

    	}
    	if (beg < end)
    	{
    		auto it1 = find(number.begin(), number.end(), numbers[beg]);
    		auto dis1 = it1 - number.begin();
    		result.push_back(dis1 + 1);
    		vector<int>::iterator it2;
    		if (numbers[beg] != numbers[end])
    		{
    
    			 it2 = find(number.begin(), number.end(), numbers[end]);
    		}
    		else
    		{
    			 it2 = find(it1+1, number.end(), numbers[end]);
    		}
    		auto dis2 = it2 - number.begin();
    		result.push_back(dis2 + 1);
    		if (result[0]>result[1])
    			swap(result[0], result[1]);
    	}
    	return result;
    }

  • 0
    D

    you'd better format your code firstly


  • 0
    J
       vector<int> twoSum(vector<int> &number, int target)
    {
    	vector<int>result;
    	int twosum = 0;
    	if (number.size() <= 1)
    		return result;
    	vector<int>numbers(number);
    	sort(numbers.begin(), numbers.end());
    	int beg = 0, end = numbers.size() - 1;
    	while (beg <= end)
    	{
    		twosum = numbers[beg] + numbers[end];
    		if (twosum == target)
    		{
    			break;
    		}
    		else if (twosum > target)
    			end--;
    		else
    			beg++;
    
    
    	}
    	if (beg < end)
    	{
    		auto it1 = find(number.begin(), number.end(), numbers[beg]);
    		auto dis1 = it1 - number.begin();
    		result.push_back(dis1 + 1);
    		vector<int>::iterator it2;
    		if (numbers[beg] != numbers[end])
    		{
    
    			 it2 = find(number.begin(), number.end(), numbers[end]);
    		}
    		else
    		{
    			 it2 = find(it1+1, number.end(), numbers[end]);
    		}
    		auto dis2 = it2 - number.begin();
    		result.push_back(dis2 + 1);
    		if (result[0]>result[1])
    			swap(result[0], result[1]);
    	}
    	return result;
    }
    

  • 0
    J

    I commit my answer, I cant see it ,so ,it cant format ,,,,,,,,,,,My GOD ....


  • 0
    J

    I commit my code, I cant see it ,so ,it cant format ,,,,,,,,,,,My GOD ....


Log in to reply
 

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