当前位置:网站首页>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
};
边栏推荐
- What are the cache interfaces of nailing open platform applet API?
- EasyCVR电子地图中设备播放器loading样式的居中对齐优化
- 解决 contents have differences only in line separators
- The era of Web3.0 is coming. See how Tianyi cloud storage resources revitalize the system to enable new infrastructure (Part 2)
- Blue sky drawing bed Apple quick instructions
- 自动化测试的好处
- Rse2020/ cloud detection: accurate cloud detection of high-resolution remote sensing images based on weak supervision and deep learning
- How to write good code defensive programming
- 块编辑器如何选择?印象笔记 Verse、Notion、FlowUs
- 机器学习基础(三)——KNN/朴素贝叶斯/交叉验证/网格搜索
猜你喜欢
【Autosar 十四 启动流程详解】
Technology sharing | interface testing value and system
1亿单身男女撑起一个IPO,估值130亿
Analysis of postman core functions - parameterization and test report
Mysql database indexing tutorial (super detailed)
How to quickly advance automated testing? Listen to the personal feelings of the three bat test engineers
[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
Solutions contents have differences only in line separators
集合处理的利器
Icml2022 | partial and asymmetric comparative learning of out of distribution detection in long tail recognition
随机推荐
机器学习基础(三)——KNN/朴素贝叶斯/交叉验证/网格搜索
Word查找红色文字 Word查找颜色字体 Word查找突出格式文本
2022最新中高级Android面试题目,【原理+实战+视频+源码】
Icml2022 | partial and asymmetric comparative learning of out of distribution detection in long tail recognition
Powerful tool for collection processing
跨境支付平台 XTransfer 的低代码实践:如何与其他中台融合是核心
Applet modification style (placeholder, checkbox style)
图扑软件数字孪生智慧风电系统
SAP feature description
[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
Shang Silicon Valley Shang preferred project tutorial release
国内低代码开发平台靠谱的都有哪些?
Deep copy and shallow copy [interview question 3]
Tupu software digital twin smart wind power system
出海十年:新旧接力,黑马崛起
CF: B. almost Ternary Matrix [symétrie + règles de recherche + Construction + I am Construction Waste]
鱼和熊掌可以兼得!天翼云弹性裸金属一招鲜!
Interprocess communication (IPC): shared memory
EasyCVR电子地图中设备播放器loading样式的居中对齐优化
Startup and shutdown of CDB instances