当前位置:网站首页>JS sum of two numbers
JS sum of two numbers
2022-06-12 18:02:00 【Out of the autistic bird】
Sum of two numbers
Two layers of for loop
// O(n^2)
const twoNum = function(nums,target){
for(let i =0;i<nums.length;i++){
for(let j=i+1 ;j<nums.length;j++){
if(nums[i]+nums[j]===target){
return[i,j]
}
}
}
}
Double pointer 
// When the array is ordered O(n)
const twoNum2 = function(nums,target){
let i = 0 // head
let j = nums.length-1 // tail
while (i<j){
const sum = nums[i]+nums[j]
if(sum>target){
j--
}else if(sum<target){
i++
}else{
return [i,j]
}
}
}
js Of map and set
Map
Is the structure of a set of key value pairs , With extremely fast search speed .
var m = new Map([['Michael', 95], ['Bob', 75], ['Tracy', 85]]);
m.get('Michael'); // 95
initialization Map We need a two-dimensional array , Or initialize a null directly Map.Map There are the following methods :
var m = new Map(); // empty Map
m.set('Adam', 67); // Add new key-value
m.has('Adam'); // Whether there is key 'Adam': true
m.get('Adam'); // 67
m.delete('Adam'); // Delete key 'Adam'
m.get('Adam'); // undefined
- clear Remove all elements from the map .
- delete Remove the specified element from the map .
- forEach Perform the specified operation on each element in the map .
- get Returns the specified element in the map .
- has If the map contains the specified element , Then return to true.
- set Add a new element to the map .
- toString Returns the string representation of the mapping .
- valueOf Returns the original value of the specified object .
Set
Set It's a data structure called a set , It's also a group. key Set , But no storage. value. because key Can't repeat , therefore , stay Set in , No repeat key.
To create a Set, Need to provide a Array As input , Or create an empty Set
var s1 = new Set(); // empty Set
var s2 = new Set([1, 2, 3]); // contain 1, 2, 3
Set Properties of :
- size: Returns the number of elements contained in the collection
Set Methods :
Operation method
- add(value): Add a new item to the collection
- delete(value): Remove a value from the collection
- has(value): If the value exists in the collection , return true, otherwise false
- clear(): Remove all items in the collection
Traversal methods
- keys(): Returns an array containing all the keys in the collection
- values(): Returns an array containing all the values in the collection
- entries: Returns an array containing all key value pairs in the collection
- forEach(): Used to perform some kind of operation on a collection member , no return value
边栏推荐
猜你喜欢

Vant3+ts dropdownmenu drop-down menu, multi data can be scrolled

USB转串口那些事儿—最大峰值串口波特率VS连续通信最高波特率

MySQL学习笔记

JDBC快速入门教程

js求斐波那契数列

Eve-ng installation (network device simulator)

轻量、便捷的小程序转App技术方案,实现与微信/流量App互联互通

Nixos 22.05 installation process record

LCD parameter interpretation and calculation

Introduction to reinforcement learning and analysis of classic items 1.3
随机推荐
Still using Microsoft office, 3 fairy software, are you sure you don't want to try?
js将数组中的0移动到末尾
Use the official go Library of mongodb to operate the original mongodb
USB转串口那些事儿—串口驱动类型
C operation database added business data value content case school table
C business serial number rule generation component
有关cookie的用法介绍
118. Yanghui triangle (dynamic planning)
关于数据集
AlibabaProtect. How to delete and uninstall exe
Error record: illegalstateexception: optional int parameter 'XXXX' is
Leetcode 674 longest incrementing substring
Use of split method of string
C#的变量
TypeScript高级类型(二)
Common dependencies of SSM
Stream流注意点
vant3+ts h5页面嵌套进app 与原生app通信
Reconnaître l'originalité de la fonction
Vant3+ts encapsulates uploader upload image component