diff --git a/README.md b/README.md index f05c852..199ebd6 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ - [1721. Swapping Nodes in a Linked List(medium)](./book_sources/medium/1721.md) - [2096. Step-By-Step Directions From a Binary Tree Node to Another(medium)](./book_sources/medium/2096.md) - [2196. Create Binary Tree From Descriptions(medium)](./book_sources/medium/2196.md) +- [2418. Sort the People(easy)](./book_sources/easy/2418.md) - [2751. Robot Collisions(hard)](./book_sources/hard/2751.md) ## 使用说明 diff --git a/book_sources/README.md b/book_sources/README.md index ad6e076..d79638f 100644 --- a/book_sources/README.md +++ b/book_sources/README.md @@ -125,5 +125,6 @@ - [1721. Swapping Nodes in a Linked List(medium)](./medium/1721.md) - [2096. Step-By-Step Directions From a Binary Tree Node to Another(medium)](./medium/2096.md) - [2196. Create Binary Tree From Descriptions(medium)](./medium/2196.md) +- [2418. Sort the People(easy)](./easy/2418.md) - [2751. Robot Collisions(hard)](./hard/2751.md) diff --git a/book_sources/SUMMARY.md b/book_sources/SUMMARY.md index 414abfe..38a9db9 100644 --- a/book_sources/SUMMARY.md +++ b/book_sources/SUMMARY.md @@ -30,7 +30,8 @@ - [1342. Number of Steps to Reduce a Number to Zero(easy)](./easy/1342.md) - [1380. Lucky Numbers in a Matrix(easy)](./easy/1380.md) - [1598. Crawler Log Folder(easy)](./easy/1598.md) - - [1710. Maximum Units on a Truck(medium)](./easy/1710.md) + - [1710. Maximum Units on a Truck(easy)](./easy/1710.md) + - [2418. Sort the People(easy)](./easy/2418.md) - [Meduim](medium/README.md) - [2. Add Two Numbers](./medium/2.md) - [3. Longest Substring Without Repeating Characters(medium)](./medium/3.md) diff --git a/book_sources/easy/2418.md b/book_sources/easy/2418.md new file mode 100644 index 0000000..da1dbb7 --- /dev/null +++ b/book_sources/easy/2418.md @@ -0,0 +1,38 @@ +# 2418. Sort the People + +> [Leetcode link](https://leetcode.com/problems/sort-the-people/) + +## 题目简介 + +题目给了我们两个数组 `names`, `heights` 要求我们把人物的名字根据身高排序 + +## 解题思路 + +这是一个简单题,关键的卡点在于我们要怎么在排序的时候维持两个数组元素的一致性 + +在 js 中,我们可以简单的用一个 map 来保存两者的映射关系,这样只要我们把 `heights` 排序好之后根据映射关系以此找回 `names` 就好 + +### Javascript + +```js +/** + * @param {string[]} names + * @param {number[]} heights + * @return {string[]} + */ +var sortPeople = function(names, heights) { + const map = {}; + // 建立映射关系 + names.forEach((item, index) => { + map[heights[index]] = item; + }) + // 排序 + heights.sort((a, b) => b-a); + // 根据映射关系取回名字 + heights.forEach((item, index) => { + names[index] = map[item]; + }) + return names; +}; +``` +