Two Sum
来源:互联网 发布:淘宝卖的七彩小驴 编辑:程序博客网 时间:2024/06/07 18:44
Given an array of integers, 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
specific 具体的
assume 假定
意思是:
给定一个整数数组,找出其中两个数满足相加等于你指定的目标数字。
这个方法twoSum必须要返回能够相加等于目标数字的两个数的索引,且index1必须要小于index2。请注意一点,你返回的结果(包括index1和index2)都不是基于0开始的。你可以假设每一个输入肯定只有一个结果。
public int[] twoSum(int[] numbers, int target) { int[] result = new int[2]; Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < numbers.length; i++) { if (map.containsKey(target - numbers[i])) { result[1] = i + 1; result[0] = map.get(target - numbers[i]); return result; } map.put(numbers[i], i + 1); } return result;}
阅读全文
0 0
- Two Sum
- Two Sum
- Two Sum
- two sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- CentOS 7 主机名的修改
- 一图读懂ZStack协议栈的基本架构和工作机理
- redis实现消息队列
- [BZOJ4650][NOI2016]优秀的拆分 各数据点解法
- Python学习【1】
- Two Sum
- 多线程和网络编程
- 12月3日
- Hadoop初识
- 函数与宏
- 拥有csdn博客的第一天
- 【Java 集合】List(ArrayList、Vector、LinkedList)、Map(HashMap、HashTable、LinkedHashMap和TreeMap)
- hive学习之hive的常用使用方式
- X210BV3S开发板刷机操作