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 ...
随机推荐
- flask中使用ajax 处理前端请求 弹框展示
菜小鱼初次使用 ajax,想前端提交数据,后端处理后,将结果以弹框的形式展示,在网上查看了好多,不停的调试,终于调通了 html: <html> <head></head ...
- [2019南昌邀请赛网络赛D][dp]
https://nanti.jisuanke.com/t/38223 Xiao Ming recently indulges in match stick game and he thinks he ...
- Java8实战,
Supplier 1, @FunctionalInterfacepublic interface Supplier<T> { 2, T get(); 3, Supplier<A ...
- Nginx 负载均衡条件下 Redis 共享Session (Java)(二)
关于Session的问题 网上有各个方面的针对,有在nginx 上处理: ip_hash的session,有在 tomcat 做处理:修改Context文件,有针对项目做处理.本篇就是对项目处理 1. ...
- 发布一个npm package
1. 创建一个package.json文件 发布到npm registry的包必须包含一个packge.json文件. 1. 必需name字段 要求: 1. 只能是一个单词,但是可以包含-或_ 2. ...
- JAVA的选择结构
1.基本选择结构if 案例:如果Java考试成绩大于98分则奖励MP4 public class Demo02 { public static void main ...
- Greenplum 如何直连segment节点
Greenplum 展开阅读全文 直连greenplum segment节点的方法, utility模式 : 使用这种方式,不与其他节点通讯,只操作当前节点.也没有数据分布的概念. 如果使用uti ...
- learning scala implicit class
隐式类可以用来扩展对象的功能非常方便 example: object ImplicitClass_Tutorial extends App { println("Step 1: How to ...
- 用python写一个GitHub Trending Api
GitHub 给了开发者相当丰富的 API 接口 https://developer.github.com/v3/,包括认证,搜索,活动等接口,但就是没有提供获取 Trending 的接口.因此,需要 ...
- [转]C++ 模板 静态成员 定义(实例化)
提出问题: 如果有这样一个模板: template <typename T> class Test{ public: static std::string info; }; 对于以下若干种 ...