LeetCode No.258 Add Digits

LeetCode  Algorithum

# title solution difficulty
258  Add Digits  java  easy
       

NO.258 (2015.10.26 16:09:00)

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.

给定一个非负整数num,反复添加所有位数字知道只剩下一位数。

比如:给定一个非负整数 num = 38,过程就像这样:3 + 8 = 11, 1 + 1 = 2,从两位一直到一位数,返回最后相加得到的这个数字。

 1 public class Solution {
 2     public int addDigits(int num) {
 3         int result = 0;//定义最终相加结果
 4         String number = num + "";
 5         if(num >= 10) {//如果大于10,则进行相加,否则直接返回
 6             result = getResult(number);
 7             while(result >= 10) {//返回结果如果大于等于10,则继续进行相加
 8                 result = getResult(result + "");
 9             }
10         } else {
11             return num;
12         }
13         return result;
14     }
15
16     //循环遍历返回总数
17     public int getResult(String number) {
18         int result = 0;
19         for(int i = 0; i < number.length(); i++) {
20             result += Integer.parseInt(number.charAt(i) + "");
21         }
22         return result;
23     }
24 }
时间: 2024-11-10 01:10:01

LeetCode No.258 Add Digits的相关文章

【一天一道LeetCode】#258. Add Digits

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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

【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】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. 参考这一篇wiki百科 https://en.wikipedia.

Leetcode刷题记录[python]——258 Add Digits

一.前言 做这题有个小收获,关于Digital root的解法,有个极方便的小公式: 二.题258 Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. class Solution(object): def addDigits(self, num): """ :type num: int :rtype: i

LeetCode Javascript解答 258. Add Digits

258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = function(num) { var b = (num-1) % 9 + 1 ; return b; }; //之所以num要-1再+1;是因为特殊情况下:当num是9的倍数时,0+9的数字根和0的数字根不同. 性质说明 1.任何数加9的数字根还是它本身.(特殊情况num=0) 小学学加法的时候我们都明白

258. Add Digits(C++)

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

我也来刷LeetCode——3、Add Digits

唉,做完这道题目后,有种羞愧无比的感觉,果然还是读书太少... 什么意思呢?就像这道题目一样,要求你编程计算出从1加到100的结果.于是你用循环从1累加到100,结果为5050.而大家都知道,从数学上的角度来解答,结果就是(a[1]+a[n])* n / 2. LeetCode上面的这道[Add Digits]题,其实是道数学题.它这个求解的结果,在数学上称为数根. 原题大概意思是这样,给定一个正整数,循环累加它的各位,直到结果为一位数.如 num 是38,计算的过程是 3+8=11,1+1=2

[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

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的各位