leetcode 258. Add Digits(数论)
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.
这道题是求一个数的数根。
数根有一个同余性质:一个数与它的数根对(b-1)同余(b是进制数)。
举个简单的例子就明白了:
123=1*100+2*10+3
=1*(99+1)+2*(9+1)+3
=(99+2*9)+(1+2+3)
前面一项能被9整除,后面的一项就是各个位上数的和。对1+2+3后得到的数,还是可以这么拆分,一直下去直到数根。
所以一个数与它的数根对(b-1)同余(b是进制数)。
对于本题,我们就利用这个性质求数根,因为数根是一位数,而且数根%9和num%9结果一样的,所以我们就直接num%9,但是我们这里求出来的数不是数根,数根是[0,9],而%9求出的是[1,8],所以我们加一个小的处理技巧:先减1,模了以后再加1.
class Solution {
public:
int addDigits(int num) {
return +(num-)%;
}
};
leetcode 258. Add Digits(数论)的更多相关文章
- Leetcode 258 Add Digits数论
class Solution { public: int addDigits(int num) { ) return num; == ? : num % ; } }; 就是数位根!
- 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(数字相加,数字根)
翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定sum = 38,这个过程就像是:3 + 8 = 11.1 + 1 = 2.由于2仅仅有一位数.所以返回它. ...
- [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 ...
- (easy)LeetCode 258.Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- 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(easy)
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
随机推荐
- 读取Excel中的数据到DataSet
读取Excel中的数据到DataSet 1.引用命名空间 using System.Data.OleDb; 2.输入Excel文件,输出DataSet public DataSet ExecleDs( ...
- 移动端实用的meta标签
直接上代码,代码自有颜如玉 代码自有黄金屋啊 <meta http-equiv="Content-Type" content="text/html; charset ...
- CAShapeLayer实现圆形进度条效果
一.CAShapeLayer简单介绍: .CAShapeLayer继承至CALayer,能够使用CALayer的全部属性值 2.CAShapeLayer须要与贝塞尔曲线配合使用才有意义 3.使用CAS ...
- 红米4A手机刷开发版rom并且获取root权限
1 bl解锁 Critical partition flashing is not allowed就是因为没有bl解锁. 注册小米账号并且关联手机. 下载bl解锁工具 http://www.miui. ...
- php自定义函数: 遍历文件夹及其子文件夹
function traverse_folder($path = '.') { $current_dir = opendir($path); while(($file = readdir($curre ...
- Python读属性文件
# coding:utf-8 class Properties: def __init__(self, file_name): self.file_name = file_name self.prop ...
- - symfony/icu v1.2.0 requires lib-icu >=4.4 -> the requested linked library icu has the wrong version installed or is missing from your system, ma
$ composer install Loading composer repositories with package information Installing dependencies (i ...
- C调用Lua中的函数解析table
Passing Tables to Lua Functions A use case that happens often is the passing of tables to and from L ...
- LeetCode:删除排序数组中的重复项||【80】
LeetCode:删除排序数组中的重复项||[80] 题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原 ...
- fields_for
1 一对多 Using Strong Parameters With Fields For & Nested Forms in Rails 4 http://www.rubyexperimen ...