LeetCode第一题:Two Sum
来源:互联网 发布:java获取request对象 编辑:程序博客网 时间:2024/06/05 19:43
Two Sum 题目简述
题目链接:https://leetcode.com/problems/two-sum/?tab=Description
题目原文:
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 element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1].
题目大意:
给定一个整型数组nums[]和一个整型数target,编写程序在nums数组中找出两个元素 a ,b ,使 a + b = target 。返回 a 和 b 在nums中的数组下标。
解法一:直接遍历,复杂的O(N^2)
思路:直接使用两层循环遍历数组,判断给定数组中任意两个元素之和是否与给定整型数target相等。
代码(C语言):int* twoSum(int* nums, int numsSize, int target) { int* indices = (int*)malloc(2*sizeof(int));for (int i = 0; i<numsSize; i++) {for (int j = i + 1; j< numsSize; j++) {if (*(nums + i) + *(nums + j) == target) {indices[0] = i;indices[1] = j;return indices;}}}return indices;}
解法二:哈希表法,复杂的O(N)
思路:
题目其实就是要在数组中查找一个与数组某元素之和等于target的数。所以本质上是一个查找问题!提高查找效率的基本方法就是二分查找法和哈希表法,二分查找需要序列有序,在此题中显然不宜。因此自然想到要用hash表法解决此题。
我们只需要遍历数组,对遍历到的元素nums[i],判断 target - nums[i]是否在哈希表中存在。如果不存在就将nums[i]加入哈希表。如果存在则说明哈希表中存在对应元素,与nums[i]之和为target。返回 i 与 哈希表中元素对应的数组下标即可。
由于hash查找的时间复杂度为O(1),故整个程序时间复杂度为O(n)。
代码(java):
public class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); int[] indices = new int[2]; int sub; for (int i=0 ; i < nums.length ; i++){ sub = target - nums[i]; if(map.containsKey(sub)){ indices[0] = map.get(sub); indices[1] = i; return indices; } map.put(nums[i] , i); } return indices; }}
int* twoSum(int* nums, int numsSize, int target) { int* indices = (int*)malloc(2*sizeof(int));for (int i = 0; i<numsSize; i++) {for (int j = i + 1; j< numsSize; j++) {if (*(nums + i) + *(nums + j) == target) {indices[0] = i;indices[1] = j;return indices;}}}return indices;}
解法二:哈希表法,复杂的O(N)
思路:
题目其实就是要在数组中查找一个与数组某元素之和等于target的数。所以本质上是一个查找问题!提高查找效率的基本方法就是二分查找法和哈希表法,二分查找需要序列有序,在此题中显然不宜。因此自然想到要用hash表法解决此题。
我们只需要遍历数组,对遍历到的元素nums[i],判断 target - nums[i]是否在哈希表中存在。如果不存在就将nums[i]加入哈希表。如果存在则说明哈希表中存在对应元素,与nums[i]之和为target。返回 i 与 哈希表中元素对应的数组下标即可。
由于hash查找的时间复杂度为O(1),故整个程序时间复杂度为O(n)。
代码(java):
public class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); int[] indices = new int[2]; int sub; for (int i=0 ; i < nums.length ; i++){ sub = target - nums[i]; if(map.containsKey(sub)){ indices[0] = map.get(sub); indices[1] = i; return indices; } map.put(nums[i] , i); } return indices; }}
0 0
- leetcode 第一题 two sum
- LeetCode 第一题,Two Sum
- leetcode第一题two sum
- Two Sum leetcode第一题
- two Sum leetCode第一题
- leetcode 第一题:two sum
- LeetCode第一题Two Sum
- Leetcode第一题--------- Two Sum
- Leetcode第一题<two sum>
- LeetCode第一题:Two Sum
- leetcode------第一题 Two Sum
- LeetCode 1.Two Sum ------------LeetCode 第一题
- Leetcode 第一题 Two Sum java代码
- Leetcode 之第一题 two sum
- Leetcode oj1 第一题 two sum
- 第一题: Two Sum
- 第一题 two-sum
- leetcode第一题(medium)——Two Sum
- JAVA IO学习笔记
- .Net遇到的问题以及解决方法
- .Net遇到的问题以及解决方法
- android属性动画基础
- c++简单计算器
- LeetCode第一题:Two Sum
- 61. Rotate List(unsolved)
- android全屏启动页[解决黑屏白屏]
- java 内存泄漏
- Mybatis中配置Mapper的方法
- hadoop mapreduce
- Unity3D之Mecanim动画系统学习笔记(九):Blend Tree(混合树)
- 学习python[3] python 读取中文文件名/中文路径
- 欢迎使用CSDN-markdown编辑器