当前位置:网站首页>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[]{
};
}
}
边栏推荐
- 2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
- (Supplement) double pointer topic
- [combinatorics] recursive equation (the relationship theorem between the solution of the recursive equation and the characteristic root | the linear property theorem of the solution of the recursive e
- [Jianzhi offer] 57 - ii Continuous positive sequence with sum s
- [Jianzhi offer] 58 - ii Rotate string left
- [combinatorics] recursive equation (constant coefficient linear homogeneous recursive equation | constant coefficient, linear, homogeneous concept description | constant coefficient linear homogeneous
- Learn from me about the enterprise flutter project: simplified framework demo reference
- [try to hack] active detection and concealment technology
- Acwing game 58
- 中南大学|通过探索理解: 发现具有深度强化学习的可解释特征
猜你喜欢

The most complete postman interface test tutorial in the whole network, API interface test

What is the material of sa302grc? American standard container plate sa302grc chemical composition

Add color to the interface automation test framework and realize the enterprise wechat test report

New features of C 10

线程池:业务代码最常用也最容易犯错的组件

QT serial port UI design and solution to display Chinese garbled code

13mnnimo5-4 German standard steel plate 13MnNiMo54 boiler steel 13MnNiMo54 chemical properties

爱可可AI前沿推介(7.3)

Web crawler knowledge day03

What material is 12cr1movr? Chemical property analysis of pressure vessel steel plate 12cr1movr
随机推荐
CC2530 common registers for ADC single channel conversion
远程办公之如何推进跨部门项目协作 | 社区征文
How to delete a specific line from a text file using the SED command?
[combinatorics] recursive equation (example 1 of recursive equation | list recursive equation)
function overloading
Difference between JSON and bson
Alibaba P8 painstakingly sorted it out. Summary of APP UI automated testing ideas. Check it out
C语言按行修改文件
MySQL user management
The way of wisdom (unity of knowledge and action)
13mnnimo5-4 German standard steel plate 13MnNiMo54 boiler steel 13MnNiMo54 chemical properties
IDEA-配置插件
Netease UI automation test exploration: airtest+poco
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
智慧之道(知行合一)
斑马识别成狗,AI犯错的原因被斯坦福找到了
执行脚本不认\r
[Jianzhi offer] 57 - ii Continuous positive sequence with sum s
Static program analysis (I) -- Outline mind map and content introduction
BYD and great wall hybrid market "get together" again