非重复数字的计数 [英] Count of non-repeating digits

查看:94
本文介绍了非重复数字的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个数字,返回具有非重复数字的数字的计数,直到该数字从1开始.

Given a number, return the count of numbers having non-repeating digits till that number starting from 1.

测试案例1:

样本输入

7

样本输出

7

测试案例2:

样本输入

3456

样本输出

2562


我将编写程序,但是我不知道问题陈述在问什么,因为样本输入测试用例使我感到困惑.


I will write the program but I don't know what is problem statement is asking as sample input test cases are confusing me.

推荐答案

问题 不明确,因为有两种解释重复数字"的合理方法.

The question is ambiguous, since there's two reasonable ways of interpreting "repeated digit".

  • 首先,如果一个数字连续两次或更多次具有相同的数字,则该数字具有重复的数字".例如12334具有重复的数字(3).

  • First, a number has a "repeated digit" if it has the same digit twice or more in succession. For example 12334 has a repeated digit (3).

第二,如果相同的数字出现两次,则该数字具有重复的数字.因此,1231在第一个意义上不会有重复的数字,但在这个意义上会重复(1个重复).

Second, a number has a repeated digit if the same digit appears twice. So 1231 would not have a repeated digit in the first sense, but would in the this sense (1 repeats).

通过检查它们给出的测试用例,我们可以找到正确的含义.

We can find which meaning is correct by checking the test cases they give.

def nonrep1(n):
    return sum(all(c != d for c, d in zip(str(i), str(i)[1:])) for i in xrange(1, n+1))

def nonrep2(n):
    return sum(all(str(i).count(d) < 2 for d in '0123456789') for i in xrange(1, n+1))

assert nonrep1(7) == 7
assert nonrep1(3456) == 2562

assert nonrep2(7) == 7
assert nonrep2(3456) == 2562

最终断言失败,因此不可重复"的第一个含义是问题解决者想要的含义.

The final assertion fails, so the first meaning of "non repeated" is the one intended by the problem-setter.

我已经编写了一些简短的程序版本,所以现在您了解了问题所在后,仍然可以编写自己的代码.

I have written slightly silly terse versions of the programs so you can still write your own code now that you understand what the problem is asking.

这篇关于非重复数字的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆