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. 浏览器报XMLHttpRequest cannot loadxxxxxx

    解决方案 找到浏览器-----右击---属性---加 加上这一句就不会报错--allow-file-access-from-files 加上这一句就不会报错--allow-file-access-fr ...

  2. JS数据交换的三种方式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. pstack跟踪进程栈

    一:简介 这个命令可以显示每个进程的栈跟踪.pstack命令必须由相应进程的宿主或root运行.可以使用pstack来确定进程挂起的位置.此命令允许使用唯一选项就是进程的PID 这个命令在排查进程问题 ...

  4. xlrd(excel导入mysql数据库)

    一.xlrd模块: python 对 excel 文件进行读操作 1.下载xlrd 并安装 http://pypi.python.org/pypi/xlrd wget https://pypi.pyt ...

  5. consul介绍

    consul 是一个支持多数据中心分布式高可用,用于服务发现和配置共享的工具. consul与其它工具的不同,官方介绍如下: https://www.consul.io/intro/vs/index. ...

  6. Idea中的一些快捷键

    最常用快捷键 sout 输出语句 psvm 输出main方法 fori 输出for语句 iter 输出foreach语句 Ctrl+Shift+U 大小写转换 Ctrl+Shift+Enter 补全一 ...

  7. setAttribute和setParameter方法的区别

    getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型 getParame ...

  8. 解决CDN传统方法引入Iview icon 不显示问题

    因为需要字体文件,可以在github上下载. 将文件下载之后放到fonts文件夹下,fonts文件夹要与Iview.css在同级目录

  9. mq【转】

    kafka解决了什么问题? mq的几个指标 1.消息堆积能力.两亿条1k大小消息体的消息发上来,积压一周不消费,机器哭不哭. 2.吞吐量.来个峰值,每秒两万,连续两小时,临时扩容扛不扛得住. 3.安全 ...

  10. idea 快捷键ctrl+shift+f失效的解决方案

    我是使用微软自带的拼音输入法,右下角图标--右键--设置--按键,拉到最下面 ---快捷键(简体繁体切换),取消勾选 即可