[Swift]LeetCode258. 各位相加 | Add Digits

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

Example:

Input: 38
Output: 2
Explanation: 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 loop/recursion in O(1) runtime?

给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。

示例:

输入: 38
输出: 2
解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2。 由于 2 是一位数,所以返回 2。

进阶:
你可以不使用循环或者递归,且在 O(1) 时间复杂度内解决这个问题吗?



20ms

1 class Solution {
2     func addDigits(_ num: Int) -> Int {
3         return (num - 1)%9 + 1
4     }
5 }


24ms

 1 class Solution {
 2     func addDigits(_ num: Int) -> Int {
 3         var num = num
 4
 5         while num >= 10 {
 6             var x = 0
 7             while num > 0 {
 8                 x += num % 10
 9                 num /= 10
10             }
11             num = x
12         }
13
14         return num
15     }
16 }


24ms

 1 class Solution {
 2
 3     func addDigits(_ num: Int) -> Int {
 4         guard num > 9 else { return num }
 5
 6         var digits = [Int]()
 7         var number = num
 8
 9         while true {
10             digits.removeAll()
11
12             while number > 0 {
13                 digits.append(number % 10)
14                 number /= 10
15             }
16
17             if digits.count > 1 {
18                 number = digits.reduce(0, +)
19             } else {
20                 number = digits.first ?? 0
21                 break
22             }
23         }
24
25         return number
26     }
27 }

原文地址:https://www.cnblogs.com/strengthen/p/9751064.html

时间: 2024-10-10 18:17:26

[Swift]LeetCode258. 各位相加 | 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位的数字

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比他小

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 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 pro

[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 & leetcode_258 Add Digits & 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

【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

[LeetCode] Add Digits (a New question added)

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