Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1ef5cc7

Browse files
✨feat: add 556
1 parent 1a88722 commit 1ef5cc7

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

‎Index/双指针.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
| [519. 随机翻转矩阵](https://leetcode-cn.com/problems/random-flip-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/random-flip-matrix/solution/gong-shui-san-xie-note-bie-pian-yi-ti-sh-e6gi/) | 中等 | 🤩🤩🤩🤩 |
2424
| [524. 通过删除字母匹配到字典里最长单词](https://leetcode-cn.com/problems/longest-word-in-dictionary-through-deleting/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-word-in-dictionary-through-deleting/solution/gong-shui-san-xie-xiang-jie-pai-xu-shuan-qi20/) | 中等 | 🤩🤩🤩🤩 |
2525
| [532. 数组中的 k-diff 数对](https://leetcode.cn/problems/k-diff-pairs-in-an-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/k-diff-pairs-in-an-array/solution/by-ac_oier-ap3v/) | 中等 | 🤩🤩🤩🤩 |
26+
| [556. 下一个更大元素 III](https://leetcode.cn/problems/next-greater-element-iii/) | [LeetCode 题解链接](https://leetcode.cn/problems/next-greater-element-iii/solution/by-ac_oier-99bj/) | 中等 | 🤩🤩🤩🤩 |
2627
| [581. 最短无序连续子数组](https://leetcode-cn.com/problems/shortest-unsorted-continuous-subarray/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/shortest-unsorted-continuous-subarray/solution/gong-shui-san-xie-yi-ti-shuang-jie-shuan-e1le/) | 中等 | 🤩🤩🤩🤩 |
2728
| [594. 最长和谐子序列](https://leetcode-cn.com/problems/longest-harmonious-subsequence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-harmonious-subsequence/solution/gong-shui-san-xie-yi-ti-shuang-jie-hua-d-quuh/) | 简单 | 🤩🤩🤩🤩 |
2829
| [611. 有效三角形的个数](https://leetcode-cn.com/problems/valid-triangle-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/valid-triangle-number/solution/gong-shui-san-xie-yi-ti-san-jie-jian-dan-y1we/) | 中等 | 🤩🤩🤩🤩 |

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
| [539. 最小时间差](https://leetcode-cn.com/problems/minimum-time-difference/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-time-difference/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-eygg/) | 中等 | 🤩🤩🤩🤩 |
7474
| [541. 反转字符串 II](https://leetcode-cn.com/problems/reverse-string-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-string-ii/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-p88f/) | 简单 | 🤩🤩🤩🤩🤩 |
7575
| [551. 学生出勤记录 I](https://leetcode-cn.com/problems/student-attendance-record-i/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/student-attendance-record-i/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-hui7/) | 简单 | 🤩🤩🤩 |
76+
| [556. 下一个更大元素 III](https://leetcode.cn/problems/next-greater-element-iii/) | [LeetCode 题解链接](https://leetcode.cn/problems/next-greater-element-iii/solution/by-ac_oier-99bj/) | 中等 | 🤩🤩🤩🤩 |
7677
| [566. 重塑矩阵](https://leetcode-cn.com/problems/reshape-the-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reshape-the-matrix/solution/jian-dan-ti-zhong-quan-chu-ji-ke-yi-kan-79gv5/) | 简单 | 🤩🤩🤩 |
7778
| [591. 标签验证器](https://leetcode-cn.com/problems/tag-validator/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/tag-validator/solution/by-ac_oier-9l8z/) | 困难 | 🤩🤩🤩🤩 |
7879
| [594. 最长和谐子序列](https://leetcode-cn.com/problems/longest-harmonious-subsequence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-harmonious-subsequence/solution/gong-shui-san-xie-yi-ti-shuang-jie-hua-d-quuh/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[556. 下一个更大元素 III](https://leetcode.cn/problems/next-greater-element-iii/solution/by-ac_oier-99bj/)** ,难度为 **中等**
4+
5+
Tag : 「模拟」、「双指针」
6+
7+
8+
9+
给你一个正整数 $n,ドル请你找出符合条件的最小整数,其由重新排列 $n$ 中存在的每位数字组成,并且其值大于 $n$。如果不存在这样的正整数,则返回 $-1$ 。
10+
11+
注意 ,返回的整数应当是一个 32ドル$ 位整数 ,如果存在满足题意的答案,但不是 32ドル$ 位整数 ,同样返回 $-1$。
12+
13+
示例 1:
14+
```
15+
输入:n = 12
16+
17+
输出:21
18+
```
19+
示例 2:
20+
```
21+
输入:n = 21
22+
23+
输出:-1
24+
```
25+
26+
提示:
27+
* 1ドル <= n <= 2^{31} - 1$
28+
29+
---
30+
31+
### 模拟
32+
33+
根据题意,只有给定数字 $x$ 本身从高位到低位是「非严格降序」时,我们无法找到一个合法值。
34+
35+
首先,我们可以先对 $x$ 诸位取出,存成 `nums` 数组(数组的首位元素为原数 $x$ 的最低位)。
36+
37+
然后尝试找到第一个能够交换的位置 `idx`(若无法找到,说明不存在合法值,返回 $-1$),即从 $nums[0]$(原始 $x$ 的最低位)开始找,找到第一个「降序」的位置 `idx`,然后从 $[0, idx)$ 范围内找一个比 $nums[idx]$ 要大的最小数与其进行互换,也是从 $nums[0]$ 开始找,找到第一个满足比 $nums[idx]$ 大的数。
38+
39+
当互换完成后,此时比 $x$ 要大这一目标已由第 `idx` 位所保证(后续的排序应该按照从小到大放置),同时互换结束后的 $[0, idx)$ 仍然满足「非严格升序」特性,因此我们可以对其运用「双指针」进行翻转。
40+
41+
代码:
42+
```Java
43+
class Solution {
44+
public int nextGreaterElement(int x) {
45+
List<Integer> nums = new ArrayList<>();
46+
while (x != 0) {
47+
nums.add(x % 10);
48+
x /= 10;
49+
}
50+
int n = nums.size(), idx = -1;
51+
for (int i = 0; i < n - 1 && idx == -1; i++) {
52+
if (nums.get(i + 1) < nums.get(i)) idx = i + 1;
53+
}
54+
if (idx == -1) return -1;
55+
for (int i = 0; i < idx; i++) {
56+
if (nums.get(i) > nums.get(idx)) {
57+
swap(nums, i, idx);
58+
break;
59+
}
60+
}
61+
for (int l = 0, r = idx - 1; l < r; l++, r--) swap(nums, l, r);
62+
long ans = 0;
63+
for (int i = n - 1; i >= 0; i--) ans = ans * 10 + nums.get(i);
64+
return ans > Integer.MAX_VALUE ? -1 : (int)ans;
65+
}
66+
void swap(List<Integer> nums, int a, int b) {
67+
int c = nums.get(a);
68+
nums.set(a, nums.get(b));
69+
nums.set(b, c);
70+
}
71+
}
72+
```
73+
* 时间复杂度:$O(\log{n})$
74+
* 空间复杂度:$O(\log{n})$
75+
76+
---
77+
78+
### 最后
79+
80+
这是我们「刷穿 LeetCode」系列文章的第 `No.556` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
81+
82+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
83+
84+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
85+
86+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
87+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /