当前位置:网站首页>01. Sum of two numbers
01. Sum of two numbers
2022-07-25 17:09:00 【User 5573316】
#01. Sum of two numbers
difficulty : Simple
Given an array of integers nums And a target value target, Please find and as the target value in the array Two Integers , And return their array subscripts .
You can assume that each input corresponds to only one answer . however , The same element in an array cannot be used twice .
Example :
Given nums = [2, 7, 11, 15], target = 9
because nums[0] + nums[1] = 2 + 7 = 9
So back [0, 1]
# Violence law
# Ideas
Double loop cracking
Outer loop acquisition nums[0] To nums[length-2] Of i Index value
Memory cyclic acquisition nums[1] To nums[length-1] Of j Index value
Judge nums[i] + nums[j] == target Then return to [ i , j ]
# Code
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] arr = new int[2];
for(int i=0;i<nums.length;i++){
for(int j=(i+1);j<nums.length;j++){
if(nums[i]+nums[j] == target){
arr[0] = i;
arr[1] = j;
return arr;
}
}
}
throw new IllegalArgumentException("No two sum solution");
}
}
# Hashifa
# Ideas
Traverse nums
By judging each traversal key Is it satisfactory? nums[i] - target
If satisfied, the result is returned {map.get(nums[i]-target),i}
Every iteration map.put(nums[i], i)
# Code
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i] - target)) {
return new int[]{map.get(nums[i] - target), i};
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}
}
边栏推荐
- 超越 ConvNeXt、RepLKNet | 看 51×51 卷积核如何破万卷!
- ReBudget:通过运行时重新分配预算的方法,在基于市场的多核资源分配中权衡效率与公平性
- jenkins的文件参数,可以用来上传文件
- Rebudget汇报PPT
- IAAs infrastructure cloud cloud network
- Talk about how to use redis to realize distributed locks?
- 【目标检测】YOLOv5跑通VisDrone数据集
- 爬虫框架-crawler
- HCIP笔记十一天
- Gtx1080ti fiber HDMI interference flash screen 1080ti flash screen solution
猜你喜欢

【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part5-完结):信息检索与结果组装

The gas is exhausted! After 23 years of operation, the former "largest e-commerce website in China" has become yellow...

Why 4everland is the best cloud computing platform for Web 3.0

【目标检测】YOLOv5跑通VisDrone数据集

博云容器云、DevOps平台斩获可信云“技术最佳实践奖”

MySQL linked table query, common functions, aggregate functions

Birui data joins Alibaba cloud polardb open source database community
![[target detection] yolov5 Runtong visdrone data set](/img/a6/118e6bbeb254f9d1afd1406d1b0089.png)
[target detection] yolov5 Runtong visdrone data set
![[mathematical modeling and drawing series tutorial] II. Drawing and optimization of line chart](/img/73/2b6fe0cf69fa013894abce331e1386.png)
[mathematical modeling and drawing series tutorial] II. Drawing and optimization of line chart

Random talk on generation diffusion model: DDPM = Bayesian + denoising
随机推荐
生成扩散模型漫谈:DDPM = 贝叶斯 + 去噪
[redis] redis installation
China's chip self-sufficiency rate has increased significantly, resulting in high foreign chip inventories and heavy losses. American chips can be said to have thrown themselves in the foot
WPF implements user avatar selector
Exception handling mechanism topic 1
【redis】redis安装
Multi tenant software development architecture
[knowledge atlas] practice -- Practice of question and answer system based on medical knowledge atlas (Part5 end): information retrieval and result assembly
QT listview list display component notes
MySQL view
异常处理机制专题1
【目标检测】YOLOv5跑通VOC2007数据集(修复版)
Data analysis and privacy security become the key factors for the success or failure of Web3.0. How do enterprises layout?
2022 latest Beijing Construction welder (construction special operation) simulation question bank and answer analysis
基于redis6.2.4的redis cluster部署
win10设备管理认不到GTX1080Ti 显示设备的解决办法
Budget report ppt
mindoc制作思维导图
EasyUI DataGrid control uses
Unity is better to use the hot scheme Wolong