分析一:最简单的循环方法 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 cl...
Could you do it without any loop/recursion in O(1) runtime? 题目描写叙述:给出一个数组,不断地切割数字,直到这个数字是一个数字为止。题目非常easy,直接上代码。 代码实现: class Solution { public: int addDigits(int num) { while(num<10) { return num; } int s=0; while(num>0) { s+=nu...
题目链接: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?
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 = ...
public class Solution { public int getAllSum(int val) { int sum = 0; while (val > 0) { sum += val % 10; val /= 10; } return sum; } public int addDigits(int num) { if (num <= 0) return 0; while (num >= 10) { ...
简介:给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。 Description 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. ...
Leetcode 258. Add Digits JAVA语言 1 2 3 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....
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. ruochen 2022/01/14 1.2K0 LeetCode 241. Different Ways to Add Parentheses 递归 题目递归,分治,暴力跑就可以了 class Solution { public: vector<int> ...
You are given twonon-emptylinked lists representing two non-negative integers. The digits are stored inreverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero,...
Example Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 解题思路 就是按照我们小时候的竖式加法的方式来计算就可以了。不同的是,这里的进位是向右进位;而我们小时候计算加法的进位是向左进位。 My Solution 代码语言:javascript ...