当前位置:网站首页>[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);
}
}
边栏推荐
- ARTS_20220628
- Zebras are recognized as dogs, and the reason for AI's mistakes is found by Stanford
- Load test practice of pingcode performance test
- 完美融入 Win11 风格,微软全新 OneDrive 客户端抢先看
- Cocoscreator event dispatch use
- The Block:USDD增长势头强劲
- gatling 之性能测试
- Vscode modification indentation failed, indent four spaces as soon as it is saved
- To sort out messy header files, I use include what you use
- [template] [Luogu p4630] duathlon Triathlon (round square tree)
猜你喜欢
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
Firewall basic transparent mode deployment and dual machine hot standby
解决el-input输入框.number数字输入问题,去掉type=“number“后面箭头问题也可以用这种方法代替
CocosCreator事件派發使用
[test development] software testing - Basics
kaili不能输入中文怎么办???
上市公司改名,科学还是玄学?
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.
居家打工年入800多万,一共五份全职工作,他还有时间打游戏
曾经的“彩电大王”,退市前卖猪肉
随机推荐
【Hot100】31. 下一个排列
curl 命令妙用
Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions
Recast of recastnavigation
R语言plotly可视化:plotly可视化多分类变量小提琴图(multiple variable violin plot in R with plotly)
上网成瘾改变大脑结构:语言功能受影响,让人话都说不利索
Win32 API 访问路由的加密网页
如何进行MDM的产品测试
[proteus simulation] printf debugging output example based on VSM serial port
无心剑中译伊丽莎白·毕肖普《一门技艺》
regular expression
S5PV210芯片I2C适配器驱动分析(i2c-s3c2410.c)
R language plot visualization: plot visualization of multiple variable violin plot in R with plot
防火墙基础透明模式部署和双机热备
Solve the El input input box For number number input problem, this method can also be used to replace the problem of removing the arrow after type= "number"
VSCode修改缩进不成功,一保存就缩进四个空格
Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
超标量处理器设计 姚永斌 第6章 指令解码 摘录
【HCIA持续更新】网络管理与运维
Superscalar processor design yaoyongbin Chapter 7 register rename excerpt