当前位置:网站首页>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
};
边栏推荐
- MySQL优化六个点的总结
- Analysis of postman core functions - parameterization and test report
- c期末复习
- You can have both fish and bear's paw! Sky wing cloud elastic bare metal is attractive!
- 爬虫01-爬虫基本原理讲解
- Deep copy and shallow copy [interview question 3]
- Web3.0时代来了,看天翼云存储资源盘活系统如何赋能新基建(下)
- 技术分享 | 常见接口协议解析
- 案例分享|金融业数据运营运维一体化建设
- 国内低代码开发平台靠谱的都有哪些?
猜你喜欢
![[detailed explanation of AUTOSAR 14 startup process]](/img/c4/5b08b43db97d8bb10c7207005f8c0a.png)
[detailed explanation of AUTOSAR 14 startup process]

企业数字化转型之路,从这里开始

集合处理的利器
![[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day

5. 数据访问 - EntityFramework集成

Technology sharing | interface testing value and system

Postman核心功能解析 —— 参数化和测试报告

MySQL数据库索引教程(超详细)
![2022 latest intermediate and advanced Android interview questions, [principle + practice + Video + source code]](/img/c9/f4ab4578029cf043155a5811a64489.png)
2022 latest intermediate and advanced Android interview questions, [principle + practice + Video + source code]

案例分享|金融业数据运营运维一体化建设
随机推荐
常见时间复杂度
Talking about fake demand from takeout order
Mathematical modeling of oil pipeline layout MATLAB, mathematical model of oil pipeline layout
2022 the most complete Tencent background automation testing and continuous deployment practice in the whole network [10000 words]
CDB 实例的启动与关闭
You can have both fish and bear's paw! Sky wing cloud elastic bare metal is attractive!
使用文件和目录属性和属性
Icml2022 | partial and asymmetric comparative learning of out of distribution detection in long tail recognition
图扑软件数字孪生智慧风电系统
Interprocess communication (IPC): shared memory
AI Open2022|基于异质信息网络的推荐系统综述:概念,方法,应用与资源
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
c期末复习
Use file and directory properties and properties
集合处理的利器
Ant group open source trusted privacy computing framework "argot": open and universal
What are the cache interfaces of nailing open platform applet API?
Tupu software digital twin | visual management system based on BIM Technology
2022 Alibaba Android advanced interview questions sharing, 2022 Alibaba hand Taobao Android interview questions
The road of enterprise digital transformation starts from here