var Node = function (val) { //Node辅助类
this.val = val;
this.next = null;
};
var append = function (head,ele) {//添加元素
var node = new Node(ele);
var current;
if (head == null) {
head = node;
} else {
current = head;
while (current.next) {//找到最后一项
current = current.next;
}
current.next = node;//将最后一项的next指向 node
}
return head;
}
var mergeTwoLists = function(l1, l2) {//合并链表
if(l1 === null){
return l2;
} else if (l2 === null) {
return l1;
}
let list;
if(l1.val > l2.val){
list = l2;
list.next = mergeTwoLists(l2.next, l1);
} else {
list = l1;
list.next = mergeTwoLists(l2, l1.next)
}
return list;
};
var a1 = [0,2,5,7,9,10],a2 = [1,3,4,6,7,8];
var l1 = new Node("head");
var l2 = new Node("head");
for(var i=0;i
function reverse(nodeLst){
var pNode = nodeLst;
var pPre = null; //翻转之后 第一个节点的next值 为 null
var pNext;
while(pNode){
pNext = pNode.next; //获取到当前节点的下一个节点
pNode.next = pPre; //当前节点的后一个指向上一个节点
pPre = pNode; //上一个节点赋值为当前节点
pNode = pNext; //当前节点赋值为下一个节点
}
return pPre;
}
方法一:自执行函数
for (var i=0; i<5; i++) {
(function (i) {
setTimeout(() => console.log(i), 1000*i)
})(i)
}
方法二: es6块级作用域let
for (let i=0; i<5; i++) {
(function () {
setTimeout(() => console.log(i), 1000*i)
})()
}
方法三:传址传递
var out = (i) => {
setTimeout (() => console.log(i), 1000*i)
}
for (var i=0; i<5; i++) {
out(i)
}
方法四:Promise.all()方法
var arr = []
var output = (i) => new Promise(res => {
setTimeout(()=>{
console.log(i)
res()
}, 1000*i)
})
for (var i=0; i<5; i++) {
arr.push(output(i))
}
Promise.all(arr).then(()=> console.log(5))
var str="this is a book that is a desk";
var array=str.split(" ");
var map ={};
for(var i=0;i
function quickSort(arr){
//如果数组<=1则返回
if(arr.length<=1){
return arr;
}
//找基准
var num=Math.floor(arr.length/2);
//把基准删除
var numVal=arr.splice(num,1);
//定义左右数组
var left=[];
var right=[];
//比基准小的放在左,比基准大的放在右
for(i=0;i
function add(strNum1, strNum2) {
// 将传进来的数字/数字字符串调用toString方法转换成字符串,并进行切割,专成数组
let splitNum1 = strNum1.toString().split(''),
splitNum2 = strNum2.toString().split('')
// 判断两个数组的长度,进行值的互换,将splitNum1设置为最长的值,方便后续计算
if (splitNum1.length < splitNum2.length) {
let temp = splitNum1
splitNum1 = splitNum2
splitNum2 = temp
}
// carry: 进位值; currentNum: 相加之后,除以10的余数; sum: 相加的值
let len1 = splitNum1.length,
len2 = splitNum2.length,
carry = 0,
currentNum = 0,
sum = 0
// len1递减到1之后,循环体中的len1 - 1 = 0 即可拿到下标为零的数组元素,
// 所以这里条件是 大于 0,下面len2 > 0 同理
while (len1 > 0) {
if (len2 > 0) {
sum = parseInt(splitNum1[len1 - 1]) + parseInt(splitNum2[len2 - 1]) + carry
} else {
sum = parseInt(splitNum1[len1 - 1]) + carry
}
carry = Math.floor(sum / 10) // 进位数值
currentNum = sum % 10 // 取余数,作为当前位置的数值
splitNum1[len1 - 1] = currentNum // 设置当前值
// 相加之后,数值长度都递减
len1--
len2--
}
// 判断是否还有进位
if (carry) {
splitNum1.unshift(1)
}
return splitNum1.join('')
}
let result = add(9527, 2019)
console.log('result :', result) // result : 11546
console.log(bigMut("567", "1234")); // 699678
function bigMut(big, common) {
big += "";
common += "";
if (big.length < common.length) {
big = [common, common = big][0];
}
big = big.split("").reverse();
var oneMutManyRes = [];
var i = 0,
len = big.length;
for (; i < len; i++) {
oneMutManyRes[oneMutManyRes.length] = oneMutMany(big[i], common) + getLenZero(i);
}
var result = oneMutManyRes[0];
for (i = 1, len = oneMutManyRes.length; i < len; i++) {
result = bigNumAdd(result, oneMutManyRes[i]);
}
return result;
}
function getLenZero(len) {
len += 1;
var ary = [];
ary.length = len;
return ary.join("0");
}
function oneMutMany(one, many) {
one += "";
many += "";
if (one.length != 1) {
one = [many, many = one][0];
}
one = parseInt(one, 10);
var i = 0,
len = many.length,
resAry = [],
addTo = 0,
curItem,
curRes,
toSave;
many = many.split("").reverse();
for (; i <= len; i++) {
curItem = parseInt(many[i] || 0, 10);
curRes = curItem * one + addTo;
toSave = curRes % 10;
addTo = (curRes - curRes % 10) / 10;
resAry.unshift(toSave);
}
if (resAry[0] == 0) {
resAry.splice(0, 1);
}
return resAry.join("");
}
function bigNumAdd(big, common) {
big += "";
common += "";
var maxLen = Math.max(big.length, common.length),
bAry = big.split("").reverse(),
cAry = common.split("").reverse(),
i = 0,
addToNext = 0,
resAry = [],
fn,
sn,
sum;
for (; i <= maxLen; i++) {
fn = parseInt(bAry[i] || 0);
sn = parseInt(cAry[i] || 0);
sum = fn + sn + addToNext;
addToNext = (sum - sum % 10) / 10;
resAry.unshift(sum % 10);
}
if (resAry[0] == 0) {
resAry.splice(0, 1);
}
return resAry.join("");
}
方法一:Set(ES6 方法,一行代码)
function unique(arr){
return Array.from(new Set(arr));
}
方法二:for循环,使用splice去重
function unique(arr){
for(var i=0; i
方法三:indexOf
function unique(arr) {
var array = [];
for (var i = 0; i < arr.length; i++) {
if (array .indexOf(arr[i]) === -1) {
array .push(arr[i])
}
}
return array;
}
方法四:sort()(先排序)
function unique(arr) {
arr = arr.sort()
var arrry= [arr[0]];
for (var i = 1; i < arr.length; i++) {
if (arr[i] !== arr[i-1]) {
arrry.push(arr[i]);
}
}
return arrry;
}
方法五:includes
function unique(arr) {
var array =[];
for(var i = 0; i < arr.length; i++) {
if( !array.includes( arr[i]) ) {//includes 检测数组是否有某个值
array.push(arr[i]);
}
}
return array
}
方法六:hasOwnProperty
function unique(arr) {
var obj = {};
return arr.filter(function(item, index, arr){
return obj.hasOwnProperty(typeof item + item) ? false : (obj[typeof item + item] = true)
})
}
方法七:filter
function unique(arr) {
return arr.filter(function(item, index, arr) {
//当前元素,在原始数组中的第一个索引==当前索引值,否则返回当前元素
return arr.indexOf(item, 0) === index;
});
}
方法八:递归
function unique(arr) {
var array= arr;
var len = array.length;
array.sort(function(a,b){ //排序后更加方便去重
return a - b;
})
function loop(index){
if(index >= 1){
if(array[index] === array[index-1]){
array.splice(index,1);
}
loop(index - 1); //递归loop,然后数组去重
}
}
loop(len-1);
return array;
}
方法九:Map数据结构
function unique(arr) {
let map = new Map();
let array = new Array(); // 数组用于返回结果
for (let i = 0; i < arr.length; i++) {
if(map .has(arr[i])) { // 如果有该key值
map .set(arr[i], true);
} else {
map .set(arr[i], false); // 如果没有该key值
array .push(arr[i]);
}
}
return array ;
}
方法十:reduce+includes(一行代码)
function unique(arr){
return arr.reduce((prev,cur) => prev.includes(cur) ? prev : [...prev,cur],[]);
}
方法十一:[…new Set(arr)](一行代码)
function unique(arr){
return [...new Set(arr)];
}