【02_258】Add Digits

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 };


最后是自己交上去的代码:
 1 class Solution {
 2 public:
 3     int addDigits(int num) {
 4         if (num < 10)
 5             return num;
 6         else {
 7             int i = num % 9;
 8             if (i == 0)
 9                 return 9;
10             else
11                 return i;
12         }
13     }
14 };
				
时间: 2024-10-14 00:45:25

【02_258】Add Digits的相关文章

【Leetcode】Add Digits

题目链接:https://leetcode.com/problems/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 digi

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【leetcode】Add Two Numbers 解析以及拓展

题目: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -&

【LeetCode】Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". public class Solution { public String addBinary(String a, String b) { if(a.equalsIgnoreCase("")||a==null) r

【HackerRank】 Find Digits

Find Digits Problem Statement Given a number you have to print how many digits in that number exactly divides that number. Input format The first line contains T (number of test cases followed by t lines each containing n Constraints 1 <=T <= 15 0 &

【LeetCode】Add Two Numbers 解题报告

[题目] You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -

【LeetCode415】Add Strings

题目描述: 解决思路: 此题较简单,和前面[LeetCode67]方法一样. Java代码: 1 public class LeetCode415 { 2 public static void main(String[] args) { 3 String a="1",b="9"; 4 System.out.println(a+"和"+b+"相加的结果是:"+new Solution().addStrings(a, b)); 5

【leetcode】 Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

LeetCode【2】Add two numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->