当前位置:网站首页>数组学习之入门简单题 两数之和
数组学习之入门简单题 两数之和
2022-07-29 05:09:00 【卷饼85】
题目
给定一个整数数组 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]
方法一:暴力
先确定一个值,再依次遍历数组后边的值,将两数相加,若等于target
则返回下标 i 和 j
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (nums[i] + nums[j] == target) {
return {
i, j};
}
}
}
return {
};
}
};
边栏推荐
- 365天挑战LeetCode1000题——Day 040 设计跳表 + 避免洪水泛滥 + 查找大小为 M 的最新分组 + 销售价值减少的颜色球
- Visual Basic .Net 如何获取命令参数
- 预约中,2022京东云产业融合新品发布会线上开启
- 容器安全开源检测工具--问脉 VeinMind(镜像后门、恶意样本、敏感信息、弱口令等)
- Live broadcast Preview: integration of JD cloud Devops and jfrog product library
- Complete ecological map of R & D Efficiency & selection of Devops tools
- Vs code的安装步骤及环境配置
- 法线可视化
- Getting started with arfoundation tutorial 10- plane detection and placement
- Xiaobai high salary shortcut Qt development game Snake
猜你喜欢

365天挑战LeetCode1000题——Day 042 数组序号转换 + 相对名次 离散化处理

研发效能生态完整图谱&DevOps工具选型必看

QT series - Installation

Helm chart for Kubernetes

Complete ecological map of R & D Efficiency & selection of Devops tools

最新坦克大战2022-全程开发笔记-1

510000 prize pool invites you to fight! The second Alibaba cloud ECS cloudbuild developer competition is coming

Unity3D - 物体太远看不见的问题

7.3-function-templates

AI应用第一课:C语言支付宝刷脸登录
随机推荐
Come on! See how Clickhouse, which has risen 16 places a year, can be implemented in jd.com
CryEngine3 调试Shader方法
副作用和序列点
In depth analysis of common cross end technology stacks of app
321, Jingdong Yanxi × Nlpcc 2022 challenge starts!
Scikit learn -- steps and understanding of machine learning application development
Alibaba cloud architect Liang Xu: MES on cloud box helps customers quickly build digital factories
GPIO的输入输出详解
The latest tank battle 2022 full development notes-1
Getting started with arfoundation tutorial 10- plane detection and placement
NVIDIA Zhou Xijian: the last mile from design to digital marketing
EXIT中断详解
More than 200 ISVs have settled in! The first anniversary of Alibaba cloud computing nest
C语言求字符串的长度
Complete ecological map of R & D Efficiency & selection of Devops tools
Qml类型:MouseArea
APP常用跨端技术栈深入分析
QT learning: qdropevent drag event
What is_ GLIBCXX_ VISIBILITY(default)
365天挑战LeetCode1000题——Day 036 二叉树剪枝 + 子数组和排序后的区间和 + 删除最短的子数组使剩余数组有序