当前位置:网站首页>leetcode-1. Sum of two numbers
leetcode-1. Sum of two numbers
2022-06-30 00:25:00 【Yunhu doesn't like it?】
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 .
such as :
Input [3,2,4],6 Then return to [1,2];(2+4 = 6)
Input [1,2,3,7,5],6 Then return to [0,4];(1+5 =6)
Their thinking
The two methods are the same , In different ways , Loop the original array twice , The current value is equal to the target value after adding the current value , You can return both subscripts
How to solve the problem
( Two )
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
for(let i = 0; i < nums.length-1; i++){
for(let j = i+1; j < nums.length; j++){
if(nums[i]+nums[j] == target){
return[i,j]
}
}
}
};( One )
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
// if(nums.length < 2)return;
// First set an empty array hRes, Store results
let hRes = [];
// Loop original array
for(let i = 0; i< nums.length; i++){
// After intercepting the current cyclic value of the original array, the string gets a new array that is not cycled to hNum
// Be careful Make a deep copy of the original array and then intercept
let hNum = JSON.parse(JSON.stringify(nums)).splice(i+1,nums.length);
// Traverse the new array
hNum.forEach((val,index,arr) => {
if(nums[i]+val == target){
hRes.push(i);
hRes.push(i+index+1);
}
})
}
// [...new Set(hRes)] duplicate removal
return [...new Set(hRes)];
};More trouble , Think the same , Put it another way , Later
Topic link :https://leetcode.cn/problems/two-sum
边栏推荐
- [QNX Hypervisor 2.2用户手册]6.2.2 Guest与Host之间通信
- Can't recognize the original appearance
- 8 software engineering environment
- 单位固定资产怎么管理,行政单位的固定资产应该怎么管理
- vsftp 与 TFTP 与 samba 与 nfs 复习
- Review of vsftp, TFTP, samba and NFS
- C MDI open subform to remove automatically generated menu bar
- gyctf_ 2020_ document
- Statistical query of SQL Server database
- Do mysqlcdc data not support windowing functions like row_ Number, lead
猜你喜欢

IDEA工具快捷键的使用

云呐|固定资产系统管理的优势,固定资产管理系统有何特点

Activity invitation | the Apache Doris community essay and speech solicitation activity has begun!

C MDI open subform to remove automatically generated menu bar
![[QNX Hypervisor 2.2用户手册]6.2.2 Guest与Host之间通信](/img/a4/a84f916d3aa2cc59f5b686cd32797a.png)
[QNX Hypervisor 2.2用户手册]6.2.2 Guest与Host之间通信

Vulnhub target -moriartycorp

单位固定资产怎么管理,行政单位的固定资产应该怎么管理
![[advanced C language] address book implementation](/img/e6/8a51d519d31ec323cf04c59a556325.png)
[advanced C language] address book implementation

Color space conversion in video tonemapping (HDR to SDR) (bt2020 to bt709, YCbCr, YUV and RGB)

There is no web-based development for the reward platform. Which is suitable for native development or mixed development?
随机推荐
HDCP Paring
Fine grained identification, classification, retrieval data set collation
MySQL Foundation 2
请指教在线开户是什么意思?另外想问,现在在线开户安全么?
[advanced C language] file operation (II)
Solr basic operation 10
js中的事件
Stream collectors usage
MySQL foundation 2
Several simple queries of SQL Server database
Solr basic operations 14
[graduation season 𞓜 advanced technology Er] employees who have worked for seven years do not want you to take another detour
There is no web-based development for the reward platform. Which is suitable for native development or mixed development?
Preliminary syntax of JS
Solr basic operations 7
云原生爱好者周刊:炫酷的 Grafana 监控面板集合
Mysql:sql overview and database system introduction | dark horse programmer
公司固定资产该哪个部门管理,一般公司固定资产怎么管理
How to write controller layer code gracefully?
Andorid source build/envsetup. SH details to know