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 ...
随机推荐
- 洛谷2016 战略游戏 (0/1状态的普通树形Dp)
题意: 给出一个树,覆盖树上某一个点的花费为w[i],求树上每一条边至少有一个点覆盖的最小花费. 细节: 1.一条边的两端可以均被覆盖,但是不能存在一条边的两端都不被覆盖. 2.可能存在 分析: 对于 ...
- 【01】Firebug 教程
Firebug 教程 什么是 Firebug? Firebug 是一个开源的web开发工具. 现在浏览器自带firebug了. 安装 Firebug Firebug下载地址: https: ...
- ELK的简单安装使用
ELK ELK是什么? Elasticsearch LogStash Kibana 1,简单的安装 我采用的是本地window环境: 下载的包如下: 首先安装的是jdk8,安装完成之后,设 ...
- hdu 1421经典dp
#include<stdio.h> #include<stdlib.h> #define N 2001 #define inf 0x3fffffff int a[N],dp[N ...
- Spring MVC表单实例
以下内容引用自http://wiki.jikexueyuan.com/project/spring/mvc-framework/spring-mvc-form-handling-example.htm ...
- 虚拟社会(Virtual Society)
虚拟社会(Virtual Society),又称赛博社会(Cyber Society),是指不同网民之间经由计算机.远程通讯终端等技术设备相互连接起来以进行信息的共享.互动与交流,并在其中进行社会交往 ...
- CEF3研究(三)
一.Off-Screen Rendering 脱屏绘制 CEF的脱屏渲染并不创建源生的浏览器窗口,而是CEF提供主应用程序在无效区域和像素buffer里渲染,然后主应用程序通过鼠标.键盘和焦点事件通知 ...
- 【转载】同步和互斥的POSIX支持(互斥锁,条件变量,自旋锁)
上篇文章也蛮好,线程同步之条件变量与互斥锁的结合: http://www.cnblogs.com/charlesblc/p/6143397.html 现在有这篇文章: http://blog.cs ...
- DRBD+Heratbeat+NFS高可用文件共享存储
一.概述 .通过ha-log日志可以看出主释放资源,备接管资源. 来自为知笔记(Wiz)
- 猫猫学iOS 之微博项目实战(2)微博主框架-自己定义导航控制器NavigationController
猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243?viewmode=contents 一:加入导航控制器 上一篇博 ...