Leetcode 167. Two Sum II - Input array is sorted
2016-12-22 12:32
441 查看
/** * Two-pointer approach. * pointer low starts from 0, pointer high starts from length-1 * if nums[low]+nums[high] == target return low and high * else if nums[low]+nums[high] > target means we need a samller number, move high forward * else nums[low]+nums[high] > target means we need a bigger number, move low backward */ public class Solution { public int[] twoSum(int[] numbers, int target) { int low = 0, high = numbers.length-1; while (low < high) { if (numbers[low]+numbers[high] == target) return new int[] {low+1, high+1}; else if (numbers[low]+numbers[high] < target) low++; else high--; } return new int[] {}; } }
相关文章推荐
- 【LeetCode】167. Two Sum II - Input array is sorted
- LeetCode 167. Two Sum II - Input array is sorted 题解(C++)
- 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
- Leetcode 167. Two Sum II - Input array is sorted (Medium) (cpp)
- 【LeetCode】 167. Two Sum II - Input array is sorted
- LeetCode- 167. Two Sum II - Input array is sorted - 思路详解- C++
- leetcode-java-167. Two Sum II - Input array is sorted
- LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告
- 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【M】【100%】
- 【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