当前位置:网站首页>JS solution force deduction daily question (12) - 556 Next larger element III (2022-7-3)
JS solution force deduction daily question (12) - 556 Next larger element III (2022-7-3)
2022-07-05 19:04:00 【rjlmylove_ zyw】
subject
Give you a positive integer n , Please find the smallest integer that meets the conditions , It consists of rearranging n Each number present in the consists of , And its value is greater than n . If there is no such positive integer , Then return to -1 .
Be careful , The returned integer should be a 32 An integer , If there is an answer that satisfies the meaning of the question , But it's not 32 An integer , Also return to -1 .
Example 1
Input :n = 12
Output :21
My answer
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
};
边栏推荐
- Word finds red text word finds color font word finds highlighted formatted text
- [today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day
- 出海十年:新旧接力,黑马崛起
- android中常见的面试题,2022金九银十Android大厂面试题来袭
- 自动化测试的好处
- A cloud opens a new future of smart transportation
- Ant group open source trusted privacy computing framework "argot": open and universal
- Go deep into the underlying C source code and explain the core design principles of redis
- Oracle 中文排序 Oracle 中文字段排序
- 为什么 BI 软件都搞不定关联分析?带你分析分析
猜你喜欢
How to write good code defensive programming
Shang Silicon Valley Shang preferred project tutorial release
面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
中文版Postman?功能真心强大!
决策树与随机森林
达梦数据库udf实现
[detailed explanation of AUTOSAR 14 startup process]
Word查找红色文字 Word查找颜色字体 Word查找突出格式文本
ICML2022 | 长尾识别中分布外检测的部分和非对称对比学习
cf:B. Almost Ternary Matrix【对称 + 找规律 + 构造 + 我是构造垃圾】
随机推荐
China law network joins hands to observe the cloud, and the online system is a full link observable platform
国内低代码开发平台靠谱的都有哪些?
华为让出的高端市场,小米12S靠徕卡能抢到吗?
All you want to know about clothing ERP is here
潘多拉 IOT 开发板学习(HAL 库)—— 实验8 定时器中断实验(学习笔记)
Powerful tool for collection processing
7-1 linked list is also simple fina
Cf:b. almost Terry matrix [symmetry + finding rules + structure + I am structural garbage]
Shang Silicon Valley Shang preferred project tutorial release
2022最新大厂Android面试真题解析,Android开发必会技术
Mysql database indexing tutorial (super detailed)
Idea configuring NPM startup
golang通过指针for...range实现切片中元素的值的更改
7-2 保持链表有序
Take a look at semaphore, the current limiting tool provided by JUC
使用文件和目录属性和属性
中文版Postman?功能真心强大!
视频自监督学习综述
A cloud opens a new future of smart transportation
UDF implementation of Dameng database