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的更多相关文章

  1. [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 ...

  2. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  3. 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++> 给出排序好的 ...

  4. [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% ...

  5. [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 ...

  6. Leetcode练习题Remove Element

    Leetcode练习题Remove Element Question: Given an array nums and a value val, remove all instances of tha ...

  7. [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 ...

  8. [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 ...

  9. 【leetcode】Remove Duplicates from Sorted Array

    题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...

随机推荐

  1. 【06】Firebug记录Javascript日志

    Firebug记录Javascript日志 你可以使用Firebug来生成日志. 这有助于我们调试web页面并发现页面的错误. 在Firefox浏览器中执行以下代码: <!DOCTYPE HTM ...

  2. js总结(四):关于高性能

    参考<高性能网站建设进阶指南> 不仅仅关注页面加载时间,也要关注下页面操作时的相应速度.页面操作是我们写程序中 实实在在需要的 1.使用局部变量 任何非局部变量,在函数中使用次数超过一次时 ...

  3. Struts2的Action配置的各项默认值

    1 如果没有为action指定class,默认是ActionSupport 2 如果没有为action指定method,默认执行action中的execute()方法 3 如果没有指定result的n ...

  4. Java高级程序员面试题

    1.你认为项目中最重要的过程是那些? 分析.设计阶段  尽量找出进度的优先级 2.如果给你一个4-6人的team,怎么分配? 挑选一技术过硬的人作为我的替补.其它人平均分配任务,每周进行全面的任务分配 ...

  5. zoj 2829 Beautiful Number

    Beautiful Number Time Limit: 2 Seconds      Memory Limit: 65536 KB Mike is very lucky, as he has two ...

  6. zoj 2932 The Seven Percent Solution

    The Seven Percent Solution Time Limit: 2 Seconds      Memory Limit: 65536 KB Uniform Resource Identi ...

  7. [codeforces724E]Goods transportation

    [codeforces724E]Goods transportation 试题描述 There are n cities located along the one-way road. Cities ...

  8. PHP目录文件遍历

    <meta charset="utf-8"><?php //遍历文件中的所有文件名称 foreach(glob("phpmyadmin/*") ...

  9. Xcode waring: no rule to process file *** 警告提示

    在编译程序的时候,Xcode给出了警告:warning: no rule to process file *** 类似的警告, 解决方法: 在[build Phases] -> [Compile ...

  10. Django学习之 - 基础部分

    学习记录参考: 讲师博客:http://www.cnblogs.com/wupeiqi/articles/5433893.html 老男孩博客:http://oldboy.blog.51cto.com ...