算法系列(9) LeetCode256
2017-11-28 09:49
381 查看
Given a non-negative integer
result has only one digit.
For example:
Given
only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
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
2has
only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
/** * @param {number} num * @return {number} */ var addDigits = function(num) { while(num.toString().length>1){ var sum=0; while(num!=0){ sum += num%10; num = ~~(num/10); } num = sum; } return num; };
相关文章推荐
- 算法系列(16)Leetcode 387 First Unique Character in a String
- 算法系列(17) Leetcode 104. Maximum Depth of Binary Tree
- 算法系列(18) Leetcode 496. Next Greater Element I
- 算法系列(14) Leetcode 492. Construct the Rectangle
- 算法系列(11)LeetCode136 Single Number
- 算法练习专题——LeetCode系列之 ThreeSum
- 算法系列(19)Leetcode 344. Reverse String
- 算法系列(15)LeetCode 171
- 算法练习专题——LeetCode系列之 Two Sum
- 算法系列(10)LeetCode520 Detect Capital
- 算法系列(12)LeetCode292 Nim Game
- 算法系列(3)leetcode27
- 算法系列(4) leetcode26
- 算法系列(5) leetcode350
- 算法系列(13) Leetcode 169 Majority Element
- 算法系列(20) Leetcode 575. Distribute Candies
- 算法系列(7)leetcode 206
- 白话经典算法系列之一 冒泡排序的三种实现
- Leetcode 算法题10
- Leetcode算法学习日志-737 Sentence Similarity II