剑指offer
整数中1出现的次数(从1到n整数中1出现的次数)
相似的企业真题
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 64M,其他语言128M
热度指数:358934
本题知识点:
数学
算法知识视频讲解
校招时部分企业笔试将禁止编程题跳出页面,为提前适应,练习时请使用在线自测,而非本地IDE。
题目描述
求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1、10、11、12、13因此共出现6次,但是对于后面问题他就没辙了。ACMer希望你们帮帮他,并把问题更加普遍化,可以很快的求出任意非负整数区间中1出现的次数(从1 到 n 中1出现的次数)。
示例1
输入
复制
13
13
返回值
复制
6
6
上一题
下一题
登录
/
注册
我的提交
编辑器加载中...
public class Solution { public int NumberOf1Between1AndN_Solution(int n) { } }
class Solution { public: int NumberOf1Between1AndN_Solution(int n) { } };
# -*- coding:utf-8 -*- class Solution: def NumberOf1Between1AndN_Solution(self, n): # write code here
class Solution { public int NumberOf1Between1AndN_Solution(int n) { // write code here } }
function NumberOf1Between1AndN_Solution(n) { // write code here } module.exports = { NumberOf1Between1AndN_Solution : NumberOf1Between1AndN_Solution };
function NumberOf1Between1AndN_Solution(n) { // write code here }
package main /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param n int整型 * @return int整型 */ func NumberOf1Between1AndN_Solution( n int ) int { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # @param n int整型 # @return int整型 # class Solution def NumberOf1Between1AndN_Solution(n) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param n int整型 * @return int整型 */ def NumberOf1Between1AndN_Solution(n: Int): Int = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param n int整型 * @return int整型 */ fun NumberOf1Between1AndN_Solution(n: Int): Int { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param n int整型 * @return int整型 */ public int NumberOf1Between1AndN_Solution (int n) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param n int整型 * @return int整型 */ export function NumberOf1Between1AndN_Solution(n: number): number { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param n int整型 * @return int整型 */ func NumberOf1Between1AndN_Solution ( _ n: Int) -> Int { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param n int整型 * @return int整型 */ pub fn NumberOf1Between1AndN_Solution(&self, n: i32) -> i32 { // write code here } }
13
6