两数之和 - LeetCode
2018-05-19 19:30
211 查看
题目: 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]
直接上答案:
/** * @param {number[]} nums * @param {number} target * @return {number[]} */ var nums = [2, 7, 11, 15], target = 9; var twoSum = function(nums, target) { for(var i in nums) { for (var j in nums) { if(i != j && (nums[i] + nums[j] == target)) { return [i * 1, j * 1]; } } } }; twoSum(nums, target);简易开端,趟路...
阅读更多
相关文章推荐
- [LeetCode] Sum of Two Integers 两数之和
- [leetcode, python] Two Sum 两数之和等于某数
- leetCode两数之和
- leetcode 1 Two Sum 两数之和
- LeetCode之两数之和
- 【leetcode74】Sum of Two Integers(不用+,-求两数之和)
- LeetCode两数之和
- 排序相关—— 相邻两数的最大差值( Maximum Gap-LeetCode)
- LeetCode 02. 两数相加(链表) 03.最大不重复的字符串
- [leetcode, python] Two Sum 两数之和等于某数
- 【leetcode74】Sum of Two Integers(不用+,-求两数之和)
- leetcode算法题:两数相加(Java实现-效率超过100%提交者)
- leetcode两数之和
- [LeetCode] Divide Two Integers 两数相除
- [leetcode, python] Two Sum 两数之和等于某数
- leetcode题目 寻找和为SUM的两数(O(nlogn)和O(n)解法)
- LeetCode 题目之 数组两数之和问题
- 【leetcode74】Sum of Two Integers(不用+,-求两数之和)
- LeetCode 1. Two Sum(两数之和)
- leetcode之 两数之和