LeetCode_167. Two Sum II - Input array is sorted
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.
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.
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.
package leetcode.easy;
public class TwoSumIIInputArrayIsSorted {
@org.junit.Test
public void test() {
int[] numbers = { 2, 7, 11, 15 };
int target = 9;
System.out.println(twoSum(numbers, target));
}
public int[] twoSum(int[] numbers, int target) {
for (int i = 0; i < numbers.length - 1; i++) {
for (int j = i + 1; j < numbers.length; j++) {
if (numbers[i] + numbers[j] == target) {
return new int[] { i + 1, j + 1 };
}
}
}
return new int[] { 0, 0 };
}
}
LeetCode_167. Two Sum II - Input array is sorted的更多相关文章
- leetcode2 Two Sum II – Input array is sorted
Two Sum II – Input array is sorted whowhoha@outlook.com Question: Similar to Question [1. Two Sum], ...
- 29. leetcode 167. Two Sum II - Input array is sorted
167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascendi ...
- 167. Two Sum II - Input array is sorted【easy】
167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...
- 167. Two Sum II - Input array is sorted@python
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...
- 【LEETCODE】38、167题,Two Sum II - Input array is sorted
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 167. Two Sum II - Input array is sorted - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
- [LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
随机推荐
- queue模块笔记
queue被称为消息队列,数据不会混乱,也可以用于复杂业务传递元素,队列是多线程的利器,其内部有锁的机制可以控制数据的统一且安全 queue.Queue()按照先进先出原则 queue.LifoQue ...
- unittest(一)IDE导出的代码分析
在 Python 语言下有诸多单元测试框架,如 unittest.Pytest.nose 等,其中 unittest 框架(原名 PyUnit 框架)为 Python 语言自带的单元测试框架,从 Py ...
- 文件读写(三)利用FileStream类操作字节数组byte[]、BinaryFormatter、内存流MemoryStream
一.Stream类概述 在.NET Framework中,文件和流是有区别的.文件是存储在磁盘上的数据集,它具有名称和相应的路径.当打开一个文件并对其进行读/写时,该文件就称为流(stream).但是 ...
- Python通过yagmail和smtplib模块发送简单邮件
SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件.python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是pytho ...
- 前端面试:Vue.js常见的问题
摘自今日头条用户:代码开发 原文链接: https://www.toutiao.com/a6683120112255369732/?tt_from=mobile_qq&utm_campaign ...
- DYNAMIC LINK LIBRARY - DLL
https://www.tenouk.com/ModuleBB.html MODULE BB DYNAMIC LINK LIBRARY - DLL Part 1: STORY What do we h ...
- 01_Tutorial 1: Serialization 序列化
1.序列化 1.官方教程 https://q1mi.github.io/Django-REST-framework-documentation/tutorial/1-serialization_zh/ ...
- 从linq的一次优化实践看group by+Min/Max()处理数据后需要额外的其他列问题
问题简化如下: 假设有第三方的表结构如下(可能会出现完全相同的重复数据): 1.写SQL语句,查询每个学生的,userid.最高分.最高分的科目.最高分的考点.最低分.最低分科目.最低分考点(分数相同 ...
- shell 编程整合
1.shell 浮点数运算与比较 https://www.cnblogs.com/shixun/p/6179642.html 将字符串比较的 = 改为数值比较的 -eq 更有效 2.linux she ...
- jq 字符串转数组
一般我们在添加关键词时 会添加几组关键词 上传时怎么取值呢 取值时用以下格式 就能取到值 var FTag = "" //AAA,BBB if (FTag1 != &qu ...