题目链接:https://leetcode.com/problems/add-digits/ 题目: num, repeatedly add all its digits until the result has only one digit. For example: num = 38, the process is like:3 + 8 = 11,1 + 1 = 2. Since2 Follow up: Could you do it without any loop/recursion in O(1) runtime?
classSolution {public:intaddDigits(intnum) {while(num /10!=0) {inttemp =0;while(num !=0) { temp+= num %10; num/=10; } num=temp; }returnnum; } }; 分析二:参考https://leetcode.com/discuss/52122/accepted-time-space-line-solution-with-detail-explanations classSolution {public:intadd...
intaddDigits(intnum) {if(num>9&&num%9==0)return9;if(num>9)returnnum%9;returnnum; }
LeetCode 258. Add Digits 程序员木子 香港浸会大学 数据分析与人工智能硕士在读 来自专栏 · LeetCodeDescription Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. Example: Input: 38Output: 2Explanation: The process is like: 3 + 8 = ...
[LeetCode] Add Digits Add Digits Given a non-negative integernum, repeatedly add all its digits until the result has only one digit. For example: Givennum=38, the process is like:3+8=11,1+1=2. Since2has only one digit, return it....
digits数字stored in reverse反向存储 each of每一个nodes节点 3 惊人而又蹩脚的中文翻译 本题主要是类似数据在机器中存储的方式,我们平常所见的数据比如342,在链表中是逆向存储的所以就成了2->4->3这样了,同样5 -> 6 -> 4就是465, 如果这样转换后,我们就会发现342+465=807,在十位上相加是超过10向前进一...
LeetCode 第二弹 —— Add Two Numbers 隔了这么久,突然想起来刷 LeetCode,按着顺序到了第二题 —— Add Two Numbers。 题目: You are given twonon-emptylinked lists representing two non-negative integers. The digits are stored inreverse orderand each of their nodes contain a single digit. Add ...
可以发现输出与输入的关系为: out = (in - 1) % 9 + 1 C++代码: class Solution { public: int addDigits(int num) { return (num-1)%9+1; } };
LeetCode从零刷起 (2. Add Two Numbers) 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. 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....
const digits = getDigist(n) set.add(n)n = 0 digits.forEach(num => { if (!cache[num]) { cache[num] = Math.pow(num, 2) }n += cache[num] }) }return n === 1 } ```## [最长连续序列](https://leetcode.cn/problems/longest-consecutive-sequence)>...