当前位置:网站首页>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
};
边栏推荐
- R语言可视化散点图(scatter plot)图、为图中的部分数据点添加标签、始终显示所有标签,即使它们有太多重叠、ggrepel包来帮忙
- The easycvr authorization expiration page cannot be logged in. How to solve it?
- 小程序 修改样式 ( placeholder、checkbox的样式)
- Tupu software digital twin | visual management system based on BIM Technology
- 7-2 保持链表有序
- android中常见的面试题,2022金九银十Android大厂面试题来袭
- Optimization of middle alignment of loading style of device player in easycvr electronic map
- [HCIA cloud] [1] definition of cloud computing, what is cloud computing, architecture and technical description of cloud computing, Huawei cloud computing products, and description of Huawei memory DD
- Linear table - abstract data type
- 鱼和熊掌可以兼得!天翼云弹性裸金属一招鲜!
猜你喜欢

【历史上的今天】7 月 5 日:Google 之母出生;同一天诞生的两位图灵奖先驱

公司破产后,黑石们来了

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

Oracle 中文排序 Oracle 中文字段排序

鱼和熊掌可以兼得!天翼云弹性裸金属一招鲜!

Oracle Chinese sorting Oracle Chinese field sorting

如何快速进阶自动化测试?听听这3位BAT大厂测试工程师的切身感想....

基于FPGA的超声波测距
![Cf:b. almost Terry matrix [symmetry + finding rules + structure + I am structural garbage]](/img/5d/06229ff7cfa144dbcb60ae43d5c435.png)
Cf:b. almost Terry matrix [symmetry + finding rules + structure + I am structural garbage]

How to quickly advance automated testing? Listen to the personal feelings of the three bat test engineers
随机推荐
Blue sky drawing bed Apple quick instructions
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
华律网牵手观测云,上线系统全链路可观测平台
5. Data access - entityframework integration
决策树与随机森林
1亿单身男女撑起一个IPO,估值130亿
max31865模块RTD测温注意事项
2022全网最全的腾讯后台自动化测试与持续部署实践【万字长文】
Case sharing | integrated construction of data operation and maintenance in the financial industry
Reptile 01 basic principles of reptile
如何写出好代码 - 防御式编程
Shang Silicon Valley Shang preferred project tutorial release
紧固件行业供应商绩效考核繁琐?选对工具才能轻松逆袭!
跨境支付平台 XTransfer 的低代码实践:如何与其他中台融合是核心
Word查找红色文字 Word查找颜色字体 Word查找突出格式文本
深入底层C源码讲透Redis核心设计原理
Quickly generate IPA package
Mathematical modeling of oil pipeline layout MATLAB, mathematical model of oil pipeline layout
Tianyi cloud understands enterprise level data security in this way
What is text mining? "Suggested collection"