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. Arduino Uno 引脚 –

    Arduino Uno 引脚 – Arduino Uno 板有 20 多个引脚,可用于许多不同的应用.在这篇文章中,我将为您提供 Arduino Uno 主要引脚的完整实用概述. 如果您刚开始使用 A ...

  2. Arduino 麦克风声音传感器指南

    麦克风声音传感器 麦克风声音传感器,顾名思义,检测声音.它可以测量声音的响度. 这些传感器的种类繁多.  在下图中,您可以看到 Arduino 最常用的. 最左边是KY-038,右边是LM393麦克风 ...

  3. CentOS7 Ceph分布式集群部署

    CentOS 7 下安装Ceph-nautilus 本问主要记录在CentOS 7下如何安装Ceph-nautilus,安装过程中遇到的一些问题及解决方法. 1.Ceph实验准备 以下是本次实验所用到 ...

  4. c#中原型模式详解

    基础介绍:   具体可分为2个角色:     Prototype(原型类):声明一个Clone自身的接口:     ConcretePrototype(具体原型类):,实现一个Clone自身的操作. ...

  5. Odoo—货运管理—odoo时差问题

    第一次踩odoo时差的坑,才知道原来odoo在存储日期数据时,是以UTC0时区存放的,和北京时间相差8个小时.只是odoo本身能很好的处理日期数据的存储和展示,所以刚开始接触odoo,不容易发现这个问 ...

  6. 一文读懂强化学习:RL全面解析与Pytorch实战

    在本篇文章中,我们全面而深入地探讨了强化学习(Reinforcement Learning)的基础概念.主流算法和实战步骤.从马尔可夫决策过程(MDP)到高级算法如PPO,文章旨在为读者提供一套全面的 ...

  7. Java开发者的Python快速进修指南:异常捕获

    在之前的学习中,我们已经讲解了函数和控制流等基本概念.然而,在接触实际业务时,你会发现异常捕获也是必不可少的一部分,因为在Java编程中,异常处理是不可或缺的.Python的异常捕获与Java的异常捕 ...

  8. 用友U8+与百胜E3的数据对接:实现企业数字化业务的集成与协作

    用友U8+作为中国企业最佳经营管理平台之一,在企业经营管理中广泛应用.然而,由于每个企业的内部管理方式和流程各不相同,标准软件功能难以完全适应所有企业的管理需求.同时,随着互联网和移动应用的发展,对于 ...

  9. 你真的了解HashSet 和HashMap的区别、优缺点、使用场景吗?

    HashSet 和 HashMap 是 Java 集合框架中的两个常用类,它们都用于存储和管理数据,但在使用方式.功能和性能上有很大的区别. HashSet 和 HashMap 的区别 区别一:用途不 ...

  10. 简易的git命令行入门教程

    一.Git 全局设置 git config --global user.name "用户名" git config --global user.email "邮件地址@1 ...