mirror of
https://github.com/142vip/408CSFamily.git
synced 2026-11-05 22:59:00 +08:00
[4.28 update coding]
This commit is contained in:
BIN
算法/.DS_Store
vendored
BIN
算法/.DS_Store
vendored
Binary file not shown.
@@ -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)
|
||||
- [【较难】替换空格](./数组和矩阵/replaceSpace.js)
|
||||
- [【较难】顺时针打印矩阵](./数组和矩阵/printMatrix.js)
|
||||
- [【简单】第一个只出现一次的字符位置](./数组和矩阵/FirstNotRepeatingChar.js)
|
||||
|
||||
|
||||
### 栈队列堆
|
||||
41
算法/剑指/数组和矩阵/FirstNotRepeatingChar.js
Normal file
41
算法/剑指/数组和矩阵/FirstNotRepeatingChar.js
Normal file
@@ -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
|
||||
};
|
||||
78
算法/剑指/数组和矩阵/printMatrix.js
Normal file
78
算法/剑指/数组和矩阵/printMatrix.js
Normal file
@@ -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
|
||||
};
|
||||
Reference in New Issue
Block a user