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. Oracle使用expdp/impdp导出导入数据

    这里假设已存在数据库用户,并是计划通过该用户导入导出该用户表空间上的数据.(我们这里假定用户名称为ls) 1.创建逻辑目录(数据库命令,sqlplus中执行) Oracle不能直接指定系统目录让他去读 ...

  2. 整合SpringDataJPA

    JPA:ORM(Object Relational Mapping); 1).编写一个 实体类(bean)和数据表进行映射,并且配置好映射 关系: /** * Created by windMan o ...

  3. xubuntu无法进图形界面问题

    http://www.ubuntugeek.com/fix-for-cant-login-after-upgrading-from-ubuntu-13-04-to-ubuntu-13-10.html ...

  4. MySQL(二) MySQL基本操作

    数据库的基本操作 启动关闭 MySQL 服务 MySQL 安装好后,默认是当 Windows 启动.停止时,MySQL 也自动.停止.不过,用户可以使用 Windows 下的服务管理器或从命令行使用 ...

  5. linux网络操作 配置文件

    网络接口配置文件(网卡信息文件) '/etc/sysconfig/network-srcipts/ifcfg-*(eth0)' (注意区分大小写) DEVICE=eth0 网卡编号 HWADDR=08 ...

  6. Win10系列:UWP界面布局进阶5

    提示框 在Windows应用商店应用程序中可以使用提示框来向用户显示提示信息,例如可以通过对话框来询问用户当前需要执行的操作,还可以通过弹出窗口来显示需要注意的信息.本节将向读者介绍如何在Window ...

  7. Spring Boot 如何极简入门?

    Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运行 ...

  8. Module loader:模块加载器

    <p data-height="265" data-theme-id="0" data-slug-hash="XpqRmq" data ...

  9. RegDataToDataType

    function RegDataToDataType(Value: TRegDataType): Integer; begin case Value of rdString: Result := RE ...

  10. day21 MRO和C3算法

    核能来袭 --MRO和C3算法 1. python的多继承 2.python经典类的MRO 3.python新式类的MRO, C3算法 4.super 是什么鬼? 一.python的多继承 在前面的学 ...