`

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;        
}

 

分享到:
评论

相关推荐

    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打不开-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:LeetCode题解

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

    word源码java-Play-with-Algorithm-Interview-Learningnotes:Play-with-Algori

    在LeetCode上解决第一个问题 Move Zeros 3-4 即使简单的问题,也有很多优化的思路 3-5 三路快排partition思路的应用 Sort Color 3-6 对撞指针 Two Sum II - Input Array is Sorted 3-7 滑动窗口 Minimum Size ...

Global site tag (gtag.js) - Google Analytics