【02_258】Add Digits
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.
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.
这道题自己一开始想到模拟法,但是看到O(1)后开始思考,在找规律时分类太细了,所以最终没找到规律。
看题解的时候有两种找规律方法。
- 第一种:(转自http://bookshadow.com/weblog/2015/08/16/leetcode-add-digits/?utm_source=tuicool)
方法II:观察法 根据提示,由于结果只有一位数,因此其可能的数字为0 - 9 使用方法I的代码循环输出0 - 19的运行结果: in out in out
0 0 10 1
1 1 11 2
2 2 12 3
3 3 13 4
4 4 14 5
5 5 15 6
6 6 16 7
7 7 17 8
8 8 18 9
9 9 19 1
可以发现输出与输入的关系为: out = (in - 1) % 9 + 1
这种方法属于小学奥赛思维,直接观察,无法有很好的逻辑,不过的确写出来了。
- 第二种:(转自:http://www.cnblogs.com/wrj2014/p/4981350.html)
假设原来的数字为X,X可以表示为:
1
|
X=fn*Xn+fn-1*Xn-1+...+f1*x1 , Xn= pow (10,n-1); |
对X进行一次操作后得到X‘:
1
|
X’=fn+fn-1+...f1 |
X-X':
X-X' = fn*(Xn - 1) + fn-1*(Xn-1 - 1) + ... f1*(X1 - 1)
= fn*9···99 + fn-1*9···9 + ... f1*0
= 9*( fn*1···11 + fn-1*1···1 + ... f2*1 + f1*0 )
= 9*S (S is a non-negative integer)
每一次都比原来的数少了个9的倍数!
还要考虑一些特殊情况,最终程序:

1 class Solution {
2 public:
3 int addDigits(int num) {
4 /*
5 X=fn*Xn+fn-1*Xn-1+...+f1*x1 , Xn=pow(10,n-1);
6 Every operator make X=> X'=fn+fn-1+...f1
7 X-X' =fn*(Xn - 1)+fn-1*(Xn-1 - 1)+...f1*(X1 - 1)
8 =fn*9···99+fn-1*9···9+..f1*0
9 =9*(fn*1···11+fn-1*1···1+...f2*1+f1*0)
10 =9*S (S is a non-negative integer)
11 => Everytime reduce a number of multiple 9
12 */
13 if(num==0) return 0;
14 int t=num%9;
15 return (t!=0)?t:9;
16 }
17 };

最后是自己交上去的代码:
class Solution {
public:
int addDigits(int num) {
if (num < )
return num;
else {
int i = num % ;
if (i == )
return ;
else
return i;
}
}
};
【02_258】Add Digits的更多相关文章
- 【LeetCode】Add Digits
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 【LeetCode67】 Add Binary
题目描述: 解题思路: 此题的思路简单,下面的代码用StringBuilder更加简单,注意最后的结果要反转过来.[LeetCode415]Add Strings的解法和本题一模一样. java代码: ...
- 【LeetCode445】 Add Two Numbers II★★
题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...
- 【HDU4333】Revolving Digits(扩展KMP+KMP)
Revolving Digits Description One day Silence is interested in revolving the digits of a positive i ...
- 【LeetCode415】Add Strings
题目描述: 解决思路: 此题较简单,和前面[LeetCode67]方法一样. Java代码: public class LeetCode415 { public static void main(St ...
- 【leetcode】Add Two Numbers
题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- 【题解】【链表】【Leetcode】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【leetcode】Add Two Numbers(middle) ☆
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【leetcode】 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
随机推荐
- 依赖注入 IOC
首先,明确下IoC是什么东西: 控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心. 控 ...
- vue 项目中命名方法
命名 命名的方法通常有以下几类: 命名法说明 1).camel命名法,形如thisIsAnApple 2).pascal命名法,形如ThisIsAnApple 3).下划线命名法,形如this_is_ ...
- [LeetCode] 100. Same Tree ☆(两个二叉树是否相同)
描述 解析 根与根比较,左右子树互相递归比较即可. 代码 /** * Definition for a binary tree node. * public class TreeNode { * in ...
- element-ui 表格翻页多选,数据回显
reserve-selection与row-key结合 <el-table :data="pageData" ref="goodsTable" size= ...
- qml 设置tooltip,Customizing ToolTip
Button { id: btn text: "Tip" anchors.horizontalCenter: pa ...
- mysql查看和修改密码策略
8.X版本: #查看密码策略 show variables like '%validate_password.policy%'; show variables like '%validate_pass ...
- py request.post header
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome ...
- 逆袭之旅.DAY08东软实训.多态~
2018年7月4日
- Win10系列:VC++文件选取
在C++/CX的Windows::Storage::Pickers命名空间中定义了一个FileOpenPicker类,使用此类可以新建一个文件打开选取器,并可以通过这个类里面包含的属性和函数选取一个或 ...
- UVa LA 3029 City Game 状态拆分,最大子矩阵O(n2) 难度:2
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...