本题使用动态规划法思想。
因为需要一步一步地替换相对应的数字的,如果直接模拟,那么就需要大量插入和删除操作,最快也需要lg(n)的效率,但是最后数列就会变得非常长,这样最后计算结果遍历一次也会超时的。故此使用数据结构加速替换操作,并不是好办法。
这就使用动态规划法从后往前替换,相当于路径压缩了,一步直接把数字替换成最终结果的数字。
也要记录好每个数字最终替换成多少个数位,以便正确计算结果。
可以画树来模拟一下替换操作,那么从叶子节点往根节点替换数字,把所有的路径都直接压缩起来。...
分类:
其他好文 时间:
2014-09-09 13:11:48
阅读次数:
259
题目原文:
Determine whether an integer is a palindrome. Do this without extra space.
click to show spoilers.
Some hints:
Could negative integers be palindromes? (ie, -1)
If you are thinking of conv...
分类:
其他好文 时间:
2014-09-09 13:04:18
阅读次数:
156
[leetcode]Letter Combinations of a Phone Number...
分类:
其他好文 时间:
2014-09-09 12:49:58
阅读次数:
224
题意:给你一个数,计算他的阶乘有多少位。
算阶乘再看位数必超时。1
想每次%10 。一开始用int 果然有精度问题。于是double水了。600+ms
后来想到可以 log 10 () 。于是900+ms。
总觉得不对。应该有公式。看discuss 后终于知道了。
#include
#include
#include
#include
#inclu...
分类:
其他好文 时间:
2014-09-09 12:37:18
阅读次数:
165
if(@ARGV!=2){
die "Usage: $0 sample_file sample_number\n";
}
my($sample_file, $sample_number) = @ARGV;
open my $FILE, "
my @content = ;
my $all_number = @content;
#print "|$all_number|\n";...
分类:
其他好文 时间:
2014-09-09 12:33:48
阅读次数:
351
Ahypervisorcalculates the total number of processor cycles (the number of processor cycles of one or more physical processors) in a first length of ti...
分类:
其他好文 时间:
2014-09-09 11:47:48
阅读次数:
320
Generalized Palindromic NumberTime Limit: 2 Seconds Memory Limit: 65536 KBA number that will be the same when it is written forwards or backwards i...
分类:
其他好文 时间:
2014-09-09 11:19:48
阅读次数:
351
ListList中的内容并不一定只有一种类型,一个List中可能既有number,又有string,还可能有子ListList用[]表示List的基本方法有:append,sort等要从一个List中删除某元素,可以使用del函数(它不是List的方法)#!/usr/bin/pythonmylist...
分类:
编程语言 时间:
2014-09-09 10:58:18
阅读次数:
290
1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #i...
分类:
其他好文 时间:
2014-09-09 10:56:58
阅读次数:
340