[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].
Solution1: Brute-force
Lock pointer i, move pointer j to check if nums[j] == target - nums[i].
code:
/* Time Complexity: O(n*n)
Space Complexity: O(1)
*/
class Solution {
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] == target - nums[i]) {
return new int[] { i, j };
}
}
}
return null;
}
}
Solution2: HashMap
Use a map to record the index of each element and check if target - nums[i] is in the map. if so, we find a pair whose sum is equal to target.
code:
/* Time Complexity: O(n)
Space Complexity: O(n)
*/
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i< nums.length; i++){
if(map.containsKey(target - nums[i])){
return new int[]{i, map.get(target - nums[i])};
}else{
map.put(nums[i], i);
}
}
return null;
}
}
[leetcode]1. Two Sum两数之和的更多相关文章
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- [LeetCode]1.Two Sum 两数之和&&第一次刷题感想
---恢复内容开始--- 参考博客: https://www.cnblogs.com/grandyang/p/4130379.html https://blog.csdn.net/weixin_387 ...
- [LeetCode] 1.Two Sum 两数之和分析以及实现 (golang)
题目描述: /* Given an array of integers, return indices of the two numbers such that they add up to a sp ...
- 【LeetCode】Two Sum(两数之和)
这道题是LeetCode里的第1道题. 题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会 ...
- [LeetCode]1.Two Sum 两数之和(Java)
原题地址:two-sum 题目描述: 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标. 你可以假设每 ...
- LeetCode刷题 1. Two Sum 两数之和 详解 C++语言实现 java语言实现
1. Two Sum 两数之和 Given an array of integers, return indices of the two numbers such that they add up ...
- 【LeetCode】1. Two Sum 两数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...
- Leetcode:0002(两数之和)
LeetCode:0002(两数之和) 题目描述:给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表.你可以假设除了数字 0 之外,这两 ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
随机推荐
- Linux touch命令详解
Linux touch命令 Linux touch命令用于修改文件或者目录的时间属性,包括存取时间和更改时间.若文件不存在,系统会建立一个新的文件. 用法: touch [-acfm][-d<日 ...
- mig_7series_v4_0_data_gen_chk
mig_7series_v4_0_data_gen_chk `timescale 1ns / 1ps ///////////////////////////////////////////////// ...
- 作业-JSP简单入门
说明 本次作业不打分,仅作为大家自学的指导. 本次实验内容以"JSP实验参考文件"为主. 参考资料 Java教学问卷调查,有什么想说的,请尽情投票吧! 反射实验参考文件 JSP实验 ...
- tinycc update VERSION to 0.9.27
TinyCC全称为Tiny C Compiler, 是微型c编译器,可在linux/win/平台上编译使用. 在用代码里面使用tcc当脚本,性能比lua还快,目前已有网游服务端使用TCC脚本提高性能. ...
- 一台电脑上配置多个tomcat同时运行
好使 1 1.配置运行tomcat 首先要配置java的jdk环境,这个就不在写了 不懂去网上查查,这里主要介绍再jdk环境没配置好的情况下 如何配置运行多个tomcat 2.第一个tomcat: ...
- nginx ------反向代理和负载均衡
最近由于公司的业务增长 服务器承受不住压力经常出现崩溃现象 为了解决 使用nginx的负载均衡解决,以下是操作步骤: 1.nginx 的负载均衡:将压力分散到不同的机器上 nginx不单可以作为强大的 ...
- jquery中ajax使用error调试错误的方法
JQuery使我们在开发Ajax应用程序的时候提高了效率,减少了许多兼容性问题,我们在Ajax项目中,遇到ajax异步获取数据出错怎么办,我们可以通过捕捉error事件来获取出错的信息. jquery ...
- postgresql批量备份和恢复数据表
备份数据库:pg_dump -h localhost -U root demo02 > /home/arno/dumps/demo02.bak 恢复数据库:psql -h localhost - ...
- 编写Servlet 实例 -Shopping网站时,遇到的几个问题
问题一.在Web 上运行时,用JDBC链接MySQL总是出错,一直出现驱动加载失败 ------提示java.lang.ClassNotFoundException.解决方案:将数据库驱动jar文件导 ...
- leetcode32
class Solution { public: int longestValidParentheses(string s) { ; stack<int> st; ; i < n; ...