1. 题目
从 1 开始,移除所有包含数字 9 的所有整数,例如 9,19,29,……
这样就获得了一个新的整数数列:1,2,3,4,5,6,7,8,10,11,……
给定正整数 n,请你返回新数列中第 n 个数字是多少。
1 是新数列中的第一个数字。
样例 1:
输入: 9
输出: 10
注释 :n 不会超过 9 x 10^8。
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/remove-9
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2. 解题
- 答案就是 n 对应于9进制的数
class Solution { //C++
public:
int newInteger(int n) {
vector<int> nums;
while(n)
{
nums.push_back(n%9);
n /= 9;
}
int ans = 0;
for(int i = nums.size()-1; i >= 0; --i)
ans = ans * 10 + nums[i];
return ans;
}
};
class Solution: # py3
def newInteger(self, n: int) -> int:
num = []
while n:
num.append(n%9)
n //= 9
ans = 0
for i in range(len(num)-1,-1,-1):
ans = ans*10+num[i]
return ans
我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!