当前位置:网站首页>数组学习之入门简单题 两数之和
数组学习之入门简单题 两数之和
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 {
};
}
};
边栏推荐
- 01-01-osg GL3 环境搭建
- 研发效能|Kubernetes核心技术剖析和DevOps落地经验
- APP常用跨端技术栈深入分析
- 最新坦克大战2022-全程开发笔记-3
- Arfoundation starts from scratch 3- create an arfoundation project
- CryEngine3 调试Shader方法
- 365天挑战LeetCode1000题——Day 042 数组序号转换 + 相对名次 离散化处理
- 终端shell常用命令
- 365天挑战LeetCode1000题——Day 039 完全二叉树插入器 + 寻找峰值 II + 快照数组
- AiTalk创始人梁宇淇:镜像连接虚拟与现实的纽带
猜你喜欢

AiTalk创始人梁宇淇:镜像连接虚拟与现实的纽带

ARFoundation从零开始3-创建ARFoundation项目

How rimworld uploads creative workshops through steamcmd

Database course design of online assistant teaching platform for high school chemistry

More than 200 ISVs have settled in! The first anniversary of Alibaba cloud computing nest

OCCT学习003-----MFC单文档工程

Arfoundation starts from zero 9-ar anchor

手写学生管理系统

Li Yan, CEO of parallel cloud: cloudxr, opens the channel to the metauniverse

In depth analysis of common cross end technology stacks of app
随机推荐
Come on! See how Clickhouse, which has risen 16 places a year, can be implemented in jd.com
200 多家 ISV 入驻!阿里云计算巢发布一周年
EXIT中断详解
CMU15-213 Malloc Lab实验记录
自定义Qml控件:ImageButton
Do you remember the process analysis and function implementation of post notification?
Is Huatai Securities an AA level securities company? How about this company? Is it safe to open an account?
Alibaba cloud architect Liang Xu: MES on cloud box helps customers quickly build digital factories
The road to success in R & D efficiency of 1000 person Internet companies
Thousands of databases, physical machines all over the country, JD logistics full volume cloud live record | interview with excellent technical team
Numpy Foundation
研发效能生态完整图谱&DevOps工具选型必看
510000 prize pool invites you to fight! The second Alibaba cloud ECS cloudbuild developer competition is coming
阿里云张新涛:异构计算为数字经济提供澎湃动力
321,京东言犀×NLPCC 2022挑战赛开赛!
Modification of annotation based three-tier project and the way of adding package scanning
Handwritten student management system
如视技术副总裁杨永林:当传统产业遇到“数字空间”
365天挑战LeetCode1000题——Day 036 二叉树剪枝 + 子数组和排序后的区间和 + 删除最短的子数组使剩余数组有序
In depth analysis of common cross end technology stacks of app