原题地址:two-sum

题目描述:

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标。

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。

你可以按任意顺序返回答案。

示例 1:

输入:nums = [2,7,11,15], target = 9

输出:[0,1]

解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。

解答方法:

1.暴力方法:

遍历所有数的组合使其和等于target

时间复杂度是O(n^2)

代码如下:

 1 class Solution {
2 public int[] twoSum(int[] nums, int target) {
3 int ans[] = new int[2];
4 for(int i = 0; i< nums.length; i++){
5 for(int j = i + 1; j < nums.length; j++){
6 if(nums[i] + nums[j] == target){
7 ans[0] = i;
8 ans[1] = j;
9 }
10 }
11 }
12 return ans;
13 }
14 }

2.排序+双指针:

先将数组进行排序,然后通过双指针进行加和与target对比大小。

算法的时间复杂度是O(nlogn+n)=O(nlogn)

代码如下:

 1 class Solution {
2 public int[] twoSum(int[] nums, int target) {
3 int m=0, n=0, k, board=0;
4 int[] res= new int[2];
5 int[] tmpl=new int[nums.length];
6 System.arraycopy(nums,0,tmpl,0,nums.length);
7 Arrays.sort(nums);
8 for(int i=0, j=nums.length-1; i<j;){
9 if(nums[i]+nums[j]<target){
10 i++;
11 }
12 else if(nums[i]+nums[j]>target){
13 j--;
14 }
15 else if(nums[i]+nums[j]==target){
16 m=i;
17 n=j;
18 break;
19 }
20 }
21 for(k=0; k<nums.length; k++){
22 if(tmpl[k] == nums[m]){
23 res[0]=k;
24 break;
25 }
26 }
27 for(int i=0; i<nums.length; i++){
28 if(tmpl[i] == nums[n]&&i!=k){
29 res[1]=i;
30 break;
31 }
32 }
33 return res;
34 }
35 }

3.HashMap:

先将数组存储在一个哈希表中,建立数字和其坐标位置之间的映射。在通过遍历数组,用target减去该数字,就得到了要在哈希表中查找的数字。最后记录下这两个数字的下标输出即可。

时间复杂度为O(n),同时空间复杂度也是O(n)

代码如下:

 1 class Solution {
2 public int[] twoSum(int[] nums, int target) {
3 HashMap<Integer, Integer> m = new HashMap<Integer,Integer>();
4 int res[] = new int[2];
5 for(int i = 0; i < nums.length; i++){
6 m.put(nums[i], i);
7 }
8 for(int i = 0; i < nums.length; i++){
9 int t = target - nums[i];
10 if(m.containsKey(t) && m.get(t) != i){
11 res[1] = i;
12 res[0] = m.get(t);
13 }
14 }
15 return res;
16 }
17 }

[LeetCode]1.Two Sum 两数之和(Java)的更多相关文章

  1. [LeetCode] 1. Two Sum 两数之和

    Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...

  2. [LeetCode]1.Two Sum 两数之和&&第一次刷题感想

    ---恢复内容开始--- 参考博客: https://www.cnblogs.com/grandyang/p/4130379.html https://blog.csdn.net/weixin_387 ...

  3. [LeetCode] 1.Two Sum 两数之和分析以及实现 (golang)

    题目描述: /* Given an array of integers, return indices of the two numbers such that they add up to a sp ...

  4. 【LeetCode】Two Sum(两数之和)

    这道题是LeetCode里的第1道题. 题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会 ...

  5. [leetcode]1. Two Sum两数之和

    Given an array of integers, return indices  of the two numbers such that they add up to a specific t ...

  6. 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 ...

  7. 【LeetCode】1. Two Sum 两数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...

  8. Leetcode:0002(两数之和)

    LeetCode:0002(两数之和) 题目描述:给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表.你可以假设除了数字 0 之外,这两 ...

  9. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

随机推荐

  1. javascript中new url()属性,轻松解析url地址

    1.首先写一个假的地址(q=URLUtils.searchParams&topic=api)相当于当前的window.location.href const urlParams = new U ...

  2. LG1290 欧几里德的游戏

    https://www.luogu.com.cn/problem/P1290 博弈论游戏,用到mod. 辗转相除法的过程,会构成n种状态. 到达最后一个状态就赢了. 对于一次过程如果div>1那 ...

  3. 若依(ruoyi)代码生成树表结构的那些坑

    若依(RuoYI)代码生成树表结构的那些坑 相信许多做后端开发的同学,一定用过若依这款框架,这款框架易上手,适合用来做后台管理系统,但是其中也存在一些坑,稍不注意就会中招(大佬可以忽略...) 今天, ...

  4. 【Java常用类】Calendar

    Calendar日历类 实例化 方法一 创建其子类**(GregorianCalendar)**的对象 方法二 调用其静态方法getInstance() Calendar instance = Cal ...

  5. Solon 开发

    Solon 开发 一.注入或手动获取配置 二.注入或手动获取Bean 三.构建一个Bean的三种方式 四.Bean 扫描的三种方式 五.切面与环绕拦截 六.提取Bean的函数进行定制开发 七.自定义注 ...

  6. leetcode 54. 螺旋矩阵 及 59. 螺旋矩阵 II

    54. 螺旋矩阵 问题描述 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, ...

  7. 【刷题-LeetCode】122 Best Time to Buy and Sell Stock II

    Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...

  8. 模拟axios的创建[ 实现调用axios()自身发送请求或调用属性的方法发送请求axios.request() ]

    1.axios 函数对象(可以作为函数使用去发送请求,也可以作为对象调用request方法发送请求) ❀ 一开始axios是一个函数,但是后续又给它添加上了一些属性[ 方法属性] ■ 举例子(axio ...

  9. unity3d之public变量引发错误

    public变量引发错误 在vs ide中怎么更改也无效 后来发现public里面的值一直不改变,手动改之.

  10. 带你十天轻松搞定 Go 微服务系列(八、服务监控)

    序言 我们通过一个系列文章跟大家详细展示一个 go-zero 微服务示例,整个系列分十篇文章,目录结构如下: 环境搭建 服务拆分 用户服务 产品服务 订单服务 支付服务 RPC 服务 Auth 验证 ...