zl程序教程

您现在的位置是:首页 >  其他

当前栏目

[LeetCode] Add Digits

LeetCode add digits
2023-09-14 09:01:04 时间
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 o

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 loop/recursion in O(1) runtime?

Hint:


A naive implementation of the above process is trivial. Could you come up with other methods? What are all the possible results? How do they occur, periodically or randomly? You may find this Wikipedia article useful.

思路1:一直相加直到num变为个位数。
思路2:套用维基公式。


LeetCode - #2 Add Two Numbers 我们社区从本期开始会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。微博:@故胤道长)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。
LeetCode 2:两数相加 Add Two Numbers ​给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
# Leetcode 67:Add Binary(二进制求和) Leetcode 67:Add Binary(二进制求和) (python、java) Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. 给定两个二进制字符串,返回他们的和(用二进制表示)。
Constrained Output Embeddings for End-to-End Code-Switching Speech Recognition with Only Monolingual Data 立即下载