Number of Digit One——LeetCode⑩
//原题链接https://leetcode.com/problems/number-of-digit-one/
- 题目描述
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.
Example:
Input: 13
Output: 6
Explanation: Digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.
- 思路分析
1.暴力法:对10求余,判断个位是否为1,然后除10依次判断//会超时
2.优化:总结n每位数的规律,
以n=123为例
(1的个数:count
当前位数上的权重:weight
当前位数上的数字:now
上一位数:last,例如按照从右到左,2的上一位数为3
循环次数:round)
个位数:3属于大于等于1,第十三个循环开始,则count = round + 1 = 12+1;
若为120,即now为小于1,开始第十三个循环,则count = round=12;
十位数及以上位数:以十位数为例,2大于1,则count = round*weight + weight=1*10+10
若为11X,即now为1,则count = round*weight + 1+last=1*10+1+x
若为10X,即now等于0,则count=round*weight=1*10 - 源码附录
class Solution {
public int countDigitOne(int n) {
if(n<1){
return 0;
} int count = 1;
for(int i=0;i<=n;i++){
while(i>0){
if(i%10 == 1){
count ++;
}
i = i / 10;
}
}
return count;
}
}class Solution {
public int countDigitOne(int n) {
if(n<1){
return 0;
} int count = 0;
int round = n;
int weight = 1;
int now = 0 ;
while(round>0){
now = round%10;
round = round/10;
if(now==1){
count = count + (n%weight)+1;
}
else if(now>1){
count = count + weight;
}
count = count + round*weight;
weight = weight*10;
}
return count;
}
}
Number of Digit One——LeetCode⑩的更多相关文章
- [LeetCode] Number of Digit One 数字1的个数
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- [Leetcode] Number of Digit Ones
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- Java for LeetCode 233 Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- (medium)LeetCode 233.Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- 【LeetCode】233. Number of Digit One
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...
- LeetCode 233 Number of Digit One 某一范围内的整数包含1的数量
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- LeetCode OJ 之 Number of Digit One (数字1的个数)
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...
- 233. Number of Digit One
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...
- [Swift]LeetCode233. 数字1的个数 | Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
随机推荐
- Java - JVM及其调优
原文链接:https://blog.csdn.net/qq_27098537/article/details/124436788 一.什么是JVM 用于运行java代码,包括一套字节码指令集.一组寄存 ...
- 大数据之路Week08_day03 (Hive优化)
Hive优化(下面的红色标记是十分重要的,大部分情况是需要开启的) 优化1:hive的抓取策略理论上来说,Hive中的所有sql都需要进行mapreduce,但是hive的抓取策略帮我们省略掉了这个过 ...
- Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
#div_digg { float: right; font-size: 12px; margin: 10px; text-align: center; width: 120px; position: ...
- 【刚度矩阵推导】2d frame 单元
2d frame 单元是x-y平面上的单元,每个节点上有2个平移自由度的和一个转动自由度.局部坐标系下,单元位移向量为: \( u=[ u_1 ,u_2 ,u_3, u_4, u_5, u_6]^{T ...
- 【P5】Verilog搭建流水线MIPS-CPU
课下 Thinking_Log 1.为何不允许直接转发功能部件的输出 直接转发会使一些组合逻辑部件增加新的长短不一的操作延迟,不利于计算设置流水线是时钟频率(保证流水线吞吐量?). 2.jal中将NP ...
- iterm2配置ssh自动登录
iterm2 ssh 演示 cmd + o 打开服务器列表,方向键选择要登录的机器,回车,提示输入密码: option + cmd + f 打开密码管理器,方向键选择密码,回车,即可登录:(这一步通过 ...
- U盘制作、安装Ubuntu系统
制作 ubuntu U盘启动盘 下载Ubuntu镜像 打开 Ubuntu 官网:https://ubuntu.com/download/desktop ,进入页面后,点击右边的[Download]按钮 ...
- go 简单封装数学运算包
前言 我们在编写程序时,经常会遇到一些高精度的数学运算,这时候使用简单的运算符会造成精度的缺失. 这里引用了这个第三方包 https://github.com/shopspring/decimal 做 ...
- [Winform]在Form里显示模态对话框ModalDialog
问题 如何在WinForm的一个Form里面弹出一个模态Dialog? 背景 程序的框架是Winform,只有一个窗口MainForm.MainForm里面是一个TabControl,每个TabPag ...
- 利用AI增强VS Code TypeScript插件:AnyToTS带来编程新体验
Any to TS: VSCode 扩展插件 概述 "Any to TS" 是一个强大的 VSCode 扩展插件,旨在将任何对象转换为 TypeScript 类型或接口.该工具基于 ...