258.LeetCode Add Digits(easy)[数学问题 求一个数的树根]
来源:互联网 发布:wdcp数据库备份在哪里 编辑:程序博客网 时间:2024/05/22 17:33
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?
Hint:
- A naive implementation of the above process is trivial. Could you come up with other methods?
- What are all the possible results?
- How do they occur, periodically or randomly?
- You may find this Wikipedia article useful.
参考维基百科上的https://en.wikipedia.org/wiki/Digital_root
这是一个求一个数的树根的问题,通过观察找规律可以发现一个数的树根就是这个数(num-1)%9+1的结果
class Solution {public: int addDigits(int num) { return (num-1)%9+1; }};
0 0
- 258.LeetCode Add Digits(easy)[数学问题 求一个数的树根]
- Leetcode----258.Add Digits(easy)
- 【leetcode】Add Digits[easy]
- 【Leetcode】Add Digits(easy)
- 【leetcode】【Easy】【258. Add Digits】【Math】
- Leetcode 258. Add Digits (Easy) (cpp)
- LeetCode Add Digits 数学
- 258. Add Digits #Easy
- 258. Add Digits (Easy)
- <LeetCode><Easy>258Add Digits
- LeetCode #258 - Add Digits -Easy
- leetcode 258[easy]---Add Digits
- Leetcode#258. Add Digits (数字根)
- leetcode:数学:Add Digits(258)
- 258. Add Digits [easy] (Python)
- 菜鸟写给菜鸟的 ——LeetCode解题笔记 Easy-题目2:258. Add Digits
- 【LeetCode】258.Add Digits
- LeetCode 258. Add Digits
- dom
- android 获取手机通讯录联系人,模拟通讯录显示
- 使用Android studio创建apk
- C++笔记2
- OkHTTp简单封装
- 258.LeetCode Add Digits(easy)[数学问题 求一个数的树根]
- 10得阶乘
- 两个一位数相乘
- 浏览器缓存知识小结及应用
- 树莓派 debian 创建root账户 切换账户 并且远程登录
- TCP/IP详解学习笔记(6)-UDP协议
- 利用栈ADT写了一个简单的四则混合运算
- Objective-C初学: 对象的初始化
- Android开发:什么是IBinder