当前位置:网站首页>【每日一题】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);
}
}
边栏推荐
- VB cannot access database stocks
- The Block:USDD增长势头强劲
- [template] [Luogu p4630] duathlon Triathlon (round square tree)
- Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
- How to choose one plus 10 pro and iPhone 13?
- 防火墙基础透明模式部署和双机热备
- 居家打工年入800多万,一共五份全职工作,他还有时间打游戏
- Wuzhicms code audit
- Superscalar processor design yaoyongbin Chapter 7 register rename excerpt
- Summary of tx.origin security issues
猜你喜欢

斑马识别成狗,AI犯错的原因被斯坦福找到了丨开源

电子宠物小狗-内部结构是什么?

超大规模数仓集群在大型商业银行的落地实践

RecastNavigation 之 Recast
![[unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid](/img/0d/a8f4424add7785375741bac4f0b802.png)
[unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid

解读数据安全治理能力评估框架2.0,第四批DSG评估征集中

整理混乱的头文件,我用include what you use

数学分析_笔记_第7章:多元函数的微分学

It's too convenient. You can complete the code release and approval by nailing it!

Vb无法访问数据库stocks
随机推荐
新享科技发布小程序UniPro小优 满足客户移动办公场景
解决el-input输入框.number数字输入问题,去掉type=“number“后面箭头问题也可以用这种方法代替
防火墙基础透明模式部署和双机热备
NFT流动性市场安全问题频发—NFT交易平台Quixotic被黑事件分析
[HCIA continuous update] overview of WLAN workflow
MVC模式和三层架构
What are cache penetration, cache breakdown, and cache avalanche
I2C子系统之适配器的设备接口分析(i2c-dev.c文件分析)
Great Wall Securities security does not open a securities account
Superscalar processor design yaoyongbin Chapter 7 register rename excerpt
上网成瘾改变大脑结构:语言功能受影响,让人话都说不利索
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
码农版隐秘的角落:作为开发者最讨厌的5件
Learn more about the basic situation of 2022pmp examination
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
我写了一份初学者的学习实践教程!
Ks007 realizes personal blog system based on JSP
It's too convenient. You can complete the code release and approval by nailing it!
【系统分析师之路】第七章 复盘系统设计(结构化开发方法)
DataKit——真正的统一可观测性 Agent