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 ...
随机推荐
- HDU 1166 排兵布阵(线段树单点更新)
题意: 给定n个兵营的士兵初始值, 然后有最多40000个操作: 操作一共有两种, 一个是查询给定[a,b]区间兵营的士兵总和. 另一个是增加/减少指定兵营的士兵数目. 输出每次查询的值. 分析: 线 ...
- MySQL之federated
由于夸服务器查询的限制,federated能够使得所有的表像是在同一台服务器上查询 (show engines-->no-->在my.ini里面添加fedrated) 经过测试,在开启fe ...
- python之tkinter变量设置 2014-4-9
python 可以自己定义变量以及变量类型mystring = StringVar(ticked_yes = BooleanVoption1 = IntVar()volume = DoubleVar( ...
- Java实战及解析 — Maven快速入门
五分钟快速入门 mvn --version mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -Darche ...
- Problem 2125 简单的等式(FZU),,数学题。。。
Problem 2125 简单的等式 Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description 现在有一个等式如下:x^2+ ...
- [Go]程序实体
Go语言中的程序实体包括变量.常量.函数.结构体.接口 1.常见声明变量的方式 package main import ( "flag" "fmt" ) fun ...
- SPOJ GNYR09F 数字上的找规律DP
Problem C SPOJ GNYR09F dp题,dp可能刚刚开始对大家来说比较难,但是静下心来分析还是比较简单的: dp(i ,j ,k)表示前i个数中,当前累积和为j,末尾数字为k的 ...
- POJ 3680: Intervals【最小费用最大流】
题目大意:你有N个开区间,每个区间有个重量wi,你要选择一些区间,使得满足:每个点被不超过K个区间覆盖的前提下,重量最大 思路:感觉是很好想的费用流,把每个区间首尾相连,费用为该区间的重量的相反数(由 ...
- Sencha Touch 2 实现跨域访问
最近要做手机移动App,最后采用HTMML5来做框架用Sencha Touch,在数据交互时遇到了Ajax跨域访问,在Sencha Touch 2中,实现跨域访问可以使用Ext类库提供给我们的类Ext ...
- WebService流行框架CXF
CXF官方网址:http://cxf.apache.org/ CXF官方网址:官网学习地址:http://cxf.apache.org/docs/index.html 官网下载cxf压缩文件: ...