当前位置:网站首页>[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
边栏推荐
- 中金财富在网上开户安全吗?
- 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
- nprogress插件 进度条
- 物联网智能家居基本方法实现之经典
- 什么是pyc文件
- [C language] merge sort
- 【数字IC验证快速入门】2、通过一个SoC项目实例,了解SoC的架构,初探数字系统设计流程
- Introduction to dead letter queue (two consumers, one producer)
- Sort and projection
- 1. Strengthen learning basic knowledge points
猜你喜欢

Zero cloud new UI design

Classic implementation method of Hongmeng system controlling LED

Oracle tablespace management

走入并行的世界

【数字IC验证快速入门】8、数字IC中的典型电路及其对应的Verilog描述方法

物联网智能家居基本方法实现之经典

零道云新UI设计中

Leetcode brush questions: binary tree 18 (largest binary tree)
![[Yugong series] go teaching course in July 2022 004 go code Notes](/img/18/ffbab0a251dc2b78eb09ce281c2703.png)
[Yugong series] go teaching course in July 2022 004 go code Notes

Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms
随机推荐
Informatics Olympiad 1337: [example 3-2] word search tree | Luogu p5755 [noi2000] word search tree
[quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives
小程序事件绑定
Leetcode brush question: binary tree 13 (the same tree)
小程序代码的构成
Oracle tablespace management
y57.第三章 Kubernetes从入门到精通 -- 业务镜像版本升级及回滚(三十)
2022北京眼睛健康用品展,护眼产品展,中国眼博会11月举办
[quick start of Digital IC Verification] 3. Introduction to the whole process of Digital IC Design
【刷题记录】1. 两数之和
【c语言】归并排序
When JS method passes long type ID value, precision loss will occur
2.8、项目管理过程基础知识
model方法
19 Mongoose模块化
JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)
ICTCLAS用的字Lucene4.9捆绑
Zero cloud new UI design
Process file and directory names
Unity编辑器扩展 UI控件篇