当前位置:网站首页>[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
边栏推荐
- Some problems encountered in cocos2d-x project summary
- [C language] three implementations of quick sorting and optimization details
- nprogress插件 进度条
- 14、Transformer--VIT TNT BETR
- [quick start of Digital IC Verification] 3. Introduction to the whole process of Digital IC Design
- leetcode刷题:二叉树14(左叶子之和)
- Is it safe for CICC fortune to open an account online?
- 小程序项目结构
- 【愚公系列】2022年7月 Go教学课程 004-Go代码注释
- 2022北京眼睛健康用品展,护眼产品展,中国眼博会11月举办
猜你喜欢
1、强化学习基础知识点
. Net distributed transaction and landing solution
How to select the Block Editor? Impression notes verse, notation, flowus
Rainbond 5.7.1 支持对接多家公有云和集群异常报警
物联网智能家居基本方法实现之经典
IC科普文:ECO的那些事儿
Practical demonstration: how can the production research team efficiently build the requirements workflow?
[quick start of Digital IC Verification] 7. Basic knowledge of digital circuits necessary for verification positions (including common interview questions)
小程序页面导航
Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms
随机推荐
Document method
[quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives
Hong Kong stocks will welcome the "best ten yuan store". Can famous creative products break through through the IPO?
E. Singhal and Numbers(质因数分解)
ICTCLAS用的字Lucene4.9捆绑
插值查找的简单理解
Go language learning tutorial (XV)
Classic implementation method of Hongmeng system controlling LED
Relationship between mongodb documents
【c语言】快速排序的三种实现以及优化细节
.Net分布式事务及落地解决方案
July 4, 2022 - July 10, 2022 (UE4 video tutorial MySQL)
【刷题记录】1. 两数之和
sun.misc.BASE64Encoder报错解决方法[通俗易懂]
Leetcode brush question: binary tree 14 (sum of left leaves)
Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)
Leetcode (347) - top k high frequency elements
【数字IC验证快速入门】2、通过一个SoC项目实例,了解SoC的架构,初探数字系统设计流程
基金网上开户安全吗?去哪里开,可以拿到低佣金?
Leetcode brush questions: binary tree 11 (balanced binary tree)