给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。
你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。
示例:
给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]
解法:
class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { Integer other = map.get((target - nums[i])); if(other != null) { return new int[]{other, i}; } map.put(nums[i], i); } return null; } }
相关推荐
答案LeetCode_1_TwoSum LeetCode 问题:给定一个整数数组,找出两个数字,使它们相加为特定的目标数字。 函数 twoSum 应该返回两个数字的索引,使它们相加为目标,其中 index1 必须小于 index2。 请注意,您返回的...
leetcode_0001_two_sum.c leetcode_0011_max_area.c leetcode_0015_three_sum.c leetcode_0016_three_sum_closest.c leetcode_0018_four_sum.c : not ready leetcode_0026_remove_duplicates.c 27, 80, 283 ...
twoSum ( self , nums : List [ int ], target : int ) -> List [ int ]: # method 1 using hash map num_pair = {} for i , num in enumerate ( nums ): if target - num in num_pair : #line 6 return [ num_pair ...
001Sum_of_two_numbers 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用...
TwoSum2 DCP 75 是一个双指针问题,如果当前项为 0,则使用 p1 p2 指向开始和结束,然后与开始交换,如果当前项目为 2,则与结束交换。 167是同一个想法 02/01/2020 16 -3SumClosest 344 - ReverseString 16 类似于...
leetcode167 LeetCode167_Two-Sum-II 给定一个目标target,再数组中找到两个数,使其和为target,并返回对应数组中索引(从1开始)
leetcode 答案 LeetCode_Answer LeetCode刷刷题 twosum 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数 isPanlindrome 判断是否是回文数 reverseint 翻转整数,如 1234--4321
leetcode安卓The_LeetCode_Awakens Notice Naming Rule 资料夹 请把题号(三码) 放在题目前面,并用"_" ...TwoSum_sol.txt) 一般只要把自己的nickname 放在最后即可(ex: TwoSum_jordan.txt) Resource Data Structure
leetcode_001_two_sum_test.c 有 5 个断言 sh test.sh 001 ..... 添加 -a 以运行所有测试(助手除外)。 sh test.sh all leetcode_001_two_sum_test.c ..... leetcode_002_add_two_numbers_test.c .... 添加 -b 以...
[1_two_sum.cpp] [10_regular-expression-matching.cpp] [100_same-tree.cpp] [1001_sorted-merge-lcci.cpp] [101_对称树.cpp] [102_binary-tree-level-order-traversal.cpp] [103_binary-tree-zigzag-level-order-...
twoSum ( self , numbers : List [ int ], target : int ) -> List [ int ]: i = 0 j = len ( numbers ) - 1 while ( i < j ): if numbers [ i ] + numbers [ j ] == target : return [ i + 1 , j + 1 ] elif ...
两两认识leetcode 算法二和 无论您使用的是 LeetCode、AlgoExpert,还是仅使用我的网站,二数和编码问题可能是您将要做的第一个编码问题之一。 这个问题将带您超越基础知识,并介绍一些逻辑和...sum to the target. The
TwoSum test only gradle -Dtest.single=TestTwoSum test # run TwoSum test only with some informnation gradle -Dtest.single=TestTwoSum test --info 问题清单 问题 笔记 缩写 缩写用于命名标识符。 缩写 意义 ...
leetcode_kt_solution使用Kotlin在LeetCode上刷的译文项目相关建造工具:Gradle Kotlin版本:1.4.31 ...优点:简单链接: : class Solution1 { /* * * 使用哈希表的 key 来进行匹配,时间复杂度 O(n) */ fun twoSum
圆和椭圆重叠leetcode ——#158 尖端 关心特殊情况 从不同的方向思考 简单的 大批 1.Two Sum -> 使用哈希表避免遍历列表448.查找数组中消失的所有数字-> 1.建立缓冲区来计算数字| 2.使用数组作为带符号的缓冲区118....
Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring 6.ZigZag Conversion 7.Reverse Integer 8.String To Integer 9....
0001. Two Sum 0002. Add Two Numbers 0003. Longest Substring Without Repeating Characters 解题思路 使用 st[i] 保存以i结尾的最大非重复子串,外层循环遍历以 i 结尾的子串,内层循环根据st[i]检查是否有重复。...
Leetcode two sum java 解法
Sum 给出一个包含数字的数组,和一个目标数字,找到数组中哪两个数之和等于这个目标数,把数组坐标返回出来,同一个数不能用两次。 A: 加数1 + 加数2 = 和 通过 【和 - 加数】 得到另一个加数 循环数组内容,通过...
Sum 题目: 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 ...