-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode第738题.cpp
More file actions
38 lines (38 loc) · 929 Bytes
/
Leetcode第738题.cpp
File metadata and controls
38 lines (38 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution {
public:
int monotoneIncreasingDigits(int n) {
if(n<10){
return n;
}
if(n==1000000000){
return 999999999;
}
vector<int> bitNums;
int temp = n;
while (temp>0)
{
bitNums.push_back(temp % 10);
temp /= 10;
}//将n的各个位数上的数字放入到bitNums数组中
reverse(bitNums.begin(),bitNums.end());
int flag=bitNums.size();
for(int i=bitNums.size()-1;i>0;--i)
{
if(bitNums[i-1]>bitNums[i]){
bitNums[i-1]--;
flag=i;
}
}
for(;flag<bitNums.size();++flag)
{
bitNums[flag]=9;
}
int ans=0,pos=1;
reverse(bitNums.begin(),bitNums.end());
for(int i=0;i<bitNums.size();++i,pos*=10)
{
ans+=pos*bitNums[i];
}
return ans;
}
};