|
1 | 1 | [#1387-sort-integers-by-the-power-value]
|
2 |
| -= 1387. Sort Integers by The Power Value |
| 2 | += 1387. 将整数按权重排序 |
3 | 3 |
|
4 |
| -{leetcode}/problems/sort-integers-by-the-power-value/[LeetCode - 1387. Sort Integers by The Power Value ^] |
| 4 | +https://leetcode.cn/problems/sort-integers-by-the-power-value/[LeetCode - 1387. 将整数按权重排序 ^] |
5 | 5 |
|
6 |
| -The power of an integer `x` is defined as the number of steps needed to transform `x` into `1` using the following steps: |
| 6 | +我们将整数 `x` 的 *权重* 定义为按照下述规则将 `x` 变成 `1` 所需要的步数: |
7 | 7 |
|
| 8 | +* 如果 `x` 是偶数,那么 `+x = x / 2+` |
| 9 | +* 如果 `x` 是奇数,那么 `+x = 3 * x + 1+` |
8 | 10 |
|
9 |
| -* if `x` is even then `x = x / 2` |
10 |
| -* if `x` is odd then `x = 3 * x + 1` |
| 11 | +比方说,x=3 的权重为 7 。因为 3 需要 7 步变成 1 (3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1)。 |
11 | 12 |
|
| 13 | +给你三个整数 `lo`, `hi` 和 `k` 。你的任务是将区间 `[lo, hi]` 之间的整数按照它们的权重 *升序排序 *,如果大于等于 2 个整数有 *相同* 的权重,那么按照数字自身的数值 *升序排序* 。 |
12 | 14 |
|
13 |
| -For example, the power of `x = 3` is `7` because `3` needs `7` steps to become `1` (`3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1`). |
| 15 | +请你返回区间 `[lo, hi]` 之间的整数按权重排序后的第 `k` 个数。 |
14 | 16 |
|
15 |
| -Given three integers `lo`, `hi` and `k`. The task is to sort all integers in the interval `[lo, hi]` by the power value in *ascending order*, if two or more integers have *the same* power value sort them by *ascending order*. |
| 17 | +注意,题目保证对于任意整数 `x` `+(lo <= x <= hi)+` ,它变成 `1` 所需要的步数是一个 32 位有符号整数。 |
16 | 18 |
|
17 |
| -Return the `k^th^` integer in the range `[lo, hi]` sorted by the power value. |
| 19 | +*示例 1:* |
18 | 20 |
|
19 |
| -Notice that for any integer `x` `(lo <= x <= hi)` it is *guaranteed* that `x` will transform into `1` using these steps and that the power of `x` is will *fit* in a 32-bit signed integer. |
| 21 | +.... |
| 22 | +输入:lo = 12, hi = 15, k = 2 |
| 23 | +输出:13 |
| 24 | +解释:12 的权重为 9(12 --> 6 --> 3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1) |
| 25 | +13 的权重为 9 |
| 26 | +14 的权重为 17 |
| 27 | +15 的权重为 17 |
| 28 | +区间内的数按权重排序以后的结果为 [12,13,14,15] 。对于 k = 2 ,答案是第二个整数也就是 13 。 |
| 29 | +注意,12 和 13 有相同的权重,所以我们按照它们本身升序排序。14 和 15 同理。 |
| 30 | +.... |
20 | 31 |
|
21 |
| - |
22 |
| -*Example 1:* |
| 32 | +*示例 2:* |
23 | 33 |
|
24 |
| -[subs="verbatim,quotes"] |
25 |
| ----- |
26 |
| -*Input:* lo = 12, hi = 15, k = 2 |
27 |
| -*Output:* 13 |
28 |
| -*Explanation:* The power of 12 is 9 (12 --> 6 --> 3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1) |
29 |
| -The power of 13 is 9 |
30 |
| -The power of 14 is 17 |
31 |
| -The power of 15 is 17 |
32 |
| -The interval sorted by the power value [12,13,14,15]. For k = 2 answer is the second element which is 13. |
33 |
| -Notice that 12 and 13 have the same power value and we sorted them in ascending order. Same for 14 and 15. |
34 |
| ----- |
35 |
| - |
36 |
| -*Example 2:* |
37 |
| - |
38 |
| -[subs="verbatim,quotes"] |
39 |
| ----- |
40 |
| -*Input:* lo = 7, hi = 11, k = 4 |
41 |
| -*Output:* 7 |
42 |
| -*Explanation:* The power array corresponding to the interval [7, 8, 9, 10, 11] is [16, 3, 19, 6, 14]. |
43 |
| -The interval sorted by power is [8, 10, 11, 7, 9]. |
44 |
| -The fourth number in the sorted array is 7. |
45 |
| ----- |
| 34 | +.... |
| 35 | +输入:lo = 7, hi = 11, k = 4 |
| 36 | +输出:7 |
| 37 | +解释:区间内整数 [7, 8, 9, 10, 11] 对应的权重为 [16, 3, 19, 6, 14] 。 |
| 38 | +按权重排序后得到的结果为 [8, 10, 11, 7, 9] 。 |
| 39 | +排序后数组中第 4 个数字为 7 。 |
| 40 | +.... |
46 | 41 |
|
47 |
| - |
48 |
| -*Constraints:* |
49 | 42 |
|
| 43 | +*提示:* |
50 | 44 |
|
51 |
| -* `1 <= lo <= hi <= 1000` |
52 |
| -* `1 <= k <= hi - lo + 1` |
53 |
| - |
| 45 | +* `+1 <= lo <= hi <= 1000+` |
| 46 | +* `+1 <= k <= hi - lo + 1+` |
54 | 47 |
|
55 | 48 |
|
56 | 49 |
|
@@ -82,4 +75,5 @@ include::{sourcedir}/_1387_SortIntegersByThePowerValue.java[tag=answer]
|
82 | 75 |
|
83 | 76 | == 参考资料
|
84 | 77 |
|
85 |
| - |
| 78 | +. https://leetcode.cn/problems/sort-integers-by-the-power-value/solutions/168355/jiang-zheng-shu-an-quan-zhong-pai-xu-by-leetcode-s/[1387. 将整数按权重排序 - 官方题解^] |
| 79 | +. https://leetcode.cn/problems/sort-integers-by-the-power-value/solutions/3011028/bing-bao-cai-xiang-ji-yi-hua-sou-suo-pyt-q5iz/[1387. 将整数按权重排序 - 冰雹猜想,记忆化搜索^] |
0 commit comments