当前位置:网站首页>【每日一道LeetCode】——1. 两数之和
【每日一道LeetCode】——1. 两数之和
2022-08-02 02:36:00 【月亮嚼成星~】

目录
原题:两数之和
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。
示例 1:
输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
示例 2:
输入:nums = [3,2,4], target = 6
输出:[1,2]示例 3:
输入:nums = [3,3], target = 6
输出:[0,1]解题思路:
首先从数组的第一个元素开始,由于要比较两个数的和 目标数字是否相等,所以我们要利用循环,每次比较要比较数的后面的每一个数,看其和是否等于目标数字,如果相等就返回其下标,如果没有则不返回下标。
代码实现:
class Solution {
public int[] twoSum(int[] nums, int target) {
int length=nums.length;
for(int i=0;i<length;i++){
for(int j=i+1;j<length;j++){
if(nums[i]+nums[j]==target){
return new int[]{i,j};
}
}
}
return new int[0];
}
}执行结果:

总结:
每天都要刷,不可以懈怠!!!
边栏推荐
- 20. 用两个栈实现队列
- 永磁同步电机36问(三)——SVPWM代码实现
- TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
- [Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games
- 指针数组和数组指针
- 2022-08-01 mysql/stoonedb slow SQL-Q18 analysis
- Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
- 2022-08-01 反思
- 记一次gorm事务及调试解决mysql死锁
- 机器人领域期刊会议汇总
猜你喜欢

Talking about the "horizontal, vertical and vertical" development trend of domestic ERP

2022-07-30 mysql8执行慢SQL-Q17分析

Nanoprobes纳米探针丨Nanogold偶联物的特点和应用

灰度传感器、、、diy原理。。图

qt点云配准软件

很有意思的经历,很有意思的项目--文件夹对比工具

BI-SQL丨WHILE

忽晴忽雨

国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现

The principle and code implementation of intelligent follower robot in the actual combat of innovative projects
随机推荐
Docker-compose安装mysql
数仓:为什么说 ETL 的未来不是 ELT,而是 EL (T)
Swift运行时(派发机制)
Can Youxuan database import wrongly be restored?
最大层内元素和
Analysis of the status quo of digital transformation of manufacturing enterprises
TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
记一个gorm初始化的坑
AI target segmentation capability for fast video cutout without green screen
接口测试神器Apifox究竟有多香?
analog IC layout-Environmental noise
【web】理解 Cookie 和 Session 机制
pyqt上手体验
How to adjust the cross cursor too small, CAD dream drawing calculation skills
openGauss切换后state状态显示不对
Nanoprobes免疫测定丨FluoroNanogold试剂免疫染色方案
JS中获取对象数据类型的键值对的键与值
MySQL - CRUD operations
淘宝详情.
2022河南青训联赛第(三)场