当前位置:网站首页>Force buckle - sum of two numbers
Force buckle - sum of two numbers
2022-07-01 03:15:00 【Curious rookie】
Power button - Sum of two numbers
One 、 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 1:
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]
Two 、 solution 1
public int[] twoSum1(int[] nums, int target) {
int[] indexs = new int[2];
// Double cycle The cycle limit is (n^2-n)/2
for (int i = 0; i < nums.length; i++) {
for (int j = nums.length - 1; j > i; j--) {
if (nums[i] + nums[j] == target) {
indexs[0] = i;
indexs[1] = j;
System.out.println(Arrays.toString(indexs));
return indexs;
}
}
}
return indexs;
}3、 ... and 、 solution 2
public int[] twoSum3(int[] nums, int target) {
int[] indexs = new int[2];
// establish k-v , One to one hash table
HashMap<Integer, Integer> hash = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
if (hash.containsKey(nums[i])) {
indexs[0] = i;
indexs[1] = hash.get(nums[i]);
System.out.println(Arrays.toString(indexs));
return indexs;
}
// Store data key Complement number ,value Subscript
hash.put(target - nums[i], i);
}
return indexs;
}边栏推荐
- Dart training and sphygmomanometer inflation pump power control DPC
- PHP batch Excel to word
- Elk elegant management server log
- LeetCode_栈_困难_227.基本计算器(不含乘除)
- Hello World generation
- # 使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'
- Introduction to the core functions of webrtc -- an article to understand peerconnectionfactoryinterface rtcconfiguration peerconnectioninterface
- Restcloud ETL practice to realize incremental data synchronization without identification bit
- 限流组件设计实战
- Mouse over effect 8
猜你喜欢

Best used trust automation script (shell)
![Servlet [first introduction]](/img/2a/aff3b93e43550d30a33c1683210d3a.png)
Servlet [first introduction]

Latest interface automation interview questions

How to verify whether the contents of two files are the same

Redis efficient like and cancel function

Const and the secret of pointers

Network address translation (NAT) technology

基于Pytorch完整的训练一个神经网络并进行验证

安装VCenter6.7【VCSA6.7(vCenter Server Appliance 6.7) 】
![Lavaweb [first understanding the solution of subsequent problems]](/img/8a/08cb2736c2c198d926dbe00c004c3f.png)
Lavaweb [first understanding the solution of subsequent problems]
随机推荐
第03章_用戶與權限管理
A shooting training method based on the digital measurement of Joule energy and posture of sphygmomanometer air bag with standard air pressure
Design practice of current limiting components
世界上最好的学习法:费曼学习法
[exsi] transfer files between hosts
几行事务代码,让我赔了16万
【机器学习】向量化计算 -- 机器学习路上必经路
Big orange crazy blog move notice
STM32——一线协议之DS18B20温度采样
Is it safe to open an account online in a small securities firm? Will my money be unsafe?
LeetCode_栈_困难_227.基本计算器(不含乘除)
如果在小券商办理网上开户安全吗?我的资金会不会不安全?
About the application of MySQL
Restcloud ETL data realizes incremental data synchronization through timestamp
Detailed explanation of pointer array and array pointer (comprehensive knowledge points)
An article explaining the publisher subscriber model and the observer model
The best learning method in the world: Feynman learning method
通信协议——分类及其特征介绍
XXL job User Guide
Huawei operator level router configuration example | configuration static VPLS example