經典面試題


查重

(1)利用for循環需要45次

(2)利用對象不能有同一個屬性 需要10次

 

冒泡排序

 

數組的去重

 

字符串的去重

 

統計字符串出現最多的次數

 

查找沒有重復的字符串

 

深度拷貝

 

聖杯模式

 

insertAfter方法

查找數組里面最小的值

通過二分法查找(學習其中思想,不是為了解題)

 

代碼

 

 1 let arr = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 14, 16]]
 2 
 3         function matrix(arr) {
 4             //設置方向
 5             let direction = {
 6                 //向右移動 行不變 列加一
 7                 'RIGHT': {
 8                     'ROW': 0,
 9                     'COL': +1,
10                 },
11                 //向下移動 行加一 列不變
12                 'DOWN': {
13                     'ROW': +1,
14                     'COL': 0
15                 },
16                 //向右移動 行不變 列減一
17                 'LEFT': {
18                     'ROW': 0,
19                     'COL': -1
20                 },
21                 //向右移動 行減一 列不變
22                 'UP': {
23                     'ROW': -1,
24                     'COL': 0
25                 }
26             }
27             //設置最大行數
28             let maxRow = arr.length - 1;
29             //設置最大列數
30             let maxCol = arr[0].length - 1;
31             //最小行數
32             let minRow = 0;
33             //最小列數
34             let minCol = 0;
35             //獲取行的長度 便於循環
36             let rowLen = arr.length;
37             //獲取列的長度 便於循環
38             let colLen = arr[0].length;
39             //設置初始運動默認方向
40             let initialDirection = direction.RIGHT;
41             //設置接收值的數組
42             let newArr = [];
43             //設置當前的行的位置
44             let nowRow = 0;
45             //設置當前的列的位置
46             let nowCol = 0;
47             
48             //循環整個矩陣
49             for(let i = 0; i < rowLen * colLen; i++){
50                 //添加每一位數據
51                 newArr.push(arr[nowRow][nowCol])
52                 //默認的行運動的值
53                 nowRow += initialDirection.ROW;
54                 //默認的列運動的值
55                 nowCol += initialDirection.COL
56                 //判斷是否為右拐點 row最小 col最大
57                 if(initialDirection == direction.RIGHT && nowRow == minRow && nowCol == maxCol){
58                     initialDirection = direction.DOWN //改變方向
59                     //判斷是否為下拐點 row最大 col最大
60                 }else if (initialDirection == direction.DOWN && nowRow == maxRow && nowCol == maxCol){
61                     initialDirection = direction.LEFT //改變方向
62                     //判斷是否為左拐點 row最大 col最小
63                 }else if (initialDirection == direction.LEFT && nowRow == maxRow && nowCol == minCol){
64                     initialDirection = direction.UP //改變方向
65                     //判斷是否為左拐點 row最小 col最小
66                 }else if (initialDirection == direction.UP && nowRow == minRow && nowCol == minCol){
67                     initialDirection = direction.RIGHT //改變方向
68                     //走完一圈 縮圈 讓 row加一 列不變
69                 }else if(nowRow == minRow + 1 && nowCol == minCol){
70                     maxRow --; // 最大行減小
71                     maxCol --; // 最大列減小
72                     minRow ++; // 最小行增加
73                     minCol ++; // 最小列增加
74                     initialDirection = direction.RIGHT; //最后改變方向
75                 }
76             }
77             return newArr //返回最終的數組結構
78         }
79     
80         console.log(matrix(arr)) //[1, 2, 3, 4, 8, 12, 16, 14, 14, 13, 9, 5, 6, 7, 11, 10]

 

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM