[LeetCode][JavaScript]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 = 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)时间复杂度。

贪心,每读一位数都做求Digit Root的操作,最后的结果是正确的。

 1 /**
 2  * @param {number} num
 3  * @return {number}
 4  */
 5 var addDigits = function(num) {
 6     var str = num.toString(), res = 0, tmp1, tmp2;
 7     for(var i = 0; i < str.length; i++){
 8         res = parseInt(str[i]) + res;
 9         if(res >= 10){
10             tmp1 = parseInt(res / 10);
11             tmp2 = res % 10;
12             res = tmp1 + tmp2;
13         }
14     }
15     return res;
16 };

最佳答案参照wiki : https://en.wikipedia.org/wiki/Digital_root

好几种写法都可以,这里列了2种。

1 /**
2  * @param {number} num
3  * @return {number}
4  */
5 var addDigits = function(num) {
6     return num === 0 ? 0 : num - 9 * Math.floor((num - 1) / 9);
7 };
1 /**
2  * @param {number} num
3  * @return {number}
4  */
5 var addDigits = function(num) {
6     return 1 + (num - 1) % 9;
7 };
时间: 2024-10-23 02:17:21

[LeetCode][JavaScript]Add Digits的相关文章

LeetCode:Add Digits - 非负整数各位相加

1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits 3.题目内容 英文:Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. 中文:有一个非负整数num,重复这样的操作:对该数字的各位数字求和,对这个和的各位数字再求和--直到最后得到一个仅1位的数字

[LeetCode][JavaScript]Add Two Numbers

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) + (

【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] 258. 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

[LeetCode]64. 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

[LeetCode][JavaScript]Add and Search Word - Data structure design

Add and Search Word - Data structure design Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or 

[LeetCode][JavaScript]Add Binary

https://leetcode.com/problems/add-binary/ Add Binary Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 长整型二进制加法. 1 /** 2 * @param {string} a 3 * @param {string} b 4 *

Java [Leetcode 258]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. 解题思路: 假设输入的数字是一个5位数字num,则num的各位

leetcode 258. 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. 这道题是求一个数的数根. 数根有一个同余性质:一个数与它的数根对(b-1)