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(int num) {
return 1 + (num - 1) % 9;
}
}

相似题目

参考资料

LeetCode 258. 各位相加(Add Digits)的更多相关文章

  1. Java实现 LeetCode 258 各位相加

    258. 各位相加 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由 ...

  2. [Swift]LeetCode258. 各位相加 | Add Digits

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  3. Leetcode -- 258 数位相加

    258. Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...

  4. Leetcode——258.各位相加【水题】

    给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所 ...

  5. LeetCode(258.各位相加)的思路及解决过程

    问题如下: 给一个非负整数 num,反复添加所有的数字,直到结果只有一个数字. 例如: 设定 num = 38,过程就像: 3 + 8 = 11, 1 + 1 = 2. 由于 2 只有1个数字,所以返 ...

  6. leetcode 258. 各位相加 (python)

    给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所以 ...

  7. LeetCode:Add Digits - 非负整数各位相加

    1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...

  8. LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree

    258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...

  9. LN : leetcode 258 Add Digits

    lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...

随机推荐

  1. HDU 6086 Rikka with String ——(AC自动机 + DP)

    这是一个AC自动机+dp的问题,在中间的串的处理可以枚举中断点来插入自动机内来实现,具体参见代码. 在这题上不止为何一直MLE,一直找不到结果(lyf相同写法的代码消耗内存较少),还好考虑到这题节点应 ...

  2. Tensorflow object detection API(1)---环境搭建与测试

    参考: https://blog.csdn.net/dy_guox/article/details/79081499 https://blog.csdn.net/u010103202/article/ ...

  3. SQL查询练习题

    1.查询学生"百里守约"的基本信息 select * from students where name='百里守约' 2.查询学生百里守约"或"百里玄策&quo ...

  4. unity手机游戏应用程序调试控制台Lunar Mobile Console - PRO 1.5.5

    unity手机游戏应用程序调试控制台Lunar Mobile Console - PRO 1.5.5 High-performance Unity iOS/Android console built ...

  5. mac PHP安装imageMagic扩展

    1. 安装:ImageMagick:命令:brew install ImageMagick这种方式安装下来的imageMagic,不缺少东西,报错较少.安装之后的位置:/usr/local/Cella ...

  6. PHPStorm提示:phpdoc comment doesn't contain all necessary @throw tag(s)

    选择Settings => Editor => Inspection, 选择PHP => PHPDoc => Missing @throws tag(s) ,把后面的勾勾去掉就 ...

  7. Mockplus更快更简单的原型设计

    更快更简单的原型设计 https://www.mockplus.cn/ Mockplus,更快更简单的原型设计工具.快速创建原型,一键拖拽创建交互,团队协作省事省力.微软.华为.东软.育碧.Oracl ...

  8. 004-行为型-08-状态模式(State)

    一.概述 允许一个对象在其内部状态改变时,改变它的行为 在状态模式中,我们创建表示各种状态的对象和一个行为随着状态对象改变而改变的 context 对象. 注意事项:在行为受状态约束的时候使用状态模式 ...

  9. 泡泡一分钟: A Linear Least Square Initialization Method for 3D Pose Graph Optimization Problem

    张宁 A Linear Least Square Initialization Method for 3D Pose Graph Optimization Problem "链接:https ...

  10. ES6深入浅出-10 ES6新增的数据类型-3.其他类型

    Map类型 Map 类型 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map Ma ...