当前位置:网站首页>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;
}边栏推荐
- Basic concept and classification of sorting
- Huawei operator level router configuration example | BGP VPLS and LDP VPLS interworking example
- 【小程序项目开发-- 京东商城】uni-app之分类导航区域
- Huawei operator level router configuration example | BGP VPLS configuration example
- Analyze datahub, a new generation metadata platform of 4.7K star
- How do spark tasks of 10W workers run? (Distributed Computing)
- Hal library setting STM32 interrupt
- 【小程序项目开发 -- 京东商城】uni-app 商品分类页面(上)
- Share Creators萌芽人才培養計劃來了!
- CX5120控制汇川IS620N伺服报错E15解决方案
猜你喜欢
性能测试常见面试题

Restcloud ETL data realizes incremental data synchronization through timestamp
![[machine learning] vectorized computing -- a must on the way of machine learning](/img/3f/d672bb254f845ea705b3a0ca10ee19.png)
[machine learning] vectorized computing -- a must on the way of machine learning

The best learning method in the world: Feynman learning method

手把手带你了解一块电路板,从设计到制作(干货)

POI导出excel,按照父子节点进行分级显示

Communication protocol -- Classification and characteristics Introduction

STM32——一线协议之DS18B20温度采样

Voici le programme de formation des talents de SHARE Creators!

最新接口自动化面试题
随机推荐
Redis分布式锁的8大坑
【小程序项目开发--京东商城】uni-app之自定义搜索组件(上)
Edge Drawing: A combined real-time edge and segment detector 翻译
PHP batch Excel to word
Mouse over effect IV
[exsi] transfer files between hosts
Druid monitoring statistics source
Saving images of different depths in opencv
Analyze datahub, a new generation metadata platform of 4.7K star
Restcloud ETL data realizes incremental data synchronization through timestamp
Chapitre 03 Bar _ Gestion des utilisateurs et des droits
MySQL index --01--- design principle of index
PTA 1017
[applet project development -- Jingdong Mall] classified navigation area of uni app
性能测试常见面试题
POI exports excel and displays hierarchically according to parent-child nodes
通信协议——分类及其特征介绍
Is it safe to open an account online in a small securities firm? Will my money be unsafe?
Big orange crazy blog move notice
Completely solve the lost connection to MySQL server at 'reading initial communication packet