【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 ...
随机推荐
- Qt绘制文本一
QPainterPath,使用 drawText且设置字体,再使用painter.drawText方式 效果图: void WgtText::paintEvent(QPaintEvent *event ...
- bzoj2055
题解: 似乎是放在费用流里的 然而是有上下界的网络流QAQ 代码: #include<bits/stdc++.h> using namespace std; ; int n,m,x,min ...
- day24-python操作数据库四
#!/usr/bin/env python # -*- coding:utf-8 -*- # @time: 2017/11/23 23:10 # Author: caicai # @File: dem ...
- MATLAB 批量处理图片
function resizephotos(directory, wh, isrecursive, isoverwrite, savetopath, supportFormat) % resizeph ...
- 正则:img的url,width,height 和 a标签的url以及替换
代码:// 内容:$detail['content'] //img的url,width,height $img = array(); $matches = array(); $regeImg = '/ ...
- [Leetcode221]最大面积 Maximal Square
[题目] Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's a ...
- VSTO:使用C#开发Excel、Word【10】
第二部分:.NET中的Office编程本书前两章介绍了Office对象模型和Office PIA. 您还看到如何使用Visual Studio使用VSTO的功能构建文档中的控制台应用程序,加载项和代码 ...
- Cracking The Coding Interview 3.2
//How would you design a stack which, in addition to push and pop, also has a function min which ret ...
- 安装连接mysql8时候遇到的问题以及解决(转)
官网下载mysql8的安装包: https://dev.mysql.com/downloads/ 下一步安装即可. mysql8增加了传说中的安全性校验 遇到的几个问题: 1.natcat连接不上.参 ...
- UBUNTU安装 Rabbitvsc可视化版本控制客户端软件
sudo add-apt-repository ppa:rabbitvcs/ppa sudo apt-get update sudo apt-get install rabbitvcs-core ra ...