当前位置:网站首页>JS解力扣每日一题(十二)——556. 下一个更大元素 III(2022-7-3)
JS解力扣每日一题(十二)——556. 下一个更大元素 III(2022-7-3)
2022-07-05 18:40:00 【rjlmylove_zyw】
题目
给你一个正整数 n ,请你找出符合条件的最小整数,其由重新排列 n 中存在的每位数字组成,并且其值大于 n 。如果不存在这样的正整数,则返回 -1 。
注意 ,返回的整数应当是一个 32 位整数 ,如果存在满足题意的答案,但不是 32 位整数 ,同样返回 -1 。
示例1
输入:n = 12
输出:21
我的答案
var nextGreaterElement = function(n) {
const nums = String(n).split('').map((num) => + num)
let i = nums.length - 1
let j = i
const stack = []
for (; i >= 0; i --) {
if (stack.length && nums[i] < nums[stack[stack.length - 1]]) {
while (stack.length && nums[i] < nums[stack[stack.length - 1]]) {
j = stack.pop()
}
break
}
stack.push(i)
}
if (i < 0) {
return -1
}
[nums[i], nums[j]] = [nums[j], nums[i]]
const ans = +(nums.slice(0, i + 1).join('') + nums.slice(i + 1).sort((a, b) => a - b).join(''))
return ans > 2147483647 ? -1 : ans
};
边栏推荐
- Benefits of automated testing
- Analysis of postman core functions - parameterization and test report
- 解决 contents have differences only in line separators
- Is it safe to make fund fixed investment on access letter?
- cf:B. Almost Ternary Matrix【对称 + 找规律 + 构造 + 我是构造垃圾】
- 案例分享|金融业数据运营运维一体化建设
- Icml2022 | partial and asymmetric comparative learning of out of distribution detection in long tail recognition
- 公司破产后,黑石们来了
- Startup and shutdown of CDB instances
- Is it complicated to open an account? Is online account opening safe?
猜你喜欢

Chinese postman? Really powerful!

Summary of six points of MySQL optimization

Rse2020/ cloud detection: accurate cloud detection of high-resolution remote sensing images based on weak supervision and deep learning

技术分享 | 接口测试价值与体系

Case sharing | integrated construction of data operation and maintenance in the financial industry

2022最新中高级Android面试题目,【原理+实战+视频+源码】

Mysql database indexing tutorial (super detailed)

You can have both fish and bear's paw! Sky wing cloud elastic bare metal is attractive!

解决 contents have differences only in line separators

Solutions contents have differences only in line separators
随机推荐
Linear table - abstract data type
Oracle date format conversion to_ date,to_ char,to_ Timestamp mutual conversion
2022 the most complete Tencent background automation testing and continuous deployment practice in the whole network [10000 words]
2022年5月腾讯云开发者社区视频月度榜单公布
Overview of video self supervised learning
ICML2022 | 长尾识别中分布外检测的部分和非对称对比学习
彻底理解为什么网络 I/O 会被阻塞?
常见时间复杂度
7-1 linked list is also simple fina
Find in MySQL_ in_ Detailed explanation of set() function usage
AI表现越差,获得奖金越高?纽约大学博士拿出百万重金,悬赏让大模型表现差劲的任务
潘多拉 IOT 开发板学习(HAL 库)—— 实验8 定时器中断实验(学习笔记)
The main thread anr exception is caused by too many binder development threads
The era of Web3.0 is coming. See how Tianyi cloud storage resources revitalize the system to enable new infrastructure (Part 2)
Talking about fake demand from takeout order
Various pits of vs2017 QT
Common time complexity
技术分享 | 接口测试价值与体系
集合处理的利器
中文版Postman?功能真心强大!