当前位置:网站首页>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[]{
};
}
}
边栏推荐
- Netease UI automation test exploration: airtest+poco
- CC2530 common registers for timer 1
- NLP四范式:范式一:非神经网络时代的完全监督学习(特征工程);范式二:基于神经网络的完全监督学习(架构工程);范式三:预训练,精调范式(目标工程);范式四:预训练,提示,预测范式(Prompt工程)
- Meituan side: why does thread crash not cause JVM crash
- Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
- 网络安全web渗透技术
- Redis:关于列表List类型数据的操作命令
- Necessary ability of data analysis
- Processing strategy of message queue message loss and repeated message sending
- Leetcode: lucky number in matrix
猜你喜欢
CC2530 common registers for timer 1
(补)双指针专题
The most complete postman interface test tutorial in the whole network, API interface test
C语言按行修改文件
Build your own website (23)
线程池:业务代码最常用也最容易犯错的组件
CC2530 common registers for port interrupts
NLP四范式:范式一:非神经网络时代的完全监督学习(特征工程);范式二:基于神经网络的完全监督学习(架构工程);范式三:预训练,精调范式(目标工程);范式四:预训练,提示,预测范式(Prompt工程)
arduino-esp32:LVGL项目(一)整体框架
Thread pool executes scheduled tasks
随机推荐
[JDBC] API parsing
Simulink oscilloscope data is imported into Matlab and drawn
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
Preventing/catching “IllegalArgumentException: parameter must be a descendant of this view” error
Mysql database DDL and DML
Why is WPA3 security of enterprise business so important?
What kind of material is 14Cr1MoR? Analysis of chemical composition and mechanical properties of 14Cr1MoR
[sword finger offer] 58 - I. flip the word order
visual studio “通常每个套接字地址(协议/网络地址/端口)只允许使用一次“
跨境电商:外贸企业做海外社媒营销的优势
Thread pool: the most common and error prone component of business code
汇编实例解析--实模式下屏幕显示
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
What is the maximum number of concurrent TCP connections for a server? 65535?
比亚迪、长城混动市场再“聚首”
ANOVA example
Thread pool executes scheduled tasks
Bcvp developer community 2022 exclusive peripheral first bullet
Top k questions of interview
RF Analyze Demo搭建 Step by Step