当前位置:网站首页>【每日一题】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);
}
}
边栏推荐
- 整理混乱的头文件,我用include what you use
- 简单易用的地图可视化
- Dynamic programming stock problem comparison
- R语言plotly可视化:plotly可视化多分类变量小提琴图(multiple variable violin plot in R with plotly)
- 雨量预警广播自动化数据平台BWII 型广播预警监测仪
- [test development] software testing - Basics
- 动态规划股票问题对比
- Great Wall Securities security does not open a securities account
- OPPO小布推出预训练大模型OBERT,晋升KgCLUE榜首
- 一文掌握数仓中auto analyze的使用
猜你喜欢

The 18th IET AC / DC transmission International Conference (acdc2022) was successfully held online

7 RSA Cryptosystem
![[Huawei HCIA continuous update] SDN and FVC](/img/02/f86b509fdc515f14a4497090f0d070.png)
[Huawei HCIA continuous update] SDN and FVC

Vscode modification indentation failed, indent four spaces as soon as it is saved

MVC mode and three-tier architecture

Using win10 scheduling task program to automatically run jar package at fixed time

To sort out messy header files, I use include what you use

Learn more about the basic situation of 2022pmp examination

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

解读数据安全治理能力评估框架2.0,第四批DSG评估征集中
随机推荐
动态规划股票问题对比
Pytorch deep learning quick start tutorial
Solution of dealer collaboration system in building materials industry: empowering enterprises to build core competitiveness
中信证券网上开户安全吗 开户收费吗
CocosCreator事件派发使用
What grade does Anxin securities belong to? Is it safe to open an account
What is low code development?
正则表达式
超标量处理器设计 姚永斌 第7章 寄存器重命名 摘录
如何进行MDM的产品测试
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
Win32 API 访问路由的加密网页
Difference between redis' memory obsolescence strategy and expiration deletion strategy
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
CocosCreator事件派發使用
【HCIA持续更新】WLAN概述与基本概念
Using win10 scheduling task program to automatically run jar package at fixed time
kaili不能输入中文怎么办???
开发者,MySQL专栏完更,助你轻松从安装到入门进阶
长城证券安全不 证券开户