当前位置:网站首页>[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
边栏推荐
- Road construction issues
- MySQL constraints
- MyCms 自媒体商城 v3.4.1 发布,使用手册更新
- Unity embeddedbrowser browser plug-in event communication
- Setting up remote links to MySQL on Linux
- Flutter动态化 | Fair 2.5.0 新版本特性
- Several common optimization methods matlab principle and depth analysis
- 使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
- 双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
- Flink SQL knows why (XIV): the way to optimize the performance of dimension table join (Part 1) with source code
猜你喜欢
Detailed explanation of multithreading
道路建设问题
CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team
Today's sleep quality record 77 points
Mycms we media mall v3.4.1 release, user manual update
The principle of human voice transformer
Servlet
When updating mysql, the condition is a query
KEIL5出现中文字体乱码的解决方法
MySQL constraints
随机推荐
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
Libuv Library - Design Overview (Chinese version)
18W word Flink SQL God Road manual, born in the sky
Unity embeddedbrowser browser plug-in event communication
Anan's doubts
The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can
Road construction issues
8皇后问题
mysql更新时条件为一查询
Resolved (error in viewing data information in machine learning) attributeerror: target_ names
json序列化时案例总结
Flink SQL knows why (XI): weight removal is not only count distinct, but also powerful duplication
全面发展数字经济主航道 和数集团积极推动UTONMOS数藏市场
Unity EmbeddedBrowser浏览器插件事件通讯
File uploading and email sending
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
Servlet
IBEM mathematical formula detection data set
Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
The reasons why there are so many programming languages in programming internal skills