Skip to content

Commit c2bfc61

Browse files
committed
feat:华为笔试题
1 parent 54649ef commit c2bfc61

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

dp/maxSubArray.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ const maxSubArray = function (nums) {
1414
return max;
1515
};
1616

17-
const arr = [-2, 1, 3, 4, -1, 2, 1, -5, 4];
17+
const arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4];
1818

1919
console.log(maxSubArray(arr));

dp/maxSubArray.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function maxSubArray(nums: number[]): number {
2+
const dp: number[] = [];
3+
dp[0] = nums[0];
4+
for (let i = 1; i < nums.length; i++) {
5+
if (dp[i - 1] > 0) dp[i] = dp[i - 1] + nums[i];
6+
else dp[i] = nums[i];
7+
}
8+
return Math.max(...dp);
9+
}
10+
11+
const nums = [-2,1,-3,4,-1,2,1,-5,4];
12+
console.log(maxSubArray(nums));

huawei/climbStairs.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 经典的爬台阶问题,一次走一个台阶或者三个台阶,求走n级台阶总共有多少种走法,台阶数不超过50
3+
* @param {number} n
4+
* @return {number}
5+
*/
6+
var climbStairs = function(n) {
7+
if (n < 3) {
8+
return 1;
9+
}
10+
if (n === 3) {
11+
return 2;
12+
}
13+
return climbStairs(n - 1) + climbStairs(n - 3);
14+
};
15+
console.log(climbStairs(50));
16+
var dpClimbStairs = function(n) {
17+
const dp = [];
18+
dp[0] = 1, dp[1] = 1, dp[2] = 2;
19+
for (let i = 3; i < n; i++) {
20+
dp[i] = dp[i - 1] + dp[i - 3];
21+
}
22+
return dp[n - 1];
23+
};
24+
console.log(dpClimbStairs(50));

huawei/squre.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
const len = 3
3+
const square = [];
4+
arr = [
5+
'8 1 6',
6+
'3 5 7',
7+
'4 99 2'
8+
]
9+
for (const item of arr) {
10+
let lineArr = item.split(' ');
11+
for (let i = 0; i < len; i++) {
12+
lineArr[i] = parseInt(lineArr[i]);
13+
}
14+
square.push(lineArr);
15+
}
16+
17+
const lineCount = [];
18+
const colCount = [];
19+
let lineError;
20+
let colError;
21+
let rightCount;
22+
23+
for (let i = 0; i < len; i++) {
24+
let lcount = 0;
25+
let ccount = 0;
26+
for (let j = 0; j < len; j++) {
27+
lcount += square[i][j];
28+
ccount += square[j][i];
29+
}
30+
lineCount.push(lcount);
31+
colCount.push(ccount);
32+
}
33+
34+
const lineCopy = [...lineCount];
35+
const colCopy = [...colCount];
36+
37+
lineCopy.sort();
38+
if (lineCopy[0] !== lineCopy[1] && lineCopy[1] === lineCopy[2]) {
39+
lineError = lineCopy[0];
40+
rightCount = lineCopy[1];
41+
} else {
42+
lineError = lineCopy[len - 1];
43+
rightCount = lineCopy[0];
44+
}
45+
colCopy.sort();
46+
if (colCopy[0] !== colCopy[1] && colCopy[1] === colCopy[2]) {
47+
colError = colCopy[0];
48+
} else {
49+
colError = colCopy[len - 1];
50+
}
51+
const lineIndex = lineCount.indexOf(lineError);
52+
const colIndex = colCount.indexOf(colError);
53+
const rightNum = rightCount - lineError + square[lineIndex][colIndex];
54+
55+
console.log(`${lineIndex + 1} ${colIndex + 1} ${rightNum}`);
56+

0 commit comments

Comments
 (0)