`

LeetCode 167 - Two Sum II - Input array is sorted

阅读更多
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. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
public int[] twoSum(int[] numbers, int target) {
	int[] result = {-1, -1};
	
	if (numbers == null || numbers.length == 0) {
		return result;
	}
	
	int start = 0;
	int end = numbers.length - 1; 
	
	while (start < end) {
		int sum = numbers[start] + numbers[end];
		if (sum == target) {
			result[0] = start + 1;
			result[1] = end + 1;
			break;
		} else if (sum < target) {
			start++;
		} else {
			end--;
		}
	}
	
	return result;        
}

 

分享到:
评论

相关推荐

    js-leetcode题解之167-two-sum-II-input-array-is-sorted.js

    javascript js_leetcode题解之167-two-sum-II-input-array-is-sorted.js

    python-leetcode题解之167-Two-Sum-II-Input-array-is-sorted.py

    python python_leetcode题解之167_Two_Sum_II_Input_array_is_sorted.py

    leetcode167-LeetCode167_Two-Sum-II:给定一个目标target,再数组中找到两个数,使其和为target,并返

    在LeetCode上的第167题,我们面临的是一个经典的“两数之和”问题,题目命名为"Two-Sum II - Input array is sorted"。这个挑战要求我们在已排序的数组中寻找两个元素,它们的和等于给定的目标值`target`,并返回这...

    LeetCode题解 - Java语言实现-181页.pdf

    12. Two Sum II Input array is sorted 两数之和II是一个变体的问题,要求找到数组中两个元素之和等于目标值的元素,并且数组已经排序。可以使用双指针来解决该问题。 13. Two Sum III Data structure design 两...

    leetcode1004-leetcode:leetcode

    leetcode 1004 leetcode E:简单,M:中等,H:困难 数组和字符串 217. Contains Duplicate (E) 48. Rotate Image (M) -&gt; 2 73. Set Matrix Zeroes (M) 1. Two Sum (E) 167. Two Sum II - Input array is sorted (E)...

    leetcode浇花-LCSolutions:我的力扣解决方案

    leetcode 浇花力扣解决方案 简单的 #0001 - Two Sum #0007 - Reverse Integer #0009 - Palindrome Number #0035 - Search Insert Position #0058 - Length of Last Word #0066 - Plus One #0083 - Remove Duplicates...

    leetcode答案-LeetCode-Trip:LeetCode刷题代码,大佬勿入

    leetcode 答案 LeetCode-Trip LeetCode刷题代码,大佬勿入。 为一年后的研究生找工作准备 目标是BAT的算法岗哈哈哈哈哈 争取做到每日一更 嗯…… 19.10.22:鸽了这么久,我又回来了……主要在实验室天天没啥事,过于...

    leetcode530-alogritme-interview:alogritme-面试

    leetcode 530 ** 面试leetcode题目 ** python ** 第一章 数组基础 ** python 1-1 BinarySearch 1-2 即使简单的问题,也有很多优化的思路 283 27 26 80 1-3 三路快排partition思路的应用 Sort Color 75 88 215 1-4 ...

    LeetCode最全代码

    (https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [C++](./C++/remove-duplicates-from-sorted-array.cpp) [Python](./Python/remove-duplicates-from-sorted-array.py) | _O(n)_ | _O(1)_ |...

    javalruleetcode-LeetCode:LeetCode算法问题

    Input array is sorted LeetCode 344 Reverse String LeetCode 345 Reverse Vowels of a String 2 字符串 编号 题目 LeetCode 3 Longest Substring Without Repeating Characters LeetCode 13 Roman to Integer ...

    leetcode分类-leetcode-practice:LeetCode题解

    leetcode 分类 LeetCode 题解 同步自我的博客: 算法 双指针 题目 难度 原题 解答 167. Two Sum II - Input array is sorted Easy 633. Sum of Square Numbers Easy 345. Reverse Vowels of a String Easy 680. ...

    leetcode530-Play-with-Algorithms:基本的算法和数据结构

    leetcode 530 Play-with-Algorithms 基本的算法和数据结构 来自liuyubobobo老师的课程 章节 讲解例题 课程练习题 更多扩展练习 难题推荐 第一章 算法面试到底是什么鬼? [无] [无] 第二章 面试中的复杂度分析 [无] ...

    Coding Interview In Java

    14 Two Sum II Input array is sorted 49 15 Two Sum III Data structure design 51 16 3Sum 53 17 4Sum 55 18 3Sum Closest 57 19 String to Integer (atoi) 59 20 Merge Sorted Array 61 ... ... 231 Counting ...

    leetcodepushfront-leetcode:leetcode问题

    leetcode 推前当前问题 5 从 9 月 9 日到 12 月 9 日,按类型。 100 个主题和 Google。 力码 Leetcode 题目汇总 分类 1、求和问题 1.1 (1) Two Sum 1.2 (15) 3 Sum 1.3 (18) 4 Sum 1.4 (454) 4 Sum II 1.5 (167) Two...

    LeetCode各公司题目合集

    - Two Sum II - Input array is sorted:与前一个Two Sum题目的不同之处在于,本题假设输入数组已经是排序过的,因此可以采用双指针技巧来解决问题,提高效率。 - Maximal Square:寻找由1组成的最大正方形的面积,...

    Leetcode 题解.pdf

    双指针可以用来解决有序数组中的两数之和问题,例如 Leetcode 的 167 题 Two Sum II - Input array is sorted( Easy)。 在这个题目中,我们可以使用双指针来遍历数组,一个指针指向值较小的元素,一个指针指向值...

    leetcode打不开-leetcode:leetcode

    leetcode打不开Leetcode Note Tips Tip1: Two pointer for sorted array (#Array 1. Two Sum) Tip2: Sum[i:j] = Sum[0:j] - Sum[0:i] for continuous array (# Array 560. Subarray Sum Equals K) Tip3: Knapsack ...

    leetcode双人赛-leetcode:我解决的leetcode问题的解决方案

    leetcode双人赛力码 LTM 岛数:对每一个 1 的实例,执行 DFS 并将所有连接的设置为 0。 帕斯卡三角形:每个 arr[i][j] = arr[i-1][j] + arr[i-1][j-1]。 处理角落案例 包含重复:创建一个集合并检查元素是否已经存在...

    Leetcode book刷题必备

    ***o Sum II – Input Array Is Sorted:当数组已经排序时,如何找到两个数的和等于目标值。 ***o Sum III – Data Structure Design:设计一种数据结构,支持添加和查询两数之和。 4. Valid Palindrome:判断一个...

    LeetCode:LeetCode题解

    LeetCode LeetCode题解 传送门 # 标题 解决方案 困难 笔记 1个 简单的 ... Remove_Duplicates_From_Sorted_Array_II ... Best_Time_To_Buy_And_Sell_StockII ... Single_NumberII ... Two_Sum_II_Input_

Global site tag (gtag.js) - Google Analytics