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
public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] result = {-1, -1};
if (nums == null || nums.length <= 1)
return result;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(nums[0], 0);
for (int i = 1; i < nums.length; i++) {
if (map.containsKey(target - nums[i])) {
result[0] = map.get(target - nums[i]) + 1;
result[1] = i + 1;
break;
}
map.put(nums[i], i);
}
return result;
}
}
分享到:
相关推荐
001.two-sum │ ├── information.json │ ├── README.md ├── 002.add-two-numbers │ ├── information.json │ ├── README.md ... 有一些有用的选项: Options: -r, --rule crawling rule, ...
twoSum , cases : [ { in : [ [ 2 , 7 , 11 , 15 ] , 9 ] , out : [ 0 , 1 ] } , { in : [ [ 11 , 2 , 15 , 7 ] , 9 ] , out : [ 1 , 3 ] } , ] , } npm start 特征 忽视问题 如果导出ignore: true则可以忽略问题 ...
js代码-1. 两数之和 [简单] https://leetcode-cn.com/problems/two-sum
离线和leetcode leetcode-cli 一个享受 leetcode 的高效 cli 工具! 非常感谢 leetcode.com,一个非常棒的网站!...⦙⦙⦙⦙⦙⦙⦙⦙ ...leetcode.com。...leetcode ...leetcode ...leetcode ...leetcode ..../two-sum.cpp
离线和leetcode leetcode-cli 一个享受 leetcode 的高效 cli 工具! 非常感谢 leetcode.com,一个非常棒的网站!...⦙⦙⦙⦙⦙⦙⦙⦙ ...leetcode.com。...leetcode ...leetcode ...leetcode ...leetcode ..../two-sum.cpp
leetcode-1-Two-Sum 这是我在 Github 的第一天。 我只是 AC leetcode 中的第一个问题。 从现在开始,我将使用Github在leetcode中记录我的生活。 我擅长 C++,但对 java 和 Python 不是很专业,我将使用最流行的 3 种...
leetcode 答案 leetcode-machine-swift :SOS_button: 请帮助在Sources/leetcode-machine-swift/leetcode.swift设置所有 leetcode 问题。 :SOS_button: 在 ...利用自动补全、类型检查......Two Sum " , inputs : [([ 2 , 7
https://leetcode-cn.com/problems/two-sum/ 难度 : 简单 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。...
leetcode 2 leetcode-训练 算法训练。 java $: javac hello.java java ...leetcode ...leetcode ...leetcode ...leetcode ...1.two-sum.cpp leetcode test 1.two-sum.cpp leetcode test 1.two-sum.cpp -t '[1,2,3]\n3'
leetcode 只专注于解决方案! leetcode-helper是一个单 jar 库,它使您无需为每个问题设置解决方案测试脚手架。 生成解决方案/测试框架,编译,通过一行命令进行测试。 集成了Junit ...two_sum/ │ └──
leetcode编辑器leetcode-问题 自定义代码演示 leetcode 编辑器配置 代码文件名: $ ! velocityTool . camelCaseName(${question ...com.shuzijun.leetcode.editor.en...title ex:Two Sum ${question.titleSlug} question t
twoSum = function(nums, target) { const { length } = nums for(let i = 0; i < length - 1; i++) { for(let j = i + 1; j < length; j++) { if(nums[i] + nums[j] === target) { return [i, j] } } } } // ...
leetcode。 3sum leetcode-练习 算法实践 15. 3和 给定一个由 n 个整数组成的数组 nums,nums 中是否有元素 ...search_sum(target,num_idx,nums):#search ...two elements sum up to target ls=[] for i in ra
leetcode 答案力码 API 使用 TypeScript 编写的 Leetcode ..."two-sum" ) ; // Fetch more properties of this problem await problem . detail ( ) ; // Show problem content, test case, code snippet
Two-Sum 要点: - 利用java中Array对象的sort方法排序,使得整个数组呈升序状态 - 再利用两段取点相加的sum与target比较 - 若大于target,则后结点前移,sum变小 - 若小于target,则前结点后移,sum变大...
_001_TwoSum | | | | |-- twoSum.java | | | | |-- twoSum.kt | | | | |-- twoSum.md | |-- ...... 非常欢迎讨论和代码审查。 TwoSum.java在 Java 中提供了 OJ 接受的解决方案。 TwoSum.kt在 Kotlin 中提供 OJ 接受...
twoSum(int[] nums, int target) { Map table = new HashMap<>(); for (int i = 0; i < nums.length; i++){ table.put(nums[i], i); } int [] result = new int[2]; for (int i = 0; i < nums.length; i++)...
two-sum 删除排序数组中的重复项 remove-duplicates-from-sorted-array 移除元素 remove-element 搜索插入位置 search-insert-position 最大子序和 maximum-subarray 加一 plus-one 合并两个有序数组 merge-sorted-...
leetcode java 常用只专注于解决方案! leetcode-helper是一个单 jar 库,它使您无需为每个问题设置解决方案测试脚手架。 生成解决方案/测试框架,编译,通过一行命令进行测试。 集成了Junit ...two_sum/ │
判断链表是否为回文链表 leetcode leetCode-Exercise 个人 LeetCode 练习合集。 目前需要使用的数据结构 题解 Two Sum Given an array of ...twoSum(_ target: Int, elements: [Int]) -> [Int] { var