当前位置:网站首页>[record of question brushing] 1 Sum of two numbers
[record of question brushing] 1 Sum of two numbers
2022-07-05 20:25:00 【InfoQ】
Preface
One 、 Title Description
Input :nums = [2,7,11,15], target = 9
Output :[0,1]
explain : because nums[0] + nums[1] == 9 , return [0, 1] .
Input : nums = [3,2,4], target = 6
Output : [1,2]
1 <= s.length <= 1000
s By the English letters ( Lowercase and upper case )、',' and '.' form
1 <= numRows <= 1000
Two 、 Thought analysis :
1. Simple direct violent solution - Traverse
class Solution {
public int[] twoSum(int[] nums, int target) {
int length = nums.length;
// Guarantee num[j] There is i Traversing n-2 until
for (int i = 0; i < length - 1; i++) {
// Avoid repetition , The traversal starts after the current array
for (int j = i + 1; j < length; j++) {
if (nums[i] + nums[j] == target) return new int[]{i,j};
}
}
return new int[]{};
}
}

2. Improve the solution - Hashtable
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[]{};
}
}

summary
边栏推荐
- 基金网上开户安全吗?去哪里开,可以拿到低佣金?
- 14、Transformer--VIT TNT BETR
- 什么是pyc文件
- 【数字IC验证快速入门】1、浅谈数字IC验证,了解专栏内容,明确学习目标
- B站UP搭建世界首个纯红石神经网络、基于深度学习动作识别的色情检测、陈天奇《机器学编译MLC》课程进展、AI前沿论文 | ShowMeAI资讯日报 #07.05
- 小程序页面导航
- .Net分布式事務及落地解决方案
- Model method
- Informatics Olympiad 1338: [example 3-3] hospital setting | Luogu p1364 hospital setting
- leetcode刷题:二叉树10(完全二叉树的节点个数)
猜你喜欢

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

欢迎来战,赢取丰厚奖金:Code Golf 代码高尔夫挑战赛正式启动

14、Transformer--VIT TNT BETR

港股将迎“最牛十元店“,名创优品能借IPO突围?

Classic implementation method of Hongmeng system controlling LED

Mysql频繁操作出现锁表问题

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

Fundamentals - configuration file analysis

1. Strengthen learning basic knowledge points

kubernetes资源对象介绍及常用命令(五)-(ConfigMap&Secret)
随机推荐
资源道具化
mongodb基操的练习
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
CTF逆向基础
基础篇——配置文件解析
零道云新UI设计中
.Net分布式事務及落地解决方案
c語言oj得pe,ACM入門之OJ~
Solve the problem that the database configuration information under the ThinkPHP framework application directory is still connected by default after modification
Y57. Chapter III kubernetes from entry to proficiency -- business image version upgrade and rollback (30)
About the priority of Bram IP reset
B站UP搭建世界首个纯红石神经网络、基于深度学习动作识别的色情检测、陈天奇《机器学编译MLC》课程进展、AI前沿论文 | ShowMeAI资讯日报 #07.05
MySql的root密码忘记该怎么找回
sort和投影
Unity编辑器扩展 UI控件篇
Introduction to dead letter queue (two consumers, one producer)
ICTCLAS用的字Lucene4.9捆绑
sun.misc.BASE64Encoder报错解决方法[通俗易懂]
计算lnx的一种方式
Cocos2d-x项目总结中的一些遇到的问题