https://leetcode.com/problems/two-sum/
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[] res = null; Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < nums.length; i++) { map.put(nums[i], i); } for (int i = 0; i < nums.length; i++) { int a = target - nums[i]; Integer b = map.get(a); if (b != null && b > i) { res = new int[] { i + 1, b + 1 }; break; } } return res; } }
/** * @param {number[]} nums * @param {number} target * @return {number[]} */ var twoSum = function(nums, target) { var res = []; var hash = {}; for ( var i = 0; i < nums.length; i++) { hash[nums[i]] = i; } for ( i = 0; i < nums.length-1; i++) { var tmp = nums[i]; var v1 = hash[target-tmp]; if (v1 !== undefined && v1 !== i) { res.push(i+1); res.push(v1+1); break; } } return res; };
相关推荐
Two Sum算法调试小demo
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)...
本文给大家介绍的是C++实现two sum问题的暴力算法,蛮力解法意味着我们需要检查每一个数字与后面是否可以组成target,在数字量不大的情况下还是可以得到很好的效果,所以蛮力法的字符串匹配仍然是得到了实际生活中的...
twoSum0.zip
Leetcode 1 two sum C++ code
Leetcode two sum java 解法
twoSum问题的核心思想.md
手绘算法力扣 1 两数之和(Two Sum)
python python_leetcode面试题解之两数之和TwoSum
题目 Twosum 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。...
答案LeetCode_1_TwoSum LeetCode 问题:给定一个整数数组,找出两个数字,使它们相加为特定的目标数字。 函数 twoSum 应该返回两个数字的索引,使它们相加为目标,其中 index1 必须小于 index2。 请注意,您返回的...
js代码-1. Two Sum
leetcode leetcode练习 twosum 问题 ;add two numbers问题;reverse integer问题;最大不重复子字符串长度问题;atoi问题;
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 ...
1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to ...
leetcode-1-Two-Sum 这是我在 Github 的第一天。 我只是 AC leetcode 中的第一个问题。 从现在开始,我将使用Github在leetcode中记录我的生活。 我擅长 C++,但对 java 和 Python 不是很专业,我将使用最流行的 3 种...
Two Sum. Memory Usage: 5.8 MB, less than 99.36% of C online submissions for Two Sum. #1. Two Sum Given an array of integers nums and an integer target, return indices of the two numbers such that they...
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same ...
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same ...