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,重复这样的操作:对该数字的各位数字求和,对这个和的...
Solution 十位数字+个位数字 Answer classSolution{public:intaddDigits(intnum){intde;intre;while(num /10>0) { de = num /10; re = num %10; num = de + re; }returnnum; } }; 这个问题叫做digital root. 有一个公式: n - 9 * [(n - 1) / 9]. 其中‘[]’ 表示下取整。
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 = ...
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? Hint: A naive implementation of the above process is triv...
classSolution{public:intaddDigits(intnum){while(num/10>0){intsum=0;while(num!=0){sum+=num%10;num=num/10;}num=sum;}returnnum;}}; 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 解法二: 题目要求没有递归,没有循环。且时间复杂度为O(1)。那就找规律。
digits数字stored in reverse反向存储 each of每一个nodes节点 3 惊人而又蹩脚的中文翻译 本题主要是类似数据在机器中存储的方式,我们平常所见的数据比如342,在链表中是逆向存储的所以就成了2->4->3这样了,同样5 -> 6 -> 4就是465, 如果这样转换后,我们就会发现342+465=807,在十位上相加是超过10向前进一...
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 contains a single digit. Add the two numbers and retu…
可以发现输出与输入的关系为: out = (in - 1) % 9 + 1 C++代码: class Solution { public: int addDigits(int num) { return (num-1)%9+1; } };
3 + 4 + 5) % 9。证明完成:12345 % 9 = (1 + 2 + 3 + 4 + 5) % 9 ; 代码 publicclassSolution{/** * @param num a non-negative integer * @return one digit */publicintaddDigits(intnum){return(num-1)%9+1;}}
[LeetCode] 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 i You are given two linked lists representing two non-negative numbers...