add: 最大数和最小数

This commit is contained in:
yi-ge 2020-05-05 20:38:12 +08:00
parent 64843298d3
commit 2530c56119
3 changed files with 33 additions and 0 deletions

View File

@ -242,6 +242,10 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
- LeetCode 45. 跳跃游戏 II https://leetcode-cn.com/problems/jump-game-ii/ - LeetCode 45. 跳跃游戏 II https://leetcode-cn.com/problems/jump-game-ii/
- LintCode 117. 跳跃游戏 II https://www.lintcode.com/problem/jump-game-ii/description - LintCode 117. 跳跃游戏 II https://www.lintcode.com/problem/jump-game-ii/description
- [最大数和最小数](src/array/maximum-and-minimum.js)
LintCode 770. 最大数和最小数 https://www.lintcode.com/problem/maximum-and-minimum/description
## 栈 ## 栈
- [最大矩阵](src/stack/maximal-rectangle.js) - [最大矩阵](src/stack/maximal-rectangle.js)

View File

@ -0,0 +1,18 @@
/**
* @param matrix: an input matrix
* @return: nums[0]: the maximum,nums[1]: the minimum
*/
export const maxAndMin = function (matrix) {
if (matrix[0] === undefined || matrix[0][0] === undefined) return []
let max = matrix[0][0]
let min = matrix[0][0]
for (let n = 0, len = matrix.length; n < len; n++) {
for (let i = 0; i < matrix[0].length; i++) {
max = Math.max(max, matrix[n][i])
min = Math.min(min, matrix[n][i])
}
}
return [max, min]
}

View File

@ -0,0 +1,11 @@
import { maxAndMin } from '../../src/array/maximum-and-minimum'
test('最大数和最小数', () => {
expect(maxAndMin([
[1, 2, 3],
[4, 3, 2],
[6, 4, 4]
])).toEqual([6, 1])
expect(maxAndMin([])).toEqual([])
})