当前位置:网站首页>【每日一题】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);
}
}
边栏推荐
- Is BigDecimal safe to calculate the amount? Look at these five pits~~
- leetcode刷题目录总结
- Web game engine
- MVC模式和三层架构
- Offline and open source version of notation -- comprehensive evaluation of note taking software anytype
- 码农版隐秘的角落:作为开发者最讨厌的5件
- Cocoscreator event dispatch use
- 数学分析_笔记_第7章:多元函数的微分学
- 公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
- R语言plotly可视化:plotly可视化多分类变量小提琴图(multiple variable violin plot in R with plotly)
猜你喜欢
整理混乱的头文件,我用include what you use
To sort out messy header files, I use include what you use
【HCIA持续更新】广域网技术
Talk about seven ways to realize asynchronous programming
[HCIA continuous update] WLAN overview and basic concepts
【Unity UGUI】ScrollRect 动态缩放格子大小,自动定位到中间的格子
我写了一份初学者的学习实践教程!
What if Kaili can't input Chinese???
整理混乱的头文件,我用include what you use
Master the use of auto analyze in data warehouse
随机推荐
Internet addiction changes brain structure: language function is affected, making people unable to speak neatly
Analysis of abnormal frequency of minor GC in container environment
[test development] software testing - Basics
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
[acwing] 58 weeks 4489 Longest subsequence
Ble HCI flow control mechanism
正则表达式
Zebras are recognized as dogs, and the reason for AI's mistakes is found by Stanford
码农版隐秘的角落:作为开发者最讨厌的5件
Wuzhicms code audit
解读数据安全治理能力评估框架2.0,第四批DSG评估征集中
大规模服务异常日志检索
Ks007 realizes personal blog system based on JSP
TP configuring multiple databases
R语言plotly可视化:plotly可视化互相重叠的直方图(historgram)、并在直方图的顶部边缘使用geom_rug函数添加边缘轴须图Marginal rug plots
MVC模式和三层架构
中信证券网上开户安全吗 开户收费吗
VSCode修改缩进不成功,一保存就缩进四个空格
wuzhicms代码审计
Is it safe for Bank of China Securities to open an account online?