-
Notifications
You must be signed in to change notification settings - Fork 0
/
660.hpp
45 lines (36 loc) · 914 Bytes
/
660.hpp
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
39
40
41
42
43
44
45
#ifndef LEETCODE_660_HPP
#define LEETCODE_660_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <numeric>
#include <stack>
#include <string>
using namespace std;
/*
Start from integer 1, remove any integer that contains 9 such as 9, 19, 29...
So now, you will have a new integer sequence: 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, ...
Given a positive integer n, you need to return the n-th integer after removing. Note that 1 will be the first integer.
Example 1:
Input: 9
Output: 10
Hint: n will not exceed 9 x 10^8.
*/
class Solution {
public:
int newInteger(int n) {
int result = 0, pos = 1;
while (n >= 9) {
result += n % 9 * pos;
n /= 9;
pos *= 10;
}
result += n % 9 * pos;
return result;
}
};
#endif //LEETCODE_660_HPP