A6:Add Digits

题目描述:

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

样例

Given num = 38.

The process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return 2.

解答:

 1 public class Solution {
 2     /**
 3      * @param num a non-negative integer
 4      * @return one digit
 5      */
 6     public int addDigits(int num) {
 7         // Write your code here
 8         while(num / 10 > 0){
 9             int[] tmp = getNum(num);
10             num = 0;
11             for(int i=0; i<tmp.length; i++){
12                 num = num + tmp[i];
13             }
14         }
15        return num;
16     }
17
18     private int[] getNum(int num){
19         int length = Integer.toString(num).length();
20         int[] tmp = new int[length];
21         int i = 0;
22         while (num / 10 > 0){
23
24             tmp[i] = num % 10;
25             num = num / 10 ;
26             i++;
27         }
28         tmp[length-1] = num;
29         return tmp;
30     }
31
32 }
时间: 2024-08-24 00:47:46

A6: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 OJ: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 an

LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number

数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. (Easy) 分析:求n的阶乘中末位0的个数,也就是求n!中因数5的个数(2比5多),简单思路是遍历一遍,对于每个数,以此除以5求其因数5的个数,但会超时. 考虑到一个数n比他小

[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 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. Follow up:Could you do it

leetcode 21_Merge Two Sorted Lists &amp; leetcode_258 Add Digits &amp; leetcode_66plus one

l leetcode 21_Merge Two Sorted Lists 题目:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 解法: #include <iostream> using namespace std; struct ListNode { int

LeetCode4: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 -&

【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 = 11, 1 + 1 = 2.

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

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