当前位置:网站首页>[daily question] 556 Next bigger element III
[daily question] 556 Next bigger element III
2022-07-04 17:48:00 【Wang Liuliu's it daily】
556. Next bigger element III
Medium question
Simple construction simulation problem .
- First pair x Every one of you takes out , Deposit nums Array ( The first element of the array is the original number x The lowest point of )
- Try to find the first place that can be exchanged idx( If you can't find , It indicates that there is no legal value , return −1)
- from nums[0]( original x The lowest point of ) Start looking for , Find the first 「 Descending 」 The location of idx
- And then from [0,idx) Find a ratio in the range nums[idx] Exchange the largest decimal with it
- From nums[0] Start looking for , Find the first satisfaction ratio nums[idx] Large number .
- When the exchange is completed , It's better than x The goal of becoming bigger has been achieved by idx Bit by bit ( Subsequent sorting should be arranged from small to large ), At the same time, after the exchange [0,idx) Still satisfied 「 Not strictly ascending 」 characteristic , Can be applied to it 「 Double pointer 」 Flip
class Solution {
public int nextGreaterElement(int x) {
List<Integer> nums = new ArrayList<>();
// Take out x Every one of them
while (x != 0) {
nums.add(x % 10);
x /= 10;
}
int n = nums.size(), idx = -1;
for (int i = 0; i < n - 1 && idx == -1; i++) {
if (nums.get(i + 1) < nums.get(i)) {
idx = i + 1;
}
}
if (idx == -1) return -1;
for (int i = 0; i < idx; i++) {
if (nums.get(i) > nums.get(idx)) {
swap(nums, i, idx);
break;
}
}
for (int l = 0, r = idx - 1; l < r; l++, r--) {
swap(nums, l, r);
}
long ans = 0;
for (int i = n - 1; i >= 0; i--) {
ans = ans * 10 + nums.get(i);
}
return ans > Integer.MAX_VALUE ? -1 : (int)ans;
}
// Exchange function
public void swap(List<Integer> nums, int a, int b) {
int c = nums.get(a);
nums.set(a, nums.get(b));
nums.set(b, c);
}
}
class Solution {
public int nextGreaterElement(int n) {
List<Integer> list = new ArrayList<>();
// Take out n Every one of them
while (n > 0) {
list.add(n % 10);
n /= 10;
}
boolean flag = true;
for (int i = 1; i < list.size(); i++) {
// Find the position where the first high digit is less than the low digit , here [0, High digit ) In ascending order
if (list.get(i - 1) > list.get(i)) {
// Traverse from low to high , Find the first number greater than the high digit , Swap places
for (int j = 0; j < i; j++) {
if (list.get(j) > list.get(i)) {
swap(list, j, i);
break;
}
}
// here [0, High position ) In ascending order , Flip this section , And modify the tag
for (int lo = 0, hi = i - 1; lo < hi; lo++, hi--) {
swap(list, lo, hi);
}
flag = false;
break;
}
}
// The number that meets the requirements is not found in the last cycle , be n There are no numbers that meet the meaning of the question , return -1
if (flag) {
return -1;
}
long sum = 0;
// The result is greater than 2^31 - 1, return -1
for (int i = list.size() - 1; i >= 0; i--) {
sum = sum * 10 + list.get(i);
if (sum > Integer.MAX_VALUE) {
return -1;
}
}
return (int) sum;
}
// Exchange elements in the linked list
void swap(List<Integer> list, int lo, int hi) {
int tmp = list.get(lo);
list.set(lo, list.get(hi));
list.set(hi, tmp);
}
}
边栏推荐
- 长城证券开户安全吗 证券账户怎么开通
- 利用win10计划任务程序定时自动运行jar包
- Recast of recastnavigation
- 上市公司改名,科学还是玄学?
- [system analyst's road] Chapter 7 double disk system design (structured development method)
- Is it safe for Bank of China Securities to open an account online?
- Hidden corners of coder Edition: five things that developers hate most
- shell脚本的替换功能实现
- Internet addiction changes brain structure: language function is affected, making people unable to speak neatly
- S5PV210芯片I2C适配器驱动分析(i2c-s3c2410.c)
猜你喜欢

La 18e Conférence internationale de l'IET sur le transport d'électricité en courant alternatif et en courant continu (acdc2022) s'est tenue avec succès en ligne.

Recast of recastnavigation

明星开店,退,退,退

超标量处理器设计 姚永斌 第6章 指令解码 摘录

如何进行MDM的产品测试

超大规模数仓集群在大型商业银行的落地实践

Electronic pet dog - what is the internal structure?

Firewall basic transparent mode deployment and dual machine hot standby
《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下

上网成瘾改变大脑结构:语言功能受影响,让人话都说不利索
随机推荐
regular expression
无心剑中译伊丽莎白·毕肖普《一门技艺》
创业两年,一家小VC的自我反思
CANN算子:利用迭代器高效实现Tensor数据切割分块处理
About the pit of firewall opening 8848 when Nacos is started
【HCIA持续更新】WLAN工作流程概述
Superscalar processor design yaoyongbin Chapter 6 instruction decoding excerpt
Ks007 realizes personal blog system based on JSP
Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions
智捷云——元宇宙综合解决方案服务商
The Block:USDD增长势头强劲
使用3DMAX制作一枚手雷
How to choose one plus 10 pro and iPhone 13?
长城证券安全不 证券开户
How to test MDM products
【Unity UGUI】ScrollRect 动态缩放格子大小,自动定位到中间的格子
超大规模数仓集群在大型商业银行的落地实践
利用win10计划任务程序定时自动运行jar包
Two methods of MD5 encryption
The top half and bottom half of the interrupt are introduced and the implementation method (tasklet and work queue)