两数之和LeetCode
nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
示例:
给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
" class="notranslate">
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
示例:
给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
/**
* 无脑暴力:
* 时间复杂度 : n^2
* 空间复杂度 : 1
* 70ms
* 38.5MB
*/
public static int[] traverseTwoSum(int[] nums, int target){
int len = nums.length;
for (int i = 0; i < len; i++) {
for (int j = i+1; j < len; j++) {
if(nums[i]+nums[j]==target){
return new int[]{i,j};
}
}
}
return null;
}
/**
* 因为哈希表的查找速度非常的快,我们可以采用hash表去解决问题.
* 时间: n
* 空间: n
* 4ms
* 36.2MB
*
*/
public static int[] secondHashTwoSum(int[] nums, int target){
Map<Integer, Integer> map = new HashMap<>();
int len = nums.length;
for (int i = 0; i < len; i++) {
// 根据键去找 值
map.put(nums[i],i);
}
int findNum;
for (int i = 0; i < len; i++) {
findNum = target - nums[i];
if(map.containsKey(findNum)&&map.get(findNum)!=i){
return new int[]{i,map.get(findNum)};
}
}
return null;
}
/**
* 改进为一次hash
* 时间: n
* 空间: n
* 4ms
* 36.2MB
*
*/
public static int[] firstHashTwoSum(int[] nums, int target){
Map<Integer, Integer> map = new HashMap<>();
int len = nums.length,findNum;
for (int i = 0; i < len; i++) {
findNum = target-nums[i];
if(map.containsKey(findNum)){
return new int[] {map.get(findNum),i};
}
map.put(nums[i],i);
}
return null;
}
两数之和LeetCode的更多相关文章
- [Java]1.两数之和 - LeetCode
1 题目 2 思路与代码 思路一:暴力法(两层For循环) 时间复杂度:O(n^2) 对于每个元素,我们试图通过遍历数组的其余部分来寻找它所对应的目标元素,这将耗费 O(n) 的时间.因此时间复杂度为 ...
- 1. 两数之和 LeetCode
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [, , , ], target = 因为 n ...
- 两数之和 [ leetcode ]
原题地址:https://leetcode-cn.com/articles/two-sum/ 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元 ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- LeetCode 371. Sum of Two Integers (两数之和)
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- 两数之和,两数相加(leetcode)
我们都知道算法是程序员成长重要的一环,怎么才能提高算法呢, 出来在网上看视频之外,动手练习是非常重要的.leetcode 就是一个非常好的锻炼平台. 1. 两数之和,在 leetcode 里面是属于 ...
随机推荐
- luoguP3066 [USACO12DEC]逃跑的BarnRunning
luoguP3066 [USACO12DEC]逃跑的BarnRunning 题目大意 给定一棵n个节点的树和参数L,查询每个节点子树中到达该节点距离<=L的数量(包括该节点) 偏模板的主席树 P ...
- 小米系列手机调试Installation failed with message Failed to establish session
用Android studio 2.3调度程序时提示"Installation failed with message Failed to establish session"错误 ...
- hihocoder 微软编程之美2015 初赛 第一场 (树算法 + 暴力思想 + 搜索思想)
题目1 : 彩色的树 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定一棵n个节点的树,节点编号为1, 2, …, n.树中有n - 1条边,任意两个节点间恰好有一条路 ...
- sqlite:多线程操作数据库“database is locked”解决方法
1. 使sqlite支持多线程(不确定是否非加不可,暂且加上,以备后患) 可以在编译时/启动时/运行时选择线程模式,参考:http://www.cnblogs.com/liaj/p/4015219.h ...
- JNDI数据源配置
一.数据源的由来 在Java开发中,使用JDBC操作数据库的四个步骤如下: ①加载数据库驱动程序(Class.forName("数据库驱动类");) ②连接数据库(Connec ...
- ctypes模块与pywin32模块
ctypes模块: 主要用于调用c动态链接库. 1.聊聊Python ctypes模块 2.ctypes模块管理 相关网址: pywin32模块: 用于访问win32API函数(win32api模块) ...
- [算法]从Trie树(字典树)谈到后缀树
我是好文章的搬运工,原文来自博客园,博主July_,地址:http://www.cnblogs.com/v-July-v/archive/2011/10/22/2316412.html 从Trie树( ...
- HDU6118:度度熊的交易计划(入门级最小费用可行流)
度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题: 喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区. 由于生产能力的区别,第i个片区能够花费a[i]元生产1个商品,但 ...
- 【Lintcode】088.Lowest Common Ancestor
题目: Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two n ...
- CF285 E Positions in Permutations——“恰好->大于”的容斥和允许“随意放”的dp
题目:http://codeforces.com/contest/285/problem/E 是2018.7.31的一场考试的题,当时没做出来. 题解:http://www.cnblogs.com/y ...