Add Digits

Total Accepted: 49702 Total Submissions: 104483 Difficulty: Easy

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 = 111 + 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:

  1. A naive implementation of the above process is trivial. Could you come up with other methods?
  2. What are all the possible results?
  3. How do they occur, periodically or randomly?
  4. 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的更多相关文章

  1. 【LeetCode】Add Digits

    Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  2. 【LeetCode67】 Add Binary

    题目描述: 解题思路: 此题的思路简单,下面的代码用StringBuilder更加简单,注意最后的结果要反转过来.[LeetCode415]Add Strings的解法和本题一模一样. java代码: ...

  3. 【LeetCode445】 Add Two Numbers II★★

    题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...

  4. 【HDU4333】Revolving Digits(扩展KMP+KMP)

    Revolving Digits   Description One day Silence is interested in revolving the digits of a positive i ...

  5. 【LeetCode415】Add Strings

    题目描述: 解决思路: 此题较简单,和前面[LeetCode67]方法一样. Java代码: public class LeetCode415 { public static void main(St ...

  6. 【leetcode】Add Two Numbers

    题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  7. 【题解】【链表】【Leetcode】Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  8. 【leetcode】Add Two Numbers(middle) ☆

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  9. 【leetcode】 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

随机推荐

  1. Mac重要目录

    App最喜欢的几个目录 Mac和Windows操作系统有一个很大的不同,大部分App是没有安装程序的,一般下载下来就是一个dmg文件,解开之后直接将App拖到应用程序目录下就可以了,所以给人感觉卸载也 ...

  2. PHPCMS V9完全开发介绍

    PHPCMS V9 文件目录结构: 根目录 | – api 接口文件目录 | – caches 缓存文件目录 | – configs 系统配置文件目录 | – caches_* 系统缓存目录 | – ...

  3. kaggle信用卡欺诈看异常检测算法——无监督的方法包括: 基于统计的技术,如BACON *离群检测 多变量异常值检测 基于聚类的技术;监督方法: 神经网络 SVM 逻辑回归

    使用google翻译自:https://software.seek.intel.com/dealing-with-outliers 数据分析中的一项具有挑战性但非常重要的任务是处理异常值.我们通常将异 ...

  4. git报错fatal: I don't handle protocol '​https'处理

    一.背景说明 今天使用在Cygwin中git clone时报fatal: I don't handle protocol '​https',如下: 以为是Cygwin实现的git有点问题没太在意,换去 ...

  5. jackSon注解– @JsonInclude 注解不返回null值字段

    @Data @JsonInclude(JsonInclude.Include.NON_NULL) public class OrderDTO { private String orderId; @Js ...

  6. 【Jmeter基础知识】Jmeter的三种参数化方式

    JMeter的三种参数化方式包括: 1.用户参数 2.函数助手 3.CSV Data Set Config 一.用户参数 位置:添加-前置处理器-用户参数 操作:可添加多个变量或者参数 二.函数助手 ...

  7. laravel中对模型和路由做缓存,提高性能

    模型缓存命令: php think optimize:schema 路由缓存命令: php think optimize:route

  8. centos6.5+python2.7+flask+apache+mod-wsgi部署

    flask部署,使用的是centos6.5,python2.7,版本很重要.基本步骤如下: 一.创建虚拟环境,创建目录把项目拷进去 二.安装mod-wsgi和apache easy_install m ...

  9. java中的方法method

    java中的方法必须存在于类class里,不能独立存在.类是描述具有某种特征的事物,方法则是这类 事物具有的某种功能,通过调用方法可以实现某种特定的功能.方法名一般以小写的动词开头. 例: publi ...

  10. 福大软工1816 · 第三次作业 - 结对项目Salty Fish原型图

    SALTY FISH原型图 LINKS IMPORT to LIST FOCUS TRENDS ANALYSE NIGHT