LeetCode 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 without any…
题意: 将一个整数num变成它的所有十进制位的和,重复操作,直到num的位数为1,返回num. 思路: 注意到答案的范围是在区间[0,9]的自然数,而仅当num=0才可能答案为0. 规律在于随着所给自然数num的递增,结果也是在1~9内循环递增的,那么结果为(num-1)%9+1. C++: class Solution { public: int addDigits(int num) { ; )%+; } }; AC代码 python: class Solution(object): def…
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 an…
Description: 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 i…
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…
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…
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = function(num) { var b = (num-1) % 9 + 1 ; return b; }; //之所以num要-1再+1;是因为特殊情况下:当num是9的倍数时,0+9的数字根和0的数字根不同. 性质说明 1.任何数加9的数字根还是它本身.(特殊情况num=0)        小学学加法的…
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位的数…
258. 各位相加 258. Add Digits 题目描述 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. LeetCode258. Add Digits 示例: 输入: 38 输出: 2 解释: 各位相加的过程为: 3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所以返回 2. 进阶: 你可以不使用循环或者递归,且在 O(1) 时间复杂度内解决这个问题吗? Java 实现 class Solution { public int addDigits(i…
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…