LeetCode 258 Add Digits(数字相加,数字根)
翻译
给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数。
比如:
给定sum = 38,这个过程就像是:3 + 8 = 11。1 + 1 = 2。由于2仅仅有一位数。所以返回它。
紧接着:
你能够不用循环或递归在O(1)时间内完毕它吗?
原文
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
For example:
Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
分析
事实上我并不会写,循环和递归都不能用……我还能怎么办呐。
然后看了LeetCode上给的提示。看了维基百科的一篇文章:Digital root。
有了这个的话,就非常easy了。详细这个公式的细节,大家自己參看维基吧,由于篇幅过长就不翻译了。
代码
class Solution {
public:
int floor(int x) {
return (x - 1) / 9;
}
int addDigits(int num) {
return num - 9 * floor(num);
}
};
LeetCode 258 Add Digits(数字相加,数字根)的更多相关文章
- LN : leetcode 258 Add Digits
lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...
- [LeetCode] 258. Add Digits 加数字
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- LeetCode 258. Add Digits
Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...
- Java [Leetcode 258]Add Digits
题目描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
- LeetCode 258 Add Digits 解题报告
题目要求 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...
- leetcode 258. Add Digits(数论)
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- (easy)LeetCode 258.Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- 258. Add Digits 数位相加到只剩一位数
[抄题]: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
- LeetCode: 258 Add Digits(easy)
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
随机推荐
- vs输出窗口,显示build的时间
https://stackoverflow.com/questions/82128/displaying-build-times-in-visual-studio Tools... Options.. ...
- rest_framework-认证-总结完结篇
执行过程 APIView() Ruquest() Authentication() OrderView()APIView() def duspatch: self.initial(request) d ...
- [COI2007] [luogu P1823] Patrik 音乐会的等待 解题报告 (单调栈)
题目链接:https://www.luogu.org/problemnew/show/P1823 题目: N个人正在排队进入一个音乐会.人们等得很无聊,于是他们开始转来转去,想在队伍里寻找自己的熟人. ...
- 基于JavaSwing的例子-非连接数据库
项目结构: Constant.java package com.mstf.test; import java.io.Serializable; public class Constant implem ...
- C#中Request.Cookies 和 Response.Cookies 的区别分析
.NET中提供了读写Cookie的多种方法,Request.Cookies 是客户端通过 Cookie 标头形式由客户端传输到服务器的 Cookie:Response.Cookies 在服务器上创建并 ...
- bzoj1612 Usaco08 Jan 牛大赛
水题模拟 建一个图,每两个牛进行比赛就连一条边,然后两遍dfs求出比他弱和比他强的牛,最后如果相加数量等于n,说明他能与全部的牛进行比较,排名确定. #include<bits/stdc++.h ...
- JZOJ5821手机信号
用set维护,(l,r,v),注意边界,保证了两个端点l,r一定有信号站 增加有三种可能,1.直接加(没有影响),2.将原本的一个区间变成两个 3.将原本的一个区间变成三个 删除有三种情况,1.全包含 ...
- md5sum---文件校验和
md5sum命令采用MD5报文摘要算法(128位)计算和检查文件的校验和.一般来说,安装了Linux后,就会有md5sum这个工具,直接在命令行终端直接运行. MD5算法常常被用来验证网络文件传输的完 ...
- Dynamic Rankings(分块)
P2617 Dynamic Rankings 题目描述 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i ...
- springboot框架笔记——springboot提供的自动配置
Springboot基本配置 spring MVC的定制配置需要我们的配置实现一个WebMvcConfigurer接口,如果实在spring环境下需要使用@EnableWebMVC注解,来开启对spr ...