每日LeetCode之Math类--167. Two Sum II - Input array is sorted (C, C++, Python)
本文讲述了Array类中第167个问题的几种解法,实现语言包括C,Python以及C++。
问题:
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.
Note:
- Your returned answers (both index1 and index2) are not zero-based.
- You may assume that each input would have exactly one solution and you may not use the same element twice.
给定已按升序排序的整数数组, 找到两个数字, 使它们加起来就是一个特定的目标数字。
函数二求和应返回两个数字的索引, 使它们加起来达到目标, 其中索引1必须小于索引2。
注意:
- 返回的答案 (索引1和索引 2) 不是从零开始的。
- 您可以假定每个输入只有一个解决方案, 并且不能两次使用相同的元素。
Example:
Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
1.C语言--指针对撞法(O(n))
指针对撞法是指用两个指针分别指向数组的最低位和最高位,通过判断两个指针所指数的和与目标值的大小,对与两者之和大于目标值的,将最高位指针下移一位,对于两者之和小于目标值的,将最低位指针上移一位,以此类推,直到找到与目标值相同的两个数,返回指针即可。
[code]/** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free(). */ int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) { int i; int minIndex = 0; int maxIndex = numbersSize - 1; *returnSize=2; int* result = (char*)malloc(2*sizeof(int)); for(i = 0; i < numbersSize; i++){ if(numbers[minIndex] + numbers[maxIndex] == target){ result[0]=minIndex + 1; result[1]=maxIndex + 1; return result; } if(numbers[minIndex] + numbers[maxIndex] > target){ maxIndex--; } if(numbers[minIndex] + numbers[maxIndex] < target){ minIndex++; } } return result; }
运行结果为:
2. C++语言--指针对撞法(O(n))
C++同样采用指针对撞法,算法如下:
[code]class Solution { public: vector<int> twoSum(vector<int>& numbers, int target) { int i; int minIndex = 0; int maxIndex = numbers.size() - 1; for(i = 0; i < numbers.size(); i++){ if(numbers[minIndex] + numbers[maxIndex] == target){ return {minIndex + 1, maxIndex + 1}; } if(numbers[minIndex] + numbers[maxIndex] > target){ maxIndex--; } if(numbers[minIndex] + numbers[maxIndex] < target){ minIndex++; } } return {}; } };
运行结果为:
3. Python--指针对撞法(O(n))
算法与前两者一样,如下所示:
[code]class Solution: def twoSum(self, numbers: List[int], target: int) -> List[int]: minIndex = 0; maxIndex = -1; while numbers[minIndex]+numbers[maxIndex] != target: if numbers[minIndex]+numbers[maxIndex] > target: maxIndex = maxIndex-1 else: minIndex = minIndex+1 return minIndex+1, maxIndex+1+len(numbers)
运行结果为:
- leetcode 167. Two Sum II - Input array is sorted python
- leetcode 167. Two Sum II - Input array is sorted
- [Leetcode] 167. Two Sum II - Input array is sorted
- LeetCode - 167. Two Sum II - Input array is sorted
- LeetCode 167. Two Sum II - Input array is sorted
- LeetCode167. Two Sum II - Input array is sorted解答
- Leetcode 167. Two Sum II - Input array is sorted
- leetcode - 167. Two Sum II - Input array is sorted
- leetcode 167. Two Sum II - Input array is sorted
- leetcode-java-167. Two Sum II - Input array is sorted
- [leetcode]: 167. Two Sum II - Input array is sorted
- 【leetcode】167. Two Sum II - Input array is sorted
- 【leetcode】167. Two Sum II - Input array is sorted【M】【100%】
- [LeetCode]167. Two Sum II - Input array is sorted(输入两数和 II - 输出排序数组这两数位置)
- LeetCode 167. Two Sum II - Input array is sorted
- 【python】【leetcode】【算法题目167—Two Sum II - Input array is sorted】
- Leetcode——167. Two Sum II - Input array is sorted
- LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)
- [leetcode]167. Two Sum II - Input array is sorted
- 【leetcode】167. Two Sum II - Input array is sorted