LeetCode 1: Two Sum

Leetcode: 1.Two Sum 两数之和 Two Sum 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 输入: nums = [2, 7, 11, 15], target = 9 输出: [0, 1] 因为 nums[0] + nums[1] = 2 + 7 = 9 ... 阅读详情

题目:

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
题目分析:

给定一个整形的数组,返回数组中两个元素的下标,这两个数的和为给定的值。假设对于每个确定的输入值都只有一种解。

思路:

这个题在LeetCode OJ上给出了三种解决方案:Brute force(简单匹配算法)、Two-pass hash table、 One-pass hash table。后两种笔者不知道怎么翻译了……有时候翻译成中文倒是没有英文那么直观。

对于第一种Brute force,网上有说成是暴力算法的,主要就是遍历,时间复杂度高是O(n^2),空间复杂度是O(1)直接上代码:

public class Solution {
    public int[] twoSum(int[] nums, int target) {
        int result[] = new int[2];
        int i;
        int j;
        for(i=0;i<nums.length-1;i++) {
            for(j=i+1;j<nums.length;j++)
                if (nums[i] + nums[j] == target) {
                    result[0] = i;
                    result[1] = j;
                    return result;
                }
        }
        return null;
    }
}

Two-pass hash table:最主要的思路来源一个问题“对于一个数组来说,维护其字面值和索引的最好的方式是什么?是哈希表。”首先将所有元素存储到哈希表中,遍历nums中的元素,每遍历一个元素时使用target-遍历元素,得到的结果再去哈希表中找,寻找的时间复杂度接近O(1),这样,我们只需要遍历一遍就能找到结果。整个算法的时间复杂度是O(n),空间复杂度是O(n)。算法的核心就是时空转换嘛,上代码:

public 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-1;i++) {
            int complement = target - nums[i];
            if(map.containsKey(complement)&&map.get(complement)!=i) {
                return new int[] {i,map.get(complement)};
            }
        }
        return null;
    }
}

One-pass hash table:我个人觉得这个算法棒极了,和第二种算法的区别在于思路!思路!思路!重要的事情说三遍!在建立哈希表的同时就可以一边看当前哈希表中是否有符合条件的值,如果有,哈希表还没建完就找到了答案,岂不妙哉?虽然时间复杂度和空间复杂度同第二种算法~,话不多说上代码:

public class Solution {
    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)&&map.get(complement)!=i) {
                return new int[] {i,map.get(complement)};
            }
            map.put(nums[i],i);
        }
        return null;
    }
}


未连接互联网的银河麒麟v10安装g++,各种报错,如何处理? 🏆本文收录于《全栈Bug调优(实战版)》专栏,主要记录项目实战过程中所遇到的Bug或因后果及提供真实有效的解决方案,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!! 备注:部分问题/疑难杂症搜集于互联网。 阅读详情

相关推荐

从5V降到3.3V,你的LDO到底有多烫?手把手算功耗、选散热,避开这些坑

本文详细解析了LDO在5V转3.3V电路中的发热问题,提供了精确的功耗计算公式和散热设计方法。通过对比不同封装的热阻参数和优化PCB布局,帮助工程师有效降低LDO温度,避免热保护触发。文章还探讨了替代方案的技术拐点,为电源系统优化设计提供实用指导。

weixin_30304375的博客 434

1. Two Sum

QuestionGiven an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution.Example: Given nums = [2

Mook的博客 1027

告别编译报错!STC32G12K128在Keil C251下的完整Target配置指南(附ROM设置详解)

本文详细介绍了STC32G12K128在Keil C251下的完整Target配置指南,包括ROM设置、CPU模式选择、内存模型优化等关键步骤。通过实战技巧和常见错误解决方案,帮助开发者高效搭建编译环境,避免常见报错,充分发挥STC32G系列芯片的性能优势。

weixin_29186249的博客 339

两数之和的一题多解

package practice; import java.util.HashMap; import java.util.Map; public class Ecercise1两数之和 { public static void main(String[] args) { } } class Solution { public int[] twoSum(int[] nums, in...

weixin_46004363的博客 444

AMD MI355X与NVIDIA B200在GLM5.2大模型推理性能与成本对比分析

在 AI 推理硬件选型中,成本与性能的平衡一直是技术决策的核心难点。AMD 最新发布的 CDNA 4 架构 MI355X 加速卡与 NVIDIA Blackwell 架构的 B200 在 GLM5.2 这类千亿参数大模型上的表现,直接关系到企业部署私有化模型或提供推理服务的经济性。实际测试数据显示,在 32 tok/s/user 的交互性设定下,单张 MI355X 能够达到 1369 tok/s 的吞吐量,虽然略低于 B200 的 1756 tok/s,但每百万 token 成本仅为 0.30 美元,比 B

weixin_28221501的博客 294

实战指南:从零开始用YOLOv8训练VisDrone无人机检测模型

本文提供了一份详细的实战指南,手把手教你从零开始使用YOLOv8训练一个无人机检测模型。内容涵盖VisDrone数据集下载与格式转换、YAML配置文件编写、模型训练与评估,以及最终的推理部署,旨在帮助开发者快速掌握无人机视角下的目标检测技术。

salt的博客 682

哈希表(Hash Table)原理及其实现

原理 介绍 哈希函数构造 冲突处理 举例 拉链法 hash索引跟B树索引的区别 实现原理介绍哈希表(Hash table,也叫散列表), 是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。哈希表hash table(key,value) 的做法其实很简单,就是把K

I AM BACK 5万+

maxwell与steady state thermal 通过workbench耦合仿真

文章目录maxwellsteady state thermal maxwell 1加入maxwell并打开 2导入模型 3选择求解器类型:涡流求解器eddy current 4修改模型名字不能带_等特殊符号 5为模型赋材料 6设立3D边界,各边电流进出方向为0其余方向300 7设置激励条件,可设置电流大小,相位,方向 8设置集肤深度,选取中间铜导体的体然后设置,如何设置看帮助文档 9求解设置 迭代步数 收敛精度 交流电频率 10验证条件满足计算 11求解 发现无法求解,删除ai

roymustang的博客 9199

面试题:两数之和

题目内容给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 示例:给定 nums = [2, 7, 11, 15], target = 9因为 nums[0] + nums[1] ...

猫巳的博客 394

海康视觉算法平台VisionMaster 4.3.0 C# 二次开发01 加载方案并获取结果

第一次使用海康视觉算法平台VisionMaster 4.3.0,项目中要使用这个平台进行视觉处理并获取结果。

StevenChen的博客 7871

uniapp [安卓苹果App端] - 实现微信支付功能+拉起微信付款支付后返回本app页面,详解uniApp如何唤起本机微信app并跳转到支付页面全流程及示例源码(uniapp实现微信支付,可打包)

uni-app项目在APP端使用微信支付功能,uniapp前端实现微信支付,uniapp开发安卓App对接微信支付,uniapp苹果ios唤起微信支付,uniapp调用微信支付uniapp开发app-调用微信支付,uniapp开发安卓苹果app如何接入微信支付,前后端应该怎么做,UniApp中实现微信支付功能,支付报错,调起失败没响应,uniapp苹果微信支付唤起实现,app唤起微信支付,拉起微信支付,uniapp中唤醒微信进行支付并返回app,uniApp调起微信支付失败报错,全网最详细的uniapp微信

🏆 前端领域优质创作者 1万+

【Java版】LeetCode 力扣第 1 题:两数之和 (two sum)

题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 题目解析 使用查找表来解决该问题。 设置一个 map 容器 record 用来记录元素的值与索引,然后遍历数组 nums

代码时光 384

基于TKVA与深度学习的量子点微型光谱仪光谱重建改进算法研究

本文提出了一种融合深度学习的改进TKVA算法(TKVA-DL),用于提升量子点微型光谱仪的光谱重建精度。该方法创新性地结合深度先验网络与注意力机制,通过自适应学习光谱隐式先验替代传统固定正则化项,并动态加权探测器响应通道。实验表明,所提方法在仿真和实测数据上均显著优于传统TKVA及主流深度学习算法,RMSE降低35%,PSNR提升4.2 dB,同时保持物理可解释性。完整MATLAB实现验证了方法的有效性与可复现性,为微型光谱仪的高精度重建提供了新思路。 关键词:量子点微型光谱仪;光谱重建;Tikhonov正

AI的内部世界的博客 81

1. 两数之和 [leetcode 1: Two Sum]

1. 两个数的和 [leetcode 1: Two Sum] 原题链接 https://leetcode.com/problems/two-sum 老王的解法链接 https://github.com/simplemain/leetcode/blob/master/1/analysis.md 难度 ★☆☆☆☆ 标签 循环 / 二分搜索 / 排序 / 哈希 题目描述 输入一个整数数组和一个整数, 要...

简单的老王 368

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

LeetCode: Problem 1, Two Sum

LeetCode: 2 sum 题目链接:https://leetcode.com/problems/two-sum/ 作为一名学院小学渣,被dalao推荐来写leetcode,第一题就耗费了我好久时间,想尽量努力长期刷下去,就有了长期写博客的想法,希望自己能在这里坚持下去吧。 ...

swordpj的博客 198

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

【LeetCode】解题1:Two Sum

LeetCode解题 1:Two Sum Problem 1: Two Sum [Easy]Solution (Java)修改过程 Problem 1: Two Sum [Easy] Given an array of integers, return indices of the two numbers such that they add up to a specific target. Yo...

Fayedily的博客 249

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

LeetCode 1:two sum

1 Python刷 1.1 暴力法:时间O(n^2),空间O(1) class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ for i in range(len(nums)): for j in

petSym的博客 398

leetcode: 1 Two Sum

最近开始刷leetcode, 这是第一题,没什么复杂的直接上代码好了题目是:Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one sol...

DinnerHowe的博客 753

JS斩杀LeetCode(1):Two Sum

题目: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use

清风的博客 1655

LeetCode1:Two Sum

1. Two Sum 1.1 description Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution,...

暮日落流年的博客 302

【LeetCode】LeetCode——第1题:Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your r

hujingshuang 5171

LeetCode 1:Two Sum

LeetCode 1:Two SumGiven an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to th

一只码畜 422

Leetcode 1:Two Sum

Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note t

Smith_David的博客 416

Leetcode Exercise 1:Two Sum + Three Sum

应老师要求,题目选自LeetCode中的算法问题,作为《算法分析与设计》这门课程的weekly练习。

qq_33361432的博客 416

leetcode-1: Two sum

题目说明: * Given an array of integers, find two numbers such that they add up to a specific target number. * * The function twoSum should return indices of the two numbers such that they add up to the ta...

qq_38236744的博客 100

LeetCode第1题:Two Sum

Given an array of integers, returnindicesof the two numbers such that they add up to a specific target. You may assume that each input would haveexactlyone solution, and you may not use thesame...

凯哥大数据——刘凯的博客 227
下一篇: Swift学习笔记一:语言特性
bornhovering
博客等级 码龄11年 1粉丝 7原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值