LeetCode one Two Sum
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的更多相关文章
- 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 ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [leetCode][013] Two Sum 2
题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [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 ...
随机推荐
- 温故知新 —— Floyd算法
什么?Floyd?sb O(n ^ 3) 算法早不用了,右上角红叉吧.我之前虽然也认识过 Floyd 算法的重要性,不过多少也是这么想的.然而最近三天连续 rand 到了好几道有关的题目,让我彻底重新 ...
- 字节缓冲流 BufferedOutputStream BufferedInputStream
/*缓冲流:读取数据大量的文件时,读取的速度慢java提供了一套缓冲流 提高io流的效率 * */ package cn.lijun.demo; import java.io.BufferedInpu ...
- shell数组等基本用法
"shell"既是一种解释型编程语言,也是一个这种编程语言的解释器的名字 shell是解释型语言,就是解释器会一条一条的翻译每一条语句并执行,对比之下,C语言是编译型语言,编译器把 ...
- Kubernetes的负载均衡问题(Nginx Ingress)
nginx 反向代理 https://www.cnblogs.com/ericnie/p/6965091.html Kubernetes 集群中使用 Traefik https://blog.csdn ...
- codeforces794D dfs+图上hash
http://codeforces.com/problemset/problem/794/D 题意:在一个国家有 n 座城市和一些双向边.这些城市被编号为 1 到 n. 一共有 m 条双线边,第 i条 ...
- CentOS 6.5 64位 安装Nginx, MySQL, PHP
此篇文章参考了一些网站找的教程,自己遇到了很多坑,写一下自己的安装全过程. 服务器是腾讯云的.安装了centos 6.5系统. 一. 安装Nginx 1.首先安装GCC,make,C++编译器 yum ...
- B+树及数据库索引的应用
B树 每个节点都存储key和data,所有节点组成这棵树,并且叶子节点指针为null. B+树 只有叶子节点存储data,叶子节点包含了这棵树的所有键值,叶子节点不存储指针. 后来,在B+树上增加了顺 ...
- Kafka技术内幕 读书笔记之(三) 消费者:高级API和低级API——消费者消费消息和提交分区偏移量
消费者拉取钱程拉取每个分区的数据,会将分区的消息集包装成一个数据块( FetchedDataChunk )放入分区信息的队列中 . 而每个队列都对应一个消息流( KafkaStream ),消费者客户 ...
- sqlyog创建数据库表关系图
作为一个后台前端,数据库,需求分析,运维,PPT全包的码农来说.uml建模不存在的,对不起我没有时间,就用sqlyog拉几个你看看吧.看的懂的一眼就看清了,看不懂的整再好也是白瞎. 第一步:选择增强工 ...
- NodeJs 学习笔记(一)Wedding 项目搭建
说明:Ubuntu16.04 自带的NodeJs版本太低,安装包更新不了,只能编译安装了 一.NodeJs编译安装 下载:https://nodejs.org/en/download/ 修改目录权限: ...