leetcode 660. Remove 9
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
.
首先预处理出 i 位数多出来多少,比如n是1位数,那么就多出1,n是两位数那么就多出19。
然后对于题目中的n 比如 n = 109,我们首先判断他在增加后是几位数,109 > 9 , 109 > 100 - 19 ,109 < 1000 - 271 所以n是三位数,那么相应的ans就加上100,然后处理剩下的n(这时的n不是n-100而是n-100 + 19)。
集体看代码吧,这不好描述。。。。
class Solution {
public:
typedef long long ll;
ll sum[] = {};
ll d[] = {};
void init() {
int x = ;
sum [] = ;
d[] = ;
for (int i = ; i <= ; ++i) {
d[i] = sum[i - ] * + (ll)pow(10.0,i-);
sum[i] = sum[i - ] + d[i];
}
}
int newInteger(int n) {
init();
ll x = ,ans = ;
while (n > ) {
ll tmp = n;
x = ;
for (int i = ; i <= ; ++i) {
if (n >= pow(,i) - sum[i]) continue;
else {
x = i;break;
}
}
n -= pow(10.0, x - );
n += sum[x - ];
ans += pow(10.0, x - );
}
if (n == ) ans ++;
return ans + n;
}
};
看到网上也有把10进制转换到9进制去搞的。。http://www.cnblogs.com/pk28/p/7356218.html
leetcode 660. Remove 9的更多相关文章
- [LeetCode] 660. Remove 9 移除9
Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... So now, you will have ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
- [LeetCode] 82. Remove Duplicates from Sorted List II_Medium tag: Linked List
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinctnumbe ...
- Leetcode练习题Remove Element
Leetcode练习题Remove Element Question: Given an array nums and a value val, remove all instances of tha ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 【leetcode】Remove Duplicates from Sorted Array
题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
随机推荐
- Counting Kangaroos is Fun 求最少可见袋鼠数
Description There are n kangaroos with pockets. Each kangaroo has a size (integer number). A kangaro ...
- mysql汉字转拼音函数
-- 创建汉字拼音对照临时表 CREATE TABLE IF NOT EXISTS `t_base_pinyin` ( `pin_yin_` ) CHARACTER SET gbk NOT NULL, ...
- 两行代码快速创建一个iOS主流UI框架
本框架适用于 使用 NavigationController+UITabBarController 的APP 框架QLSNavTab , GitHub地址:https://github.com/qia ...
- Toad Oracle 本地/远程数据库导入/导出 数据库备份
1. Toad进入数据库后,选择 Database ==> Export ===> Export Utility Wizard ,选择export user(按用户导出),选择Toa ...
- CSS属性操作二
9.float属性 基本浮动规则 先来了解一下block元素和inline元素在文档流中的排列方式. block元素通常被现实为独立的一块,独占一行,多个block元素会各自新起一行,默认block元 ...
- poj2553 有向图缩点,强连通分量。
//求这样的sink点:它能达到的点,那个点必能达到他,即(G)={v∈V|任意w∈V:(v→w)推出(w→v)} //我法:tarjan缩点后,遍历点,如果该点到达的点不在同一个强连通中,该点排除, ...
- poj1376 bfs,机器人
开始时候有点怕, 感觉什么也不会,不过静下来思考思考也就想出来了,一个简单的BFS即可,但是由于队列没有重判,一直爆队列(MLE!)下次一定要注意! (bfs第一次到达便最优?) #include&l ...
- 快速让你明白Objective-C的语法(和Java、C++对比)
很多想开发iOS,或者正在开发iOS的程序员以前都做过Java或者C++,当第一次看到Objective-C的代码时都会头疼,Objective-C的代码在语法上和Java, C++有着很大的区别,有 ...
- Sudoku---hdu2676(数独DFS)
http://poj.org/problem?id=2676 递归深搜 #include<stdio.h> #include<string.h> #include<alg ...
- 关于MSSQL的decimal(numeric)、money、float的使用以及区别
decimal(numeric).money.float(real) 都是MSSQL中的浮点类型的数据类型. 按存储的范围进行排序 float(real) decimal(numeric) money ...