当前位置:网站首页>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
};
边栏推荐
- 2022全网最全的腾讯后台自动化测试与持续部署实践【万字长文】
- Use QT to traverse JSON documents and search sub objects
- Windows Oracle 开启远程连接 Windows Server Oracle 开启远程连接
- Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
- ROS安装报错 sudo: rosdep:找不到命令
- Icml2022 | partial and asymmetric comparative learning of out of distribution detection in long tail recognition
- Talking about fake demand from takeout order
- You can have both fish and bear's paw! Sky wing cloud elastic bare metal is attractive!
- How to choose the most formal and safe external futures platform?
- Chinese postman? Really powerful!
猜你喜欢

Solutions contents have differences only in line separators

Reading notes of Clickhouse principle analysis and Application Practice (5)

Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?

5. 数据访问 - EntityFramework集成

Word查找红色文字 Word查找颜色字体 Word查找突出格式文本

ICML2022 | 长尾识别中分布外检测的部分和非对称对比学习

基于can总线的A2L文件解析(3)

Benefits of automated testing

自动化测试的好处

Technology sharing | interface testing value and system
随机推荐
7-2 保持链表有序
A cloud opens a new future of smart transportation
AI表现越差,获得奖金越高?纽约大学博士拿出百万重金,悬赏让大模型表现差劲的任务
Use file and directory properties and properties
Windows Oracle 开启远程连接 Windows Server Oracle 开启远程连接
MySQL数据库索引教程(超详细)
Rse2020/ cloud detection: accurate cloud detection of high-resolution remote sensing images based on weak supervision and deep learning
图扑软件数字孪生智慧风电系统
Isprs2020/ cloud detection: transferring deep learning models for cloud detection between landsat-8 and proba-v
2022 Alibaba Android advanced interview questions sharing, 2022 Alibaba hand Taobao Android interview questions
一朵云开启智慧交通新未来
Low code practice of xtransfer, a cross-border payment platform: how to integrate with other medium-sized platforms is the core
案例分享|金融业数据运营运维一体化建设
Solutions contents have differences only in line separators
线性表——抽象数据类型
技术分享 | 接口测试价值与体系
Use QT to traverse JSON documents and search sub objects
公司破产后,黑石们来了
企业数字化转型之路,从这里开始
The road of enterprise digital transformation starts from here