摘要:
如 1,2,3,...10出现11个数字,1,2,3..11出现了13个数字。现在知道出现d个数字,求n,如果d非法,输出impossible。算法的思想是:计算n = 9, 99, 999...这些长度为1,2,3...的各个数的出现的数字个数,反推d。int calc_from_number_of_digits(int d)
{ if(d < 9) return d; int n = 0; int last_n = 0; int len = 1; while(d > n) { last_n = n; ... 阅读全文
posted @ 2012-11-11 23:36
junfeng_feng
阅读(256)
评论(0)
推荐(0)
摘要:
二分查找查找key最左和最右的位置利用这个特点,可以在O(log n)时间 在排序的数组中,统计一个数出现的次数。如[1,2,2,3],2出现了两次//most_left_or_right: true 表示最左, false表示最右
int binary_search_most_left_or_right(int* array, int size, int key, bool most_left_or_right = true)
{ int low = 0; int high = size - 1; while(low <= high) { int mi... 阅读全文
posted @ 2012-11-11 23:28
junfeng_feng
阅读(156)
评论(0)
推荐(0)
浙公网安备 33010602011771号