当前位置:网站首页>[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);
}
}
边栏推荐
- To sort out messy header files, I use include what you use
- Device interface analysis of the adapter of I2C subsystem (I2C dev.c file analysis)
- 【测试开发】软件测试——基础篇
- ble HCI 流控机制
- wuzhicms代码审计
- Ble HCI flow control mechanism
- [template] [Luogu p4630] duathlon Triathlon (round square tree)
- 图像检索(image retrieval)
- Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
- The Block:USDD增长势头强劲
猜你喜欢

如何进行MDM的产品测试
![[test development] software testing - Basics](/img/43/514016f270574fe711e0e15b581022.png)
[test development] software testing - Basics
![[HCIA continuous update] overview of WLAN workflow](/img/0a/b3986307589a9f7379fe1dd707b9f8.png)
[HCIA continuous update] overview of WLAN workflow

Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt

What if Kaili can't input Chinese???

利用win10计划任务程序定时自动运行jar包

Electronic pet dog - what is the internal structure?

居家打工年入800多万,一共五份全职工作,他还有时间打游戏

CocosCreator事件派发使用

就在今天丨汇丰4位专家齐聚,共讨银行核心系统改造、迁移、重构难题
随机推荐
补能的争议路线:快充会走向大一统吗?
NFT liquidity market security issues occur frequently - Analysis of the black incident of NFT trading platform quixotic
To sort out messy header files, I use include what you use
Interpretation of data security governance capability evaluation framework 2.0, the fourth batch of DSG evaluation collection
就在今天丨汇丰4位专家齐聚,共讨银行核心系统改造、迁移、重构难题
为啥有些线上演唱会总是怪怪的?
RecastNavigation 之 Recast
离线、开源版的 Notion—— 笔记软件Anytype 综合评测
To sort out messy header files, I use include what you use
VB cannot access database stocks
Developers, MySQL column finish, help you easily from installation to entry
regular expression
Set the transparent hidden taskbar and full screen display of the form
Is it safe for CITIC Securities to open an account online? Is the account opening fee charged
创业两年,一家小VC的自我反思
The 18th IET AC / DC transmission International Conference (acdc2022) was successfully held online
简单易用的地图可视化
超标量处理器设计 姚永斌 第6章 指令解码 摘录
Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
【Unity UGUI】ScrollRect 动态缩放格子大小,自动定位到中间的格子