当前位置:网站首页>【刷题记录】1. 两数之和
【刷题记录】1. 两数之和
2022-07-05 20:21:00 【InfoQ】
前言
一、题目描述
输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
输入: nums = [3,2,4], target = 6
输出: [1,2]
1 <= s.length <= 1000
s 由英文字母(小写和大写)、',' 和 '.' 组成
1 <= numRows <= 1000
二、思路分析:
1.简单直接暴力解法 - 遍历
class Solution {
public int[] twoSum(int[] nums, int target) {
int length = nums.length;
//保证num[j]存在 i 遍历到 n-2 为止
for (int i = 0; i < length - 1; i++) {
//避免重复,遍历从当前数组后面开始
for (int j = i + 1; j < length; j++) {
if (nums[i] + nums[j] == target) return new int[]{i,j};
}
}
return new int[]{};
}
}

2.改进下解法-哈希表
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(target - nums[i])) return new int[]{map.get(target - nums[i]), i};
map.put(nums[i], i);
}
return new int[]{};
}
}

总结
边栏推荐
- 【数字IC验证快速入门】9、Verilog RTL设计必会的有限状态机(FSM)
- 强化学习-学习笔记4 | Actor-Critic
- 如何形成规范的接口文档
- 618 "low key" curtain call, how can baiqiushangmei join hands with the brand to cross the "uncertain era"?
- 处理文件和目录名
- Go language learning tutorial (16)
- [quick start of Digital IC Verification] 9. Finite state machine (FSM) necessary for Verilog RTL design
- Station B up builds the world's first pure red stone neural network, pornographic detection based on deep learning action recognition, Chen Tianqi's course progress of machine science compilation MLC,
- 零道云新UI设计中
- sun. misc. Base64encoder error reporting solution [easy to understand]
猜你喜欢

14. Users, groups, and permissions (14)
![[quick start to digital IC Verification] 8. Typical circuits in digital ICs and their corresponding Verilog description methods](/img/3a/7eaff0bf819c129b4f866388e57b87.png)
[quick start to digital IC Verification] 8. Typical circuits in digital ICs and their corresponding Verilog description methods

【数字IC验证快速入门】1、浅谈数字IC验证,了解专栏内容,明确学习目标
![[quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives](/img/90/88a1f79a07016738d2688548e21949.png)
[quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives

Introduction to dead letter queue (two consumers, one producer)

Leetcode brush questions: binary tree 11 (balanced binary tree)

【数字IC验证快速入门】2、通过一个SoC项目实例,了解SoC的架构,初探数字系统设计流程

.Net分布式事务及落地解决方案

leetcode刷题:二叉树15(找树左下角的值)

14、Transformer--VIT TNT BETR
随机推荐
B站UP搭建世界首个纯红石神经网络、基于深度学习动作识别的色情检测、陈天奇《机器学编译MLC》课程进展、AI前沿论文 | ShowMeAI资讯日报 #07.05
物联网智能家居基本方法实现之经典
Practical demonstration: how can the production research team efficiently build the requirements workflow?
【数字IC验证快速入门】8、数字IC中的典型电路及其对应的Verilog描述方法
Minimum commission for stock trading account opening, where to open an account with low commission? Is it safe to open an account on your mobile phone
MySql的root密码忘记该怎么找回
解决Thinkphp框架应用目录下数据库配置信息修改后依然按默认方式连接
点云文件的.dat文件读取保存
1: Citation;
1:引文;
mongodb基操的练习
mongodb文档间关系
Rainbond 5.7.1 支持对接多家公有云和集群异常报警
mongodb/文档操作
Go language | 03 array, pointer, slice usage
CVPR 2022 | 常见3D损坏和数据增强
C language OJ gets PE, OJ of ACM introduction~
Scala basics [HelloWorld code parsing, variables and identifiers]
实操演示:产研团队如何高效构建需求工作流?
计算lnx的一种方式