当前位置:网站首页>【每日一题】556. 下一个更大元素 III
【每日一题】556. 下一个更大元素 III
2022-07-04 16:03:00 【王六六的IT日常】
简单构造模拟题.
- 先对 x 的每一位取出,存成 nums 数组(数组的首位元素为原数 x 的最低位)
- 尝试找到第一个能够交换的位置 idx(若无法找到,说明不存在合法值,返回 −1)
- 从 nums[0](原始 x 的最低位)开始找,找到第一个「降序」的位置 idx
- 然后从 [0,idx) 范围内找一个比 nums[idx] 要大的最小数与其进行互换
- 既从 nums[0] 开始找,找到第一个满足比 nums[idx] 大的数。
- 当互换完成后,此时比 x 要大这一目标已由第 idx 位所保证(后续的排序应该按照从小到大放置),同时互换结束后的 [0,idx) 仍然满足「非严格升序」特性,可以对其运用「双指针」进行翻转
class Solution {
public int nextGreaterElement(int x) {
List<Integer> nums = new ArrayList<>();
//取出x的每一位
while (x != 0) {
nums.add(x % 10);
x /= 10;
}
int n = nums.size(), idx = -1;
for (int i = 0; i < n - 1 && idx == -1; i++) {
if (nums.get(i + 1) < nums.get(i)) {
idx = i + 1;
}
}
if (idx == -1) return -1;
for (int i = 0; i < idx; i++) {
if (nums.get(i) > nums.get(idx)) {
swap(nums, i, idx);
break;
}
}
for (int l = 0, r = idx - 1; l < r; l++, r--) {
swap(nums, l, r);
}
long ans = 0;
for (int i = n - 1; i >= 0; i--) {
ans = ans * 10 + nums.get(i);
}
return ans > Integer.MAX_VALUE ? -1 : (int)ans;
}
//交换函数
public void swap(List<Integer> nums, int a, int b) {
int c = nums.get(a);
nums.set(a, nums.get(b));
nums.set(b, c);
}
}
class Solution {
public int nextGreaterElement(int n) {
List<Integer> list = new ArrayList<>();
//取出n的每一位
while (n > 0) {
list.add(n % 10);
n /= 10;
}
boolean flag = true;
for (int i = 1; i < list.size(); i++) {
//找到第一个高位数字小于低位数字的位置,此时[0, 高位数字)为升序
if (list.get(i - 1) > list.get(i)) {
//从低位向高位遍历,找到第一个大于高位的数字,交换位置
for (int j = 0; j < i; j++) {
if (list.get(j) > list.get(i)) {
swap(list, j, i);
break;
}
}
//此时[0, 高位)为升序,翻转此区间,并修改标记
for (int lo = 0, hi = i - 1; lo < hi; lo++, hi--) {
swap(list, lo, hi);
}
flag = false;
break;
}
}
//上轮循环未找到符合要求的数字,则n没有满足题意的数字,返回-1
if (flag) {
return -1;
}
long sum = 0;
//结果大于2^31 - 1,返回 -1
for (int i = list.size() - 1; i >= 0; i--) {
sum = sum * 10 + list.get(i);
if (sum > Integer.MAX_VALUE) {
return -1;
}
}
return (int) sum;
}
//交换链表中的元素
void swap(List<Integer> list, int lo, int hi) {
int tmp = list.get(lo);
list.set(lo, list.get(hi));
list.set(hi, tmp);
}
}
边栏推荐
- 【系统分析师之路】第七章 复盘系统设计(结构化开发方法)
- Solve the El input input box For number number input problem, this method can also be used to replace the problem of removing the arrow after type= "number"
- 电子宠物小狗-内部结构是什么?
- Zhijieyun - meta universe comprehensive solution service provider
- 你应该懂些CI/CD
- The Ministry of human resources and Social Security announced the new construction occupation
- 金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
- Summary of tx.origin security issues
- Vscode modification indentation failed, indent four spaces as soon as it is saved
- How to implement a delay queue?
猜你喜欢
PingCode 性能测试之负载测试实践
解读数据安全治理能力评估框架2.0,第四批DSG评估征集中
一加10 Pro和iPhone 13怎么选?
Electronic pet dog - what is the internal structure?
【HCIA持续更新】广域网技术
Easy to use map visualization
CocosCreator事件派发使用
Solution of commercial supply chain coordination system in the mineral industry: build a digital intelligent supply chain platform to ensure the safe supply of mineral resources
【测试开发】软件测试——基础篇
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
随机推荐
Dynamic programming stock problem comparison
《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下
长城证券开户安全吗 证券账户怎么开通
tx.origin安全问题总结
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
Win32 API 访问路由的加密网页
PingCode 性能测试之负载测试实践
数学分析_笔记_第7章:多元函数的微分学
【系统分析师之路】第七章 复盘系统设计(结构化开发方法)
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
[HCIA continuous update] WLAN overview and basic concepts
To sort out messy header files, I use include what you use
Is it safe for Great Wall Securities to open an account? How to open a securities account
Is it safe for CITIC Securities to open an account online? Is the account opening fee charged
R language plot visualization: plot visualization of multiple variable violin plot in R with plot
【HCIA持续更新】网络管理与运维
To sort out messy header files, I use include what you use
超标量处理器设计 姚永斌 第5章 指令集体系 摘录
Is it safe for Bank of China Securities to open an account online?
Difference between redis' memory obsolescence strategy and expiration deletion strategy