[LeetCode] 1. Two 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 the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
解法1:最直接最笨的办法,遍历数组中的每一个数,从它之后的数中寻找是否有满足条件的,找到后跳出循环并返回。由于需要两次遍历,时间复杂度为O(n2),空间复杂度为O(1)。
public class Solution {
public int[] twoSum(int[] nums, int target) {int result[] = new int[]{-1, -1};
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j ++) {
if (nums[i] + nums[j] == target) {
result[0] = i;
result[1] = j;
break;
}
}
if ((result[0] != -1) && (result[1] != -1)) {
break;
}
}
return result;
}
}
解法2-1: 先遍历一遍数组,将每个数字存到hash表中,然后再遍历一遍,查找符合要求的数。由于存储和遍历的操作时间复杂度都是O(n),所以总体时间复杂度为O(n),而空间复杂度为O(n)。
public class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> numHash = new HashMap<>();
int result[] = new int[2];
for (int i = 0; i < nums.length; i++) {
numHash.put(nums[i], i);
} for (int i = 0; i < nums.length; i++) {
int other = target - nums[i];
if (numHash.containsKey(other) && numHash.get(other) != i) {
result[0] = i;
result[1] = numHash.get(other);
break;
}
}
return result;
}
}
解法2-2:将2-1的两次循环合并,每次先判断hashmap中是否有满足条件的数,没有的话再将当前数写入hashmap中,进行下一次循环。
public class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> numHash = new HashMap<>();
int result[] = new int[2];
for (int i = 0; i < nums.length; i++) {
int other = target - nums[i];
if (numHash.containsKey(other)) {
result[0] = i;
result[1] = numHash.get(other);
break;
}
numHash.put(nums[i], i);
}
return result;
}
}
解法3: 先将数组拷贝(O(n))后采用Arrays.sort()方法进行排序,排序的时间复杂度为O(nlogn)。然后采用二分搜索法查找(O(n)),最后将找出的结果在原数组中查找其下标(O(n)),所以整体时间复杂度为(O(nlogn))。
public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2]; int[] copyList = new int[nums.length];
System.arraycopy(nums, 0, copyList, 0, nums.length);
Arrays.sort(copyList); int low = 0;
int high = copyList.length - 1;
while(low < high) {
if (copyList[low] + copyList[high] < target) {
low++;
} else if (copyList[low] + copyList[high] > target) {
high--;
} else {
result[0] = copyList[low];
result[1] = copyList[high];
break;
}
} int index1 = -1;
int index2 = -1;
for (int i = 0; i < nums.length; i++) {
if ((index1 == -1) && (nums[i] == result[0])) {
index1 = i;
} else if ((index2 == -1) && (nums[i] == result[1])) {
index2 = i;
}
}
result[0] = index1;
result[1] = index2;
Arrays.sort(result);
return result;
}
}
[LeetCode] 1. Two Sum ☆的更多相关文章
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [leetCode][013] Two Sum 2
题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- LeetCode one Two Sum
LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 437. Path Sum III_ Easy tag: DFS
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
随机推荐
- 在64位的环境下利用Jet来操作Access,Excel和TXT
For example, you have a 32-bit application that uses the Microsoft OLE DB Provider for Jet. If you m ...
- vuex介绍--一篇看懂vuejs的状态管理神器
原文,请点击此链接http://www.ituring.com.cn/article/273487
- AWVS使用基础教程
什么是AWVS Acunetix Web Vulnerability Scanner(简称AWVS)是一款知名的网络漏洞扫描工具,它通过网络爬虫测试你的网站安全,检测流行安全漏洞,现已更新到10.(下 ...
- 从大量的IP访问记录中找到访问次数最多的IP
1.内存不受限 一个IP有32bit(4Byte),1GB=10亿,那么在4GB内存的情况下,可以存10亿个IP.用HashMap,边存入IP边维护一个最大次数,这样遍历一遍就可以求出,时间复杂度为O ...
- linux后台运行之screen和nohup
3.1 nohup命令 如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令. 该命令可以在你退出帐户/关闭终端之后继续运行相应的进程. nohup就是不挂起的意 ...
- MySQL中使用trim()删除两侧字符
在某些情况下由于程序没处理好,导致数据表中有些字段的值会有空白字符出现,如 这样如果在严格比对name时会匹配不到.trim()函数可以解决这样的问题 作为trim()函数的子集,ltrim()函数是 ...
- [C/C++] C/C++错题集
1. 解析: A:在GCC下输出:0 在VC6.0下输出:1 B:在GCC下输出:段错误 (核心已转储) 在VC6.0下输出:已停止工作,出现了一个问题,导致程序停止正常工作. C:正常 ...
- DBGrid相关技术整理
DBGrid相关技术整理: 注:对于DBGrid相关属性.方法的学习融入到技术整理过程中 一,多选 设置属性: Options->dgMultiSelect = True; ->dgRow ...
- sql语句中的insert 和 insert into 的区别?into有什么用?
insert into tableName values(........) insert tableName (字段名1,字段名2,...)values(......)看语句结构就知道区别了 .in ...
- BZOJ2151 种树(贪心+堆+链表/wqs二分+动态规划)
dp容易想到,但没法进一步优化了. 考虑贪心,每次选出价值最大的物品.但这显然是不对的因为会影响其他物品的选择. 于是考虑加上反悔操作.每次选出一个物品后,将其相邻两物品删除,再将原物品价值变为相邻两 ...