当前位置:网站首页>[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
边栏推荐
- Y57. Chapter III kubernetes from entry to proficiency -- business image version upgrade and rollback (30)
- Is it safe for Galaxy Securities to open an account online?
- 零道云新UI设计中
- 14、Transformer--VIT TNT BETR
- Classic implementation method of Hongmeng system controlling LED
- 走入并行的世界
- 2.8、项目管理过程基础知识
- js方法传Long类型id值时会出现精确损失
- Leetcode skimming: binary tree 12 (all paths of binary tree)
- A way to calculate LNX
猜你喜欢
[quick start of Digital IC Verification] 6. Quick start of questasim (taking the design and verification of full adder as an example)
鸿蒙系统控制LED的实现方法之经典
Leetcode (695) - the largest area of an island
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
CTF reverse Foundation
Rainbond 5.7.1 支持对接多家公有云和集群异常报警
Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms
[quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives
A solution to PHP's inability to convert strings into JSON
Oracle-表空间管理
随机推荐
处理文件和目录名
Go language learning tutorial (XV)
C langue OJ obtenir PE, ACM démarrer OJ
DP:树DP
A solution to PHP's inability to convert strings into JSON
Unity编辑器扩展 UI控件篇
中金财富在网上开户安全吗?
Convolution free backbone network: Pyramid transformer to improve the accuracy of target detection / segmentation and other tasks (with source code)
Zero cloud new UI design
Scala基础【HelloWorld代码解析,变量和标识符】
[Yugong series] go teaching course in July 2022 004 go code Notes
[quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives
Leetcode skimming: binary tree 12 (all paths of binary tree)
Ffplay document [easy to understand]
Ros2 topic [01]: installing ros2 on win10
3.3、项目评估
Leetcode(695)——岛屿的最大面积
【c语言】快速排序的三种实现以及优化细节
小程序全局配置
mongodb基操的练习