Leetcode(1)之两数之和
2019-12-30 15:27
483 查看
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
题目描述:
解题思路:
使用哈希表, 遍历列表时先检查哈希表ht里有没有 target - nums[i] 的 key,如果有的话就返回由 ht[target - nums[i]] 的值和 i 组成的数组。
代码:
private int[] TwoSum(int[] nums, int target) { int[] ret = new int[2]; Hashtable ht = new Hashtable(); for (int i = 0; i < nums.Length; i++) { if (ht.ContainsKey(target - nums[i])) { ret[0] = (int)ht[target - nums[i]]; ret[1] = i; return ret; } ht[nums[i]] = i; } return null; }
相关文章推荐
- LeetCode 2. 两数相加
- leetcode 两数相加
- Leetcode每日一道 -- 两数相加
- LeetCode-探索-初级-数组-两数之和-java
- Leetcode第一题:两数之和(3种语言)
- LeetCode刷题笔记之1:两数之和Python
- [leetcode, python] Two Sum 两数之和等于某数
- 【LeetCode】第二题:两数相加
- 【leetcode74】Sum of Two Integers(不用+,-求两数之和)
- Datawhale-LeetCode集训打卡-两数之和
- 1、LeetCode_C++题:两数之和
- 两数之和 - LeetCode
- Leetcode两数之和算法与分析
- LeetCode刷题记: 两数相加
- Leetcode刷题笔记 两数之和
- [leetcode]两数相加
- LeetCode 2. 两数相加
- 【leetcode系列】167. 两数之和 II - 输入有序数组
- leetcode_01. 两数之和
- [Leetcode] 2. 两数相加 java