LeetCode one Two Sum (JAVA)

简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数.

问题详解:

给定一个数据类型为int的数组,一个数据类型为int的目标值target,要求是在数组中寻求两个数字,使得两个数求和的值等于给定的target,要求以数组返回符合条件的索引.

技巧:可以假设数组中没有重复元素,因为我们只返回一个符合条件的数组,所以数组中只需要一个符合要求的数字就可以了.

举例:

给定数组 nums={5,8,7,2,11},target= 10,

因为nums[1]+nums[3]=8+2=10,

所以返回[1,3].

注意:

1.数组下标不能重复

错解:nums[0]+nums[0]=5+5=10,

返回[0,0].

2.要考虑到没有符合的结果时的处理.

JAVA实现方法一:暴力遍历,两层循环,不推荐(第一次愚蠢实现)

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 };

}

}

}

throw new IllegalArgumentException("No two sum solution");

}

学习之路:

官方实现一 : 双层遍历.

优点:

1.代码的简洁性.

2.用throw方法处理无结果.

public int[] twoSum(int[] nums, int target) {

Map<Integer, Integer> map = new HashMap<>();

for (int i = 0; i < nums.length; i++) {//存放数组数据

map.put(nums[i], i);

}

for (int i = 0; i < nums.length; i++) {//判断结果

int complement = target - nums[i];

if (map.containsKey(complement) && map.get(complement) != i) {

return new int[] { i, map.get(complement) };

}

}

throw new IllegalArgumentException("No two sum solution");

}

复杂度分析:

时间复杂度 :近似于O(n^2):双层遍历,方法执行了n*n次.

空间复杂度 :近似于 O(1):变量的定义为常数.

官方实现二 : 利用HashMap两次遍历

先存放数组数据,再判断Map数据.

优点:

1.利用Hash表通过空间交换时间的方法提高查找时间.

2.因为不需要两个重复的数字,所以不会与Map中键的特性冲突.

public int[] twoSum(int[] nums, int target) {

Map<Integer, Integer> map = new HashMap<>();

for (int i = 0; i < nums.length; i++) {//存放数组数据

map.put(nums[i], i);

}

for (int i = 0; i < nums.length; i++) {//判断结果

int complement = target - nums[i];

if (map.containsKey(complement) && map.get(complement) != i) {

return new int[] { i, map.get(complement) };

}

}

throw new IllegalArgumentException("No two sum solution");

}

复杂度分析:

时间复杂度 :近似于O(n):都是单层遍历,方法执行了n次.

空间复杂度 :近似于 O(n):一次遍历,变量定义了n次.

LeetCode官方实现三 : 利用HashMap两次遍历

在存放数据的过程中判断结果.

优点:

1.提高了代码的简洁性

public int[] twoSum(int[] nums, int target) {

Map<Integer, Integer> map = new HashMap<>();

for (int i = 0; i < nums.length; i++) {

int complement = target - nums[i];

if (map.containsKey(complement)) {//满足条件直接返回结果,不继续遍历存数据

return new int[] { map.get(complement), i };

}

map.put(nums[i], i);

}

throw new IllegalArgumentException("No two sum solution");

}

复杂度分析:

时间复杂度 :近似于O(n):一次遍历,方法执行了n次.

空间复杂度 :近似于 O(n):一次遍历,变量定义了n次.

随记:

length——数组的属性;

length()——String的方法;

size()——集合的方法;

put()——Map的添加方法 ;

add()——Collection的添加方法;

小白刷题之路,请多指教— — 要么大器晚成,要么石沉大海

LeetCode one Two Sum的更多相关文章

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

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. [leetCode][013] Two Sum 2

    题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...

  4. [LeetCode] #167# Two Sum II : 数组/二分查找/双指针

    一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...

  5. [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针

    一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...

  6. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  7. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  8. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  9. [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 ...

随机推荐

  1. Python之函数对象、函数嵌套、名称空间与作用域、闭包函数、装饰器

    目录 一 函数对象 二 函数的嵌套 三 名称空间和作用域 四 闭合函数 五 装饰器 一.函数对象 1.函数是第一类对象 #第一类对象的特性:# 可以被引用 # 可以当做参数传递 # 返回值是函数 # ...

  2. Excel:6种多条件查找方法

    如下图所示,要求根据设备分类和品牌来查找相应的销售数量. 1. 使用VLOOKUP+辅助列进行多条件查找 本例采用的方法是在原表的最前面加一辅助列,辅助列的公式为:=B2&C2 然后再采用VL ...

  3. eslint相关工具

    eslint工具 1. vscode搜索eslint安装,就可以在写代码时报eslint的错误了 2.文件 --> 首选项 --> 设置 --> 选ESLint --> 勾选A ...

  4. u-boot(三)启动文件

    目录 u-boot(三)启动文件 汇编 C:_start_armboot 代码摘要 C:main_loop 内核启动 菜单处理(自定义实现) 命令处理 title: u-boot(三)启动文件 tag ...

  5. Word 测试下发布博客

    目录 语法.    3 NULL,TRUE,FALSE    3 大小端存储    4 类型转换    4 转义字符    5 运算符的优先级    5 表达式(a=b=c)    7 *pa++=* ...

  6. nginx request_time 和upstream_response_time

    1.request_time 官网描述:request processing time in seconds with a milliseconds resolution; time elapsed ...

  7. 简单的jvm优化的尝试

    一.eclipse 启动优化 在日常的开发中发现eclipse 启动的时候非常慢,并且在实际的开发中也非常卡,所以尝试着优化一下.现在eclipse 是运行在jdk1.7上. 首先我们可以看到ecli ...

  8. Docker 概述

    Docker 是一个开源的应用容器引擎 ,让开发者可以打包他们的应用及依赖环境到一个可移植的容器中,然后发布到任何运行有Docker引擎的机器上. 让大家可以随心所欲地使用软件而又不会深陷到环境配置中 ...

  9. JavaSrcipt的数字(number):深入理解内部机制

    一.数字的语法 JavaScript中的数字字面量一般用十进制表示.在JavaScript中表示数字的数据类型只有一种Number,这种天使与魔鬼同体的数据类型也就只有js了. //同时表达整数和浮点 ...

  10. vue中axios 配置请求拦截功能 及请求方式如何封装

    main.js 中: import axios from '................/axios' axios.js 中: //axios.js import Vue from 'vue' i ...