1. 两数之和

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

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9

所以返回 [0, 1]

15.三数之和

给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。

注意:答案中不可以包含重复的三元组。

示例:

给定数组 nums = [-1, 0, 1, 2, -1, -4],

满足要求的三元组集合为:

[

[-1, 0, 1],

[-1, -1, 2]

]

solution1 暴力破解

//两次循环解两数之和
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] re = new int[2];
for (int i = 0; i < nums.length - 1; i ++) {
for (int j = i + 1; j < nums.length; j ++){
if (nums[i] + nums[j] == target) {
re[0] = i;
re[1] = j;
break;
}
}
}
return re;
}
}
//三次循环解三数之和,超出时间限制
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
if (nums == null || nums.length <= 2) {
return Collections.emptyList();
}
// Set集合,去除重复的数组
Set<List<Integer>> re = new LinkedHashSet<>();
for (int t = 0; t < nums.length - 2; t ++) {
for (int i = t + 1; i < nums.length - 1; i ++){
for (int j = i + 1; j < nums.length; j ++) {
if (nums[t] + nums[i] + nums[j] == 0) {
List<Integer> value = Arrays.asList(nums[t],nums[i],nums[j]);
Collections.sort(value);
re.add(value);
}
}
}
}
return new ArrayList<>(re);
}
}

solution2 两指针

//时间复杂度n^2
//先排序然后遍历数组,整两指针向中夹逼
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
if (nums == null || nums.length <= 2) {
return Collections.emptyList();
}
Arrays.sort(nums);
List<List<Integer>> res = new LinkedList<>();
for (int k = 0; k < nums.length - 2; k ++) {
if (nums[k] > 0) return res;
if (k > 0 && nums[k] == nums[k-1]) continue;
if(k == 0 || k >= 0 && nums[k] != nums[k - 1]) {
for (int i = k + 1,j = nums.length - 1; i < j;){
int sum = nums[i] + nums[j];
int min = 0 - nums[k];
if (sum == min) {
res.add(Arrays.asList(nums[i],nums[j],nums[k]));
while(i < j && nums[i] == nums[i+1]) i ++;
while(i < j && nums[j] == nums[j-1]) j --;
i ++;
j --;
}
else if (sum < min) {
i ++;
}else j --;
}
}
}
return res;
}
}

哈希表法

//时间复杂度为O(2n)
class Solution {
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 diff = target - nums[i];
if (map.containsKey(diff) && map.get(diff) != i){
return new int[]{i,map.get(diff)};
}
}
return null;
}
}
//一次哈希,时间复杂度为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 ++) {
int diff = target - nums[i];
if (map.containsKey(diff)) {//遍历之前的map里是否包含差
return new int[]{i,map.get(diff)};
}
map.put(nums[i],i);
}
return null;
}
}
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
if (nums == null || nums.length < 3) return Collections.emptyList();
List<List<Integer>> res = new ArrayList();
Set<Integer> set = new HashSet();
Arrays.sort(nums);
for (int i = 0;i<nums.length;i++){
for (int j = i+1;j < nums.length;j ++) {
int diff = 0-nums[i]-nums[j];
if (set.contains(diff)) {
List<Integer> newAdded = Arrays.asList(diff, nums[j], nums[i]);
if (!res.contains(newAdded)) {
res.add(newAdded);
}
} }
set.add(nums[i]);
}
return res;
}
}
//两遍遍历加hash
//list 可以用contains()查看是否重复

LeetCode1两数之和、15三数之和的更多相关文章

  1. LeetCode 15. 三数之和(3Sum)

    15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...

  2. LeetCode:两数之和、三数之和、四数之和

    LeetCode:两数之和.三数之和.四数之和 多数之和问题,利用哈希集合减少时间复杂度以及多指针收缩窗口的巧妙解法 No.1 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在 ...

  3. 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和

    第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...

  4. 代码随想录算法训练营day07 | leetcode 454.四数相加II 383. 赎金信 15. 三数之和 18. 四数之和

    LeetCode 454.四数相加II 分析1.0 这个最直接暴力法,不过过于暴力了,害怕.jpg 失误 读题理解失误:题目要求的是四元组的个数,读完题到我这里成了输出四元组,悲哉 分析2.0 记录有 ...

  5. Java实现 LeetCode 15 三数之和

    15. 三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以 ...

  6. 【Leetcode】两数之和,三数之和,四数之和

    两数之和 题目 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这 ...

  7. LeetCode 15. 三数之和(3Sum)

    题目描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复 ...

  8. LeetCode——15. 三数之和

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

  9. 【LeetCode】15.三数之和

    题目描述 1. 三数之和 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组. 注意: ...

  10. leetcode题目15.三数之和(中等)

    题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重 ...

随机推荐

  1. CCF CSP认证注册、报名、查询成绩、做模拟题等答疑

    CCF CSP认证注册.报名.查询成绩.做模拟题等答疑 CCF CSP认证中心将考生在注册,或报名,或查询成绩,或历次真题练习时遇到的问题进行汇总,并给出解决方法,具体如下: 1.注册时,姓名可否随意 ...

  2. Installing Gradle

    Chapter 4. Installing Gradle 4.1. Prerequisites Gradle requires a Java JDK or JRE to be installed, v ...

  3. idea2020.3 安装插件JetBrains 插件市场安装 Cloud Toolkit

    <Cloud Toolkit User Guide> 本文是 Alibaba Cloud Toolkit 的使用文档指引,所有相关的使用参考,都可以在本文中找到.如果在使用中有任何问题,请 ...

  4. np.random.uniform()

    np.random.uniform(start,end,second) start:开始数 end:结束数 second:次数,也就是选择几次. 代码结果如下所示: import numpy as n ...

  5. HCTF 2023 wp

    HCTF 2023 wp 一.Misc 1.玩原神玩的 分析:附件为一张图片 观察最后一行,明显有flag的格式 搜索得知是 对照得flag为:hctf{yuanlainiyewanyuanshenh ...

  6. docker容器管理脚本

    #!/bin/bash #auto install docker and Create VM #by jfedu.net 2017 #Define PATH Varablies IPADDR=`ifc ...

  7. Go语言基准测试(benchmark)三部曲之一:基础篇

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 关于基准测试(benchmark) Go的标准库内置的 ...

  8. 如何赋予 GPT/LLM 自我意识1

    引子 这个周末OpenAI搞了一个大新闻,围绕 Sam Altman 和 Ilya Sutskever 的各种讨论遍地开花,而其中一个关注点就是他们对于 AGI 降临态度上的偏差.本文不打算讨论公司治 ...

  9. 发现AI自我意识:知识及其载体

    知识的量子态 在回答什么是"理解"之前,我们先来讨论一下知识和其载体的定义.知识本身是一个抽象的概念,它可以被编码到各种物质载体中.无论是纸质书籍,还是人类大脑中的神经连接,抑或是 ...

  10. RIPEMD加密技术

    摘要:RIPEMD(RACE Integrity Primitives Evaluation Message Digest)是一种密码散列函数,广泛应用于网络安全领域.本文首先介绍RIPEMD的起源和 ...