当前位置:网站首页>One brush 144 force deduction hot question-1 sum of two numbers (E)
One brush 144 force deduction hot question-1 sum of two numbers (E)
2022-07-03 16:57:00 【Tang and Song Dynasties】
subject :
Given an array of integers nums And an integer target value target,
Please find... In the array And is the target value target the Two Integers , And return their array subscripts .
You can assume that each input corresponds to only one answer . however , The same element in the array cannot be repeated in the answer .
You can return the answers in any order .
--------------
Example :
Input :nums = [2,7,11,15], target = 9
Output :[0,1]
explain : because nums[0] + nums[1] == 9 , return [0, 1] .
Example 2:
Input :nums = [3,2,4], target = 6
Output :[1,2]
Example 3:
Input :nums = [3,3], target = 6
Output :[0,1]
----------------------
reflection :
Solution 1 : Violent search
Algorithm description
Two loops , The first cycle takes a number from front to back nums[i], Second cycle search target - nums[i] Whether in the remaining elements .
Complexity of time and space
The time complexity is O(n^2). Without considering the original array , The space complexity is O(1).
Code
class Solution {
public int[] twoSum(int[] nums, int target) {
if (nums == null || nums.length == 0) return new int[]{
};
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] == target - nums[j]) {
return new int[]{
i, j};
}
}
}
return new int[]{
};
}
}
Solution 2 : Hashtable 1
Algorithm description
Set the original array element value (key) And its subscripts (value) After saving a hash table , Traverse... With a loop nums,
With O(1) The time complexity is found in the hash table map.containsKey(target - nums[i]).
Complexity of time and space
Storing the original array into the hash table costs O(n) Time , Loop traversal cost O(n) Time ,
Each location of hash table only costs O(1) Time , So the time complexity is O(n).
The hashtable consumes space O(n), So the space complexity is zero O(n).
Code
class Solution {
public int[] twoSum(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return new int[]{
};
}
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
for (int i = 0; i < nums.length; i++) {
int need = target - nums[i];
if (map.containsKey(need)) {
if (i != map.get(need)) {
return new int[]{
i, map.get(need)};
}
}
}
return new int[]{
};
}
}
边栏推荐
- 跨境电商:外贸企业做海外社媒营销的优势
- Daily code 300 lines learning notes day 10
- 定义一个结构体Fraction,表示分数,用于表示 2/3, 5/6这样的分数
- What is the pledge pool and how to pledge?
- 手把手带你入门 API 开发
- 建立自己的网站(23)
- 【剑指 Offer】58 - II. 左旋转字符串
- [mathematical logic] equivalent calculus and reasoning calculus of propositional logic (propositional logic | equivalent calculus | principal conjunctive (disjunctive) paradigm | reasoning calculus)**
- Le zèbre a été identifié comme un chien, et la cause de l'erreur d'AI a été trouvée par Stanford
- [Jianzhi offer] 58 - ii Rotate string left
猜你喜欢

Atom QT 16_ audiorecorder

【Try to Hack】主动侦查隐藏技术
![[combinatorics] non descending path problem (number of non descending paths with constraints)](/img/89/bd1a2ddd9632ab5d4b4bee9336be51.jpg)
[combinatorics] non descending path problem (number of non descending paths with constraints)
![[combinatorics] polynomial theorem (polynomial theorem | polynomial theorem proof | polynomial theorem inference 1 item number is the number of non negative integer solutions | polynomial theorem infe](/img/9d/6118b699c0d90810638f9b08d4f80a.jpg)
[combinatorics] polynomial theorem (polynomial theorem | polynomial theorem proof | polynomial theorem inference 1 item number is the number of non negative integer solutions | polynomial theorem infe

网络安全web渗透技术

Leetcode: lucky number in matrix

Take you to API development by hand

Mysql database DDL and DML

Recommendation of good books on learning QT programming

Redis:关于列表List类型数据的操作命令
随机推荐
Thread pool: the most common and error prone component of business code
How to allow remote connection to MySQL server on Linux system?
ANOVA example
CC2530 common registers for serial communication
Visual studio "usually, each socket address (Protocol / network address / port) can only be used once“
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
Fast Ethernet and Gigabit Ethernet: what's the difference?
[combinatorics] recursive equation (example 1 of recursive equation | list recursive equation)
【剑指 Offer】58 - II. 左旋转字符串
智慧之道(知行合一)
[mathematical logic] equivalent calculus and reasoning calculus of propositional logic (propositional logic | equivalent calculus | principal conjunctive (disjunctive) paradigm | reasoning calculus)**
What material is sa537cl2? Analysis of mechanical properties of American standard container plate
MySQL Basics
What kind of material is 14Cr1MoR? Analysis of chemical composition and mechanical properties of 14Cr1MoR
深入理解 SQL 中的 Grouping Sets 语句
How to delete a specific line from a text file using the SED command?
IDEA-配置插件
Bcvp developer community 2022 exclusive peripheral first bullet
Kotlin学习快速入门(7)——扩展的妙用
建立自己的网站(23)