两数之和II

两数之和II_第1张图片

	public int[] twoSum(int[] numbers, int target) 
	{
		int i = 0, j = numbers.length-1;
		int[] ret = new int[2];
		while (i < j)
		{
			while (target > 0 && numbers[j] > target)
				j--;

			if (numbers[i] + numbers[j] > target)
			{
				j--;
			}
			else if (numbers[i] + numbers[j] == target)
			{
				ret[0] = i+1;
				ret[1] = j+1;
				return ret;
			}
			else
			{
				i++;
			}
		}
		return null;
	}

 

你可能感兴趣的:(算法,双指针)