233. Number of Digit One【H】【33】【再来一遍】

本文介绍了一个算法问题:计算从0到n所有整数中数字1出现的总次数。通过逐位计算的方法,解决了该问题,并给出了Python实现代码。


Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.

For example:
Given n = 13,
Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.

Show Hint 

  1. Subscribe to see which companies asked this question


按位算,先算个位数的1 有多少个,再算十位数的,依次往上

写了好多遍啊。。。。


class Solution(object):
    def countDigitOne(self, n):
        if n < 1:
            return 0

        b = 0
        t = 1
        res = 0

        while t <= n:
            b = n / (t*10)

            tres = b * t

            temp = (n / t)%10
            if temp == 10:
                temp = 1

            if temp == 1:
                tres += n % t + 1

            elif temp > 1:
                tres += t

            res += tres

            #print t,b,tres

            t *= 10

        return  int(res)


7-376 Equivalent Passwords (等效密码) 分数 10 作者 ICPC-2014 单位 贵州工程应用技术学院 Yesterday you arrived at the hotel, and you kept all your valuable stuff in your room’s safe. Unfortunately, you forgot the password. But you have a very long list of passwords (each password is at most 5 digits), and you are sure that your password is one of them. 昨天你到达酒店,把所有贵重物品都放在房间的保险箱里。不幸的是,你忘了密码。但是你有一个很长的密码列表(每个密码最多是5位数字),而且你要确定你的密码就是其中之一。 The safe will consider some passwords equivalent. Two passwords A and B are considered equivalent, if they are of the same length, and |A[i] - B[i]| is the same for all possible values of i, where X[i] is the i-th digit of X and |Y| is the absolute value of Y. 保险柜会考虑一些等效的密码。两个密码A和B的长度相同,|A[i] - B[i]|对于i的所有可能值都相同,其中X[i]是X的第 i 位,|Y|是Y的绝对值。 You will go through the list of passwords in the given order. For each password, you will do the following: 您将按照给定的顺序遍历密码列表。对于每个密码,您将执行以下操作: 1.If the same password or any of its equivalent passwords were typed before, skip this password. 2.Otherwise, type this password into the safe. 3.If it’s the correct password (or any of its equivalent passwords), the safe will open and you will stop any further processing. 如果之前输入了相同的密码或任何等效的密码,请跳过此密码。 否则,请在保险箱中键入此密码。 如果它是正确的密码(或任何与其相当的密码),保险箱将打开,您将停止任何进一步的处理。 Now given the list of all passwords, you would like to know, in the worst case scenario, what is the maximum number of passwords you will have to type? 现在给出了所有密码的列表,您想知道,在最坏的情况下,您必须键入的最大密码数量是多少? Note In the first test case: 在第一个测试用例中: all passwords are equivalent to each other. This means that the first password will open the safe for sure. 所有密码彼此相等。这意味着第一个密码将肯定打开保险箱。 In the second test case: 在第二个测试用例中: If the first password is the correct one, you will type 1 password. If the second password is the correct one, you will type 2 passwords. If the third password is the correct one, you will type 2 passwords (because the second password is equivalent to the third one). If the fourth password is the correct one, you will type 1 password (because the first password is equivalent to the fourth one). 如果第一个密码是正确的,您将键入1个密码。 如果第二个密码是正确的,您将键入2个密码。 如果第三个密码是正确的,您将键入2个密码(因为第二个密码是相当于第三个)。 如果第四个密码是正确的,您将键入1个密码(因为第一个密码是相当于第四个)。 In the third test case: 在第三个测试用例中: If the first password is the correct one, you will type 1 password. If the second password is the correct one, you will type 1 password (because the first password is equivalent to the second one). If the third password is the correct one, you will type 2 passwords. Even though the third password is equivalent to the second password, the second password was skipped, and therefore you should type the third password. 如果第一个密码是正确的,你将输入一个密码。 如果第二个密码是正确的,您将键入1个密码(因为第一个密码等于第二个密码)。 如果第三个密码是正确的,你将输入2个密码。尽管第三个密码相当于第二个密码,但第二个密码被跳过,因此应该跳过输入第三个密码。 ###** Input:** Your program will be tested on one or more test cases. The first line of the input will be a single integer T(1 ≤ T ≤ 50) representing the number of test cases. 您的程序将在一个或多个测试用例上进行测试。输入的第一行是一个整数T(1≤T≤50)表示测试用例的数量。 Followed by T test cases. Each test case starts with a line will containing an integer N (1 ≤ N ≤ 100,000) representing the number of passwords, followed by N lines, each one will contain a non-empty string of at most 5 digits (from ‘0’ to ‘9’), representing a password (might contain leading zeros). 接下来是T测试用例。每个测试用例开始的一行将包含一个整数N(1≤N≤100,000),表示密码的数量,然后是N行,每个行将包含一个不超过5位的非空字符串(从‘0’到‘9’),表示一个密码(可能包含前导零)。 Output: For each test case print a single line containing “Case n: ” (without quotes) where n is the test case number (starting from 1) followed by a space then the maximum number of passwords you will have to type. 对于每个测试用例,打印包含“case n: ”(不带引号)的单行代码,其中n是测试用例号(从1开始)后面跟着空格,然后是你需要输入的最大密码数。 Sample Input: 3 3 000 111 222 4 1111 123 214 2222 3 43434 54545 45454 Sample Output: Case 1: 1 Case 2: 2 Case 3: 2 代码长度限制 16 KB 时间限制 10000 ms 内存限制 64 MB 栈限制 8192 KB全部翻译一遍
08-22
翻译成中文,只做翻译: 9. Conflict Resolution A conflict occurs when a Multicast DNS responder has a unique record for which it is currently authoritative, and it receives a Multicast DNS response message containing a record with the same name, rrtype and rrclass, but inconsistent rdata. What may be considered inconsistent is context sensitive, except that resource records with identical rdata are never considered inconsistent, even if they originate from different hosts. This is to permit use of proxies and other fault-tolerance mechanisms that may cause more than one responder to be capable of issuing identical answers on the network. A common example of a resource record type that is intended to be unique, not shared between hosts, is the address record that maps a host's name to its IP address. Should a host witness another host announce an address record with the same name but a different IP address, then that is considered inconsistent, and that address record is considered to be in conflict. Whenever a Multicast DNS responder receives any Multicast DNS response (solicited or otherwise) containing a conflicting resource record in any of the Resource Record Sections, the Multicast DNS responder MUST immediately reset its conflicted unique record to probing state, and go through the startup steps described above in Section 8, "Probing and Announcing on Startup". The protocol used in the Probing phase will determine a winner and a loser, and the loser MUST cease using the name, and reconfigure. It is very important that any host receiving a resource record that conflicts with one of its own MUST take action as described above. In the case of two hosts using the same host name, where one has been configured to require a unique host name and the other has not, the one that has not been configured to require a unique host name will not perceive any conflict, and will not take any action. By reverting to Probing state, the host that desires a unique host name will go through the necessary steps to ensure that a unique host name is obtained. The recommended course of action after probing and failing is as follows: 1. Programmatically change the resource record name in an attempt to find a new name that is unique. This could be done by adding some further identifying information (e.g., the model name of the hardware) if it is not already present in the name, or appending the digit "2" to the name, or incrementing a number at the end of the name if one is already present. 2. Probe again, and repeat as necessary until a unique name is found. 3. Once an available unique name has been determined, by probing without receiving any conflicting response, record this newly chosen name in persistent storage so that the device will use the same name the next time it is power-cycled. 4. Display a message to the user or operator informing them of the name change. For example: The name "Bob's Music" is in use by another music server on the network. Your music collection has been renamed to "Bob's Music (2)". If you want to change this name, use [describe appropriate menu item or preference dialog here]. The details of how the user or operator is informed of the new name depends on context. A desktop computer with a screen might put up a dialog box. A headless server in the closet may write a message to a log file, or use whatever mechanism (email, SNMP trap, etc.) it uses to inform the administrator of error conditions. On the other hand, a headless server in the closet may not inform the user at all -- if the user cares, they will notice the name has changed, and connect to the server in the usual way (e.g., via web browser) to configure a new name. 5. After one minute of probing, if the Multicast DNS responder has been unable to find any unused name, it should log an error message to inform the user or operator of this fact. This situation should never occur in normal operation. The only situations that would cause this to happen would be either a deliberate denial-of-service attack, or some kind of very obscure hardware or software bug that acts like a deliberate denial-of-service attack. These considerations apply to address records (i.e., host names) and to all resource records where uniqueness (or maintenance of some other defined constraint) is desired.
最新发布
10-18
这段代码的主要功能是生成一个数字图像的热力图(heatmap),并对像素值进行归一化处理。下面是逐行解析: --- ### 详细解读 #### 函数定义 ```python def get_heatmap(digit): ``` - 定义了一个名为 `get_heatmap` 的函数,接收单个参数 `digit`,它应该是某个数字化后的图像数据。 --- #### 注释部分 ```plaintext """Computes the heatmap of the out of the digit.""" ``` - 这段注释说明了函数的目的:计算输入数字的热力图。 --- #### 绝对值与重塑操作 ```python digit = np.abs(np.reshape(digit, newshape=(28, 28))) ``` - 首先通过 `np.abs()` 对 `digit` 中的所有元素取绝对值。 - 接着使用 `np.reshape()` 将其从一维向量转换成 `(28, 28)` 形状的二维矩阵,这通常是为了适配 MNIST 数据集中手写数字图片的标准尺寸(28x28 像素)。 --- #### 归一化处理 ```python return (digit - np.min(digit)) / (np.max(digit) - np.min(digit)) ``` - **`(digit - np.min(digit))`** 表示将所有像素值减去最小值。 - **`(np.max(digit) - np.min(digit))`** 表示求最大值与最小值之差(范围跨度)。 - 整体公式实现了线性映射变换,使得新的像素值落在 `[0, 1]` 区间内,这就是所谓的“归一化”过程。 --- ### 功能总结 这个函数的作用可以分为两步: 1. 将输入的一维向量恢复为标准的手写数字图像(如 MNIST 格式下的 28x28 矩阵)。 2. 对恢复后的图像进行归一化处理,使其数值分布在 [0, 1] 之间,便于可视化或进一步分析。 这种技术常见于深度学习领域,尤其是用于展示卷积神经网络中间层激活情况的热力图。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值