当前位置:网站首页>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
};
边栏推荐
- MySQL优化六个点的总结
- 视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备
- 华律网牵手观测云,上线系统全链路可观测平台
- 公司破产后,黑石们来了
- golang通过指针for...range实现切片中元素的值的更改
- 解决 contents have differences only in line separators
- R language Visual scatter plot graph, add labels to some data points in the graph, and always display all labels, even if they have too much overlap. Ggrep package helps
- Take a look at semaphore, the current limiting tool provided by JUC
- Thoroughly understand why network i/o is blocked?
- Postman核心功能解析 —— 参数化和测试报告
猜你喜欢

尚硅谷尚优选项目教程发布

基于FPGA的超声波测距

Why can't Bi software do correlation analysis? Take you to analyze

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

Oracle日期格式转换 to_date,to_char,to_timetamp 相互转换

AI open2022 | overview of recommendation systems based on heterogeneous information networks: concepts, methods, applications and resources

How to quickly advance automated testing? Listen to the personal feelings of the three bat test engineers

How much does the mlperf list weigh when AI is named?

一朵云开启智慧交通新未来

Technology sharing | interface testing value and system
随机推荐
华律网牵手观测云,上线系统全链路可观测平台
RPC protocol details
Powerful tool for collection processing
Quickly generate IPA package
Reptile 01 basic principles of reptile
android中常见的面试题,2022金九银十Android大厂面试题来袭
Find in MySQL_ in_ Detailed explanation of set() function usage
How to automatically install pythn third-party libraries
Thoroughly understand why network i/o is blocked?
视频自监督学习综述
Isprs2022/ cloud detection: cloud detection with boundary nets
2022 Alibaba Android advanced interview questions sharing, 2022 Alibaba hand Taobao Android interview questions
企业级数据安全,天翼云是这样理解的
cf:B. Almost Ternary Matrix【对称 + 找规律 + 构造 + 我是构造垃圾】
公司破产后,黑石们来了
解决 contents have differences only in line separators
The easycvr authorization expiration page cannot be logged in. How to solve it?
R语言使用lubridate包处理日期和时间数据实战
Linear table - abstract data type
XML basic knowledge concept