From c4257591206a2400487948c3c50a743ed85b9781 Mon Sep 17 00:00:00 2001 From: hejialiang <hejialianghe@qq.com> Date: Mon, 18 Mar 2024 22:54:44 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E6=95=B0=E6=8D=AE=E7=BB=93?= =?UTF-8?q?=E6=9E=84:=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/algorithm/dataStructure.md | 2 ++ examples/algorithm/dataStructure.js | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 examples/algorithm/dataStructure.js diff --git a/docs/algorithm/dataStructure.md b/docs/algorithm/dataStructure.md index c307d3a..612db58 100644 --- a/docs/algorithm/dataStructure.md +++ b/docs/algorithm/dataStructure.md @@ -190,6 +190,8 @@ map 方法创建了一个长度为 n 的数组,占用了 n 大小的空间。 #### 方式 2: 异或比较法 +异或比较法是一种常用的技术,通常用于比较两个数据结构或者变量的不同之处。在计算机科学和编程中,异或操作符(^)用于比较两个二进制数的每一位,如果相应位相同则结果为0,如果相应位不同则结果为1。 + 异或运算符可以将两个数字比较,由于有一个数只出现了一次,其他数皆出现了两次,类似乘法 则无论先后顺序,最后相同的数都会异或成 0,唯一出现的数与 0 异或就会得到其本身,该方法是最优解,直接通过比较的方式即可得到只出现一次的数字。 ```js diff --git a/examples/algorithm/dataStructure.js b/examples/algorithm/dataStructure.js new file mode 100644 index 0000000..126f6e1 --- /dev/null +++ b/examples/algorithm/dataStructure.js @@ -0,0 +1,25 @@ + +// 2.2 数组 + +// 2.2.1 找出出现一次的数字 +// const arr = [2,5,6,7,5,6,2] + +// const num = arr.reduce((total,cur)=>total ^ cur) + +// console.log("num",num) + +// 2.2.2 两数求和问题 + +const nums = [5, 7, 8, 2, 4] +const target = 9 +const res ={} +let result = [] +nums.forEach((item,i)=>{ + if(typeof res[target-item] ==='number'){ + result.push([item,target-item]) + }else{ + res[item] = i + } +}) + +console.log('result',result) \ No newline at end of file From b226e44cab58235a64a24407fcfb8781c1f2e242 Mon Sep 17 00:00:00 2001 From: hejialiang <hejialianghe@qq.com> Date: Wed, 20 Mar 2024 23:14:48 +0800 Subject: [PATCH 2/2] =?UTF-8?q?refactor(datastructure.md):=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=90=88=E5=B9=B62=E4=B8=AA=E6=9C=89=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/algorithm/dataStructure.md | 27 +++++++---------- examples/algorithm/dataStructure.js | 45 ++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/docs/algorithm/dataStructure.md b/docs/algorithm/dataStructure.md index 612db58..2981f1d 100644 --- a/docs/algorithm/dataStructure.md +++ b/docs/algorithm/dataStructure.md @@ -287,23 +287,16 @@ let j = num2.length - 1 // m是num1和num2合并后长度 let m = k + j + 1 -while (k >= 0 && j >= 0) { - if (num2[j] > num1[k]) { - num1[m] = num2[j] - j-- - } else { - num1[m] = num1[k] - k-- - } - m-- -} -// 特殊情况:1.num1先遍历完,2.num2先遍历完。 -// num2先遍历完,我们不用处理,因为我们就是把num2合并到num1。 -// num1先遍历完,我们把num2全部复制到num1中。 -while (j >= 0) { - num1[m] = num2[j] - j-- - m-- +// 将num2中所有数据合并到num1中,当num2的指针小于0才结束 +while (y >= 0) { + if (num1[x] > num2[y]) { + num1[m] = num1[x] + x-- + } else { + num1[m] = num2[y] + y-- + } + m-- } ``` diff --git a/examples/algorithm/dataStructure.js b/examples/algorithm/dataStructure.js index 126f6e1..ae35a16 100644 --- a/examples/algorithm/dataStructure.js +++ b/examples/algorithm/dataStructure.js @@ -10,16 +10,35 @@ // 2.2.2 两数求和问题 -const nums = [5, 7, 8, 2, 4] -const target = 9 -const res ={} -let result = [] -nums.forEach((item,i)=>{ - if(typeof res[target-item] ==='number'){ - result.push([item,target-item]) - }else{ - res[item] = i - } -}) - -console.log('result',result) \ No newline at end of file +// const nums = [5, 7, 8, 2, 4] +// const target = 9 +// const res ={} +// let result = [] +// nums.forEach((item,i)=>{ +// if(typeof res[target-item] ==='number'){ +// result.push([item,target-item]) +// }else{ +// res[item] = i +// } +// }) + +// console.log('result',result) + +// 2.2.3 合并2个有序数组 +let num1 = [1,3,5,8,10,16]; +let num2 = [2,4,5,6,7,9]; + +let x = num1.length-1 +let y = num2.length-1 +let m = x+y+1 +while(y>=0){ + if(num1[x]>num2[y]){ + num1[m] = num1[x] + x-- + }else{ + num1[m] = num2[y] + y-- + } + m-- +} +console.log('num1',num1)