当前位置:网站首页>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[]{
};
}
}
边栏推荐
- Add color to the interface automation test framework and realize the enterprise wechat test report
- 静态程序分析(一)—— 大纲思维导图与内容介绍
- Deep understanding of grouping sets statements in SQL
- Mysql database -dql
- 匯編實例解析--實模式下屏幕顯示
- The word backspace key cannot delete the selected text, so you can only press Delete
- function overloading
- Hong Kong Polytechnic University | data efficient reinforcement learning and adaptive optimal perimeter control of network traffic dynamics
- Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
- How to promote cross department project collaboration | community essay solicitation
猜你喜欢

Netease UI automation test exploration: airtest+poco

What is the difference between 14Cr1MoR container plate and 14Cr1MoR (H)? Chemical composition and performance analysis of 14Cr1MoR

Simulink oscilloscope data is imported into Matlab and drawn

What is the maximum number of concurrent TCP connections for a server? 65535?

大消费企业怎样做数字化转型?

Unreal_ Datatable implements ID self increment and sets rowname

Leetcode: lucky number in matrix

Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..

What material is sa537cl2? Analysis of mechanical properties of American standard container plate

CC2530 common registers for ADC single channel conversion
随机推荐
mysql用户管理
C语言按行修改文件
NLP四范式:范式一:非神经网络时代的完全监督学习(特征工程);范式二:基于神经网络的完全监督学习(架构工程);范式三:预训练,精调范式(目标工程);范式四:预训练,提示,预测范式(Prompt工程)
匯編實例解析--實模式下屏幕顯示
What material is 13crmo4-5 equivalent to in China? 13crmo4-5 chemical composition 13crmo4-5 mechanical properties
Preventing/catching “IllegalArgumentException: parameter must be a descendant of this view” error
Idea configuration plug-in
Necessary ability of data analysis
[combinatorics] recursive equation (characteristic equation and characteristic root | example of characteristic equation | root formula of monadic quadratic equation)
What material is 12cr1movr? Chemical property analysis of pressure vessel steel plate 12cr1movr
[combinatorics] recursive equation (example 1 of recursive equation | list recursive equation)
[combinatorics] non descending path problem (number of non descending paths with constraints)
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
Aike AI frontier promotion (7.3)
New features of C 10
What is the maximum number of concurrent TCP connections for a server? 65535?
Network security web penetration technology
The word backspace key cannot delete the selected text, so you can only press Delete
CC2530 common registers for timer 1
Thread pool executes scheduled tasks