leetcode:数学:Add Digits(258)

原创 2016年08月30日 15:47:43

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.


class Solution {
public:
    int addDigits(int num) {
        int sum = getSum(num);
        while (sum >= 10) {
            sum = getSum(sum);
        }
        return sum;
    }
private:
    int getSum(int num) {
        int sum = 0;
        while (num) {
            sum += num % 10;
            num /= 10;
        }
        return sum;
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

[LeetCode 258] Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit...
  • sbitswc
  • sbitswc
  • 2015年08月25日 13:18
  • 7710

leetcode 258 Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digi...
  • dream_hai
  • dream_hai
  • 2015年10月09日 14:18
  • 124

LeetCode[258] Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit....
  • wrsophia_977
  • wrsophia_977
  • 2016年07月15日 17:30
  • 106

Leetcode 258: Add Digits

题目描述:https://leetcode.com/problems/add-digits/?tab=Description 题目分析:假设所给的数num = ABCDE,那么: 不难得到...
  • u012340794
  • u012340794
  • 2017年03月04日 16:02
  • 103

[Leetcode]#258 Add Digits

//#258 Add Digits //28ms unknown distribution class Solution { public: int addDigits(int num) ...
  • jerseyma
  • jerseyma
  • 2015年09月02日 08:13
  • 190

Leetcode 258 Add Digits

public int addDigits(int num) { int result = num; Integer I_num = new Integer(num); ...
  • xinyuehuixin
  • xinyuehuixin
  • 2016年04月27日 11:38
  • 118

Leetcode Add Digits 258

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit....
  • huaxixidongbeishida
  • huaxixidongbeishida
  • 2016年04月24日 21:34
  • 125

258 Add Digits - LeetCode

Given a non-negative integer num, repeatedly add all its digits until the result has only one digi...
  • tung_hit
  • tung_hit
  • 2015年08月22日 14:04
  • 183

【LeetCode】258 Add Digits

1. 题目描述Given a non-negative integer num, repeatedly add all its digits until the result has only one...
  • hanhanhanhuanghe
  • hanhanhanhuanghe
  • 2015年09月17日 23:18
  • 855

leetcode——258——Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digi...
  • happyxuma1991
  • happyxuma1991
  • 2016年03月22日 22:56
  • 94
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode:数学:Add Digits(258)
举报原因:
原因补充:

(最多只允许输入30个字)