当前位置:网站首页>[556. Next larger element III]
[556. Next larger element III]
2022-07-03 13:41:00 【[email protected]】
source : Power button (LeetCode)
describe :
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
Example 2:
Input :n = 21
Output :-1
Tips :
1 <= n <= 231 - 1
Method 1 : Next spread
hold n Convert to string ( A character array ), So this problem is actually solving the character array Next spread , Return when there is no next spread −1.
Code :
class Solution {
public:
int nextGreaterElement(int n) {
auto nums = to_string(n);
int i = (int) nums.length() - 2;
while (i >= 0 && nums[i] >= nums[i + 1]) {
i--;
}
if (i < 0) {
return -1;
}
int j = nums.size() - 1;
while (j >= 0 && nums[i] >= nums[j]) {
j--;
}
swap(nums[i], nums[j]);
reverse(nums.begin() + i + 1, nums.end());
long ans = stol(nums);
return ans > INT_MAX ? -1 : ans;
}
};
Execution time :0 ms, In all C++ Defeated in submission 100.00% Users of
Memory consumption :5.8 MB, In all C++ Defeated in submission 57.91% Users of
Complexity analysis
Time complexity : O(logn).
Spatial complexity :O O(logn).
Method 2 : mathematics
Do not convert to character array , How to use O(1) Space complexity to solve this problem ?
If not required 64 Bit integer ?
Similar method I , We from n Start , Constantly compare the size of the lowest digit and the second lowest digit , If the next lowest digit is not lower than the lowest digit , Then remove the lowest digit , Continue to cycle . At the end of the cycle n It corresponds to the subscript of method one i, namely nums Before i + 1 Characters .
For method one, the subscript j The same is true for the calculation of .
Code :
class Solution {
public:
int nextGreaterElement(int n) {
int x = n, cnt = 1;
for (; x >= 10 && x / 10 % 10 >= x % 10; x /= 10) {
++cnt;
}
x /= 10;
if (x == 0) {
return -1;
}
int targetDigit = x % 10;
int x2 = n, cnt2 = 0;
for (; x2 % 10 <= targetDigit; x2 /= 10) {
++cnt2;
}
x += x2 % 10 - targetDigit; // hold x2 % 10 Switch to targetDigit On
for (int i = 0; i < cnt; ++i, n /= 10) {
// reverse n Last cnt Spell the numbers to x after
int d = i != cnt2 ? n % 10 : targetDigit;
if (x > INT_MAX / 10 || x == INT_MAX / 10 && d > 7) {
return -1;
}
x = x * 10 + d;
}
return x;
}
};
Execution time :0 ms, In all C++ Defeated in submission 100.00% Users of
Memory consumption :5.7 MB, In all C++ Defeated in submission 94.76% Users of
Complexity analysis
Time complexity : O(logn).
Spatial complexity : O(1). We just need a constant space to hold a few variables .
author:LeetCode-Solution
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/184/202207031310265002.html
边栏推荐
- Multi table query of MySQL - multi table relationship and related exercises
- R language uses the data function to obtain the sample datasets available in the current R environment: obtain all the sample datasets in the datasets package, obtain the datasets of all packages, and
- [today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
- Flutter dynamic | fair 2.5.0 new version features
- Kivy tutorial how to load kV file design interface by string (tutorial includes source code)
- MySQL
- JS convert pseudo array to array
- Father and basketball
- 8皇后问题
- Stack application (balancer)
猜你喜欢

Flutter dynamic | fair 2.5.0 new version features

MySQL_ JDBC

File uploading and email sending

Kivy tutorial how to automatically load kV files

Flutter dynamic | fair 2.5.0 new version features

Internet of things completion -- (stm32f407 connects to cloud platform detection data)

人身变声器的原理

使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
![[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion](/img/3b/28327bbf5eb19254f03500a41e2adb.jpg)
[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion

MySQL installation, uninstallation, initial password setting and general commands of Linux
随机推荐
[redis] cache warm-up, cache avalanche and cache breakdown
Static linked list (subscript of array instead of pointer)
When updating mysql, the condition is a query
刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
When we are doing flow batch integration, what are we doing?
【被动收入如何挣个一百万】
Flink SQL knows why (XV): changed the source code and realized a batch lookup join (with source code attached)
Golang — 命令行工具cobra
Kivy教程之 盒子布局 BoxLayout将子项排列在垂直或水平框中(教程含源码)
已解决(机器学习中查看数据信息报错)AttributeError: target_names
MyCms 自媒体商城 v3.4.1 发布,使用手册更新
Detailed explanation of multithreading
(first) the most complete way to become God of Flink SQL in history (full text 180000 words, 138 cases, 42 pictures)
Setting up remote links to MySQL on Linux
MySQL_ JDBC
71 articles on Flink practice and principle analysis (necessary for interview)
Swiftui development experience: the five most powerful principles that a programmer needs to master
父亲和篮球
PowerPoint tutorial, how to save a presentation as a video in PowerPoint?
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线