From e9a43aa6d3968d432ab236445b4f0af8c0ec4ff8 Mon Sep 17 00:00:00 2001 From: mmdapl <2237221210@qq.com> Date: Wed, 28 Apr 2021 23:00:57 +0800 Subject: [PATCH] [4.28 update coding] --- .DS_Store | Bin 8196 -> 8196 bytes 算法/.DS_Store | Bin 6148 -> 6148 bytes 算法/剑指/Readme.md | 9 +- 算法/剑指/数组和矩阵/FirstNotRepeatingChar.js | 41 +++++++++ 算法/剑指/数组和矩阵/printMatrix.js | 78 ++++++++++++++++++ 5 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 算法/剑指/数组和矩阵/FirstNotRepeatingChar.js create mode 100644 算法/剑指/数组和矩阵/printMatrix.js diff --git a/.DS_Store b/.DS_Store index 1984108c90c75b4cc39d94e24283c3e12954d8e0..c13e1a32eabe5d252fdfe95ba10e5b4207b073fd 100644 GIT binary patch delta 39 ncmZp1XmQwZQjo*Q(o9Fe(AaeIMZqpcIB)VrL8r}2h2wbu@(T;x delta 39 ncmZp1XmQwZQjo*c!cs@U(9~k{MZqpcIB)VrL8r}2h2wbu^Pdaw diff --git a/算法/.DS_Store b/算法/.DS_Store index 7184da07357e896b60039676b87ea695684c085a..0750febcfabf96fcb7041911c2ee91ebf75860f2 100644 GIT binary patch delta 25 gcmZoMXffDuo0Y@J(o9Fe(AaeHSr)m?jBLz80A{BK*Z=?k delta 25 gcmZoMXffDuo0Y@V!cs@U(9~k`Sr)m?jBLz80B0)(;s5{u diff --git a/算法/剑指/Readme.md b/算法/剑指/Readme.md index 95d2854..39ef043 100644 --- a/算法/剑指/Readme.md +++ b/算法/剑指/Readme.md @@ -4,7 +4,7 @@ * @Author: 【B站&公众号】Rong姐姐好可爱 * @Date: 2021-04-27 08:39:46 * @LastEditors: 【B站&公众号】Rong姐姐好可爱 - * @LastEditTime: 2021-04-27 10:09:28 + * @LastEditTime: 2021-04-28 22:27:46 --> @@ -17,4 +17,9 @@ - [【简单】数组中重复的数字](./数组和矩阵/duplicate.js) - [【中等】二维数组中的查找](./数组和矩阵/Find.js) - - [【较难】替换空格](./数组和矩阵/replaceSpace.js) \ No newline at end of file + - [【较难】替换空格](./数组和矩阵/replaceSpace.js) + - [【较难】顺时针打印矩阵](./数组和矩阵/printMatrix.js) + - [【简单】第一个只出现一次的字符位置](./数组和矩阵/FirstNotRepeatingChar.js) + + +### 栈队列堆 \ No newline at end of file diff --git a/算法/剑指/数组和矩阵/FirstNotRepeatingChar.js b/算法/剑指/数组和矩阵/FirstNotRepeatingChar.js new file mode 100644 index 0000000..a0d7d0c --- /dev/null +++ b/算法/剑指/数组和矩阵/FirstNotRepeatingChar.js @@ -0,0 +1,41 @@ +/* + * @Description: 第一个只出现一次的字符位置 + * @Version: Beta1.0 + * @Author: 【B站&公众号】Rong姐姐好可爱 + * @Date: 2021-04-28 22:23:51 + * @LastEditors: 【B站&公众号】Rong姐姐好可爱 + * @LastEditTime: 2021-04-28 22:24:20 + */ + + +function FirstNotRepeatingChar (str) { + // write code here + // 转换为数组 + let arr = str.split(''); + // 数组里面去去重 + for (let index = 0; index < arr.length; index++) { + if (arr.indexOf(arr[index]) === arr.lastIndexOf(arr[index])) { + return index + } + } + return -1 + +} + +function FirstNotRepeatingChar01 (str) { + // write code here + const len = str.length; + + for (let index = 0; index < len - 1; index++) { + const s = str.slice(index, index + 1); + if (`${str.slice(0, index)}${str.slice(index + 1)}`.indexOf(s) === -1) { + return index; + } + } + return -1 + +} + +module.exports = { + FirstNotRepeatingChar: FirstNotRepeatingChar +}; diff --git a/算法/剑指/数组和矩阵/printMatrix.js b/算法/剑指/数组和矩阵/printMatrix.js new file mode 100644 index 0000000..16bddaa --- /dev/null +++ b/算法/剑指/数组和矩阵/printMatrix.js @@ -0,0 +1,78 @@ +/* + * @Description: 顺时针打印矩阵 + * @Version: Beta1.0 + * @Author: 【B站&公众号】Rong姐姐好可爱 + * @Date: 2021-04-27 14:22:13 + * @LastEditors: 【B站&公众号】Rong姐姐好可爱 + * @LastEditTime: 2021-04-28 22:25:16 + */ + + +function printMatrix (matrix) { + // write code here + + // 行 角标 + let row = matrix.length - 1; + // 列角标 + if (matrix[0].length === 0) { + // 返回空数组 + return [] + } + let col = matrix[0].length - 1 + + let r = 0, + c = 0; + + // 这里需要在脑海里想象顺时针旋转的时候,r-->left c-->top row-->right col-->bottom + + + + // 数组结果 + let result = [] + while (r <= row && c <= col) { + + // 上 + for (let index = c; index <= col; index++) { + // 当前在上测,行为r 不为 row + result.push(matrix[r][index]) + } + + // 跳出后,到右侧 + for (let index = r + 1; index <= row; index++) { + result.push(matrix[index][col]) + } + // 跳出来,就是下测 判断右下角拐角 + if (r !== row) { + // 从右到左边 + for (let index = col - 1; index >= c; index--) { + // 当前在底部,所以行为row + result.push(matrix[row][index]) + } + } + // 左侧,注意判断条件 + if (c !== col) { + // 从下到上 + for (let index = row - 1; index > r; index--) { + // 当前在左侧,所以列表为c 部位col + result.push(matrix[index][c]) + } + } + + // 指针移动 转完一圈 + r++; + row--; + c++; + col--; + } + + return result; +} + + +console.log(printMatrix([ + [1, 2], + [3, 4] +])) +module.exports = { + printMatrix: printMatrix +}; \ No newline at end of file