258. Add Digits

Easy

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.
  Since 2 has only one digit, return it.

Follow up:
Could you do it without any loop/recursion in O(1) runtime?

package leetcode.easy;

public class AddDigits {
public int addDigits(int num) {
if (num == 0) {
return 0;
} else {
return ((num - 1) % 9) + 1;
}
} @org.junit.Test
public void test() {
System.out.println(addDigits(38));
}
}

LeetCode_258. Add Digits的更多相关文章

  1. 【LeetCode】Add Digits

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

  2. Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference

    最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...

  3. 【LeetCode】258. Add Digits (2 solutions)

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

  4. [LeetCode] Add Digits (a New question added)

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

  5. 258. Add Digits(C++)

    258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has ...

  6. 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 ...

  7. 【02_258】Add Digits

    Add Digits Total Accepted: 49702 Total Submissions: 104483 Difficulty: Easy Given a non-negative int ...

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

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

  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. Tensorflow细节-P42张量的概念及使用

    1.运行以下代码 import tensorflow as tf a = tf.constant([1.0, 2.0], name="a") b = tf.constant([2. ...

  2. (转载) SQL Server AG集群启动不起来的临时自救大招

    背景 前晚一朋友遇到AG集群发生来回切换不稳定的情况,情急之下,朋友在命令行使用命令重启WSFC集群 结果重启WSFC集群之后,非但没有好转,导致整个AG无法启动,主副本和辅助副本都处于正在解析的状态 ...

  3. BZOJ 2064: 分裂 状压动归

    最多的操作次数是 $n+m-1$ (相当于把第一个暴力合并,再暴力拆成第二个).如果第一个序列的一个子序列和第二个区间的子序列相等,那么总次数就可以减 $2$.将第二个序列所有数取反,直接求解有多少个 ...

  4. Error Codes Messages查找工具介绍

    当我们通过各种手段获取到一个Windows错误码后,如何获取对应的错误消息呢?有两种方法:一是用编程的手段(FormatMessage):其二是用现成的工具.第一种方法一般在我们编码的时候有用,而更多 ...

  5. C# Dictionary使用

    Dictionary<string, int> illegParking = new Dictionary<string, int>(); 键:inData.LOTID 值:i ...

  6. 【HTML】解析原理

    标准的web前端工程师需要知道 ◎浏览器(或者相应播放器)的渲染/重绘原理 这我得加把劲了.我还真的说的不是很清楚,我就G下,结果不是很多,找到了有一个,就记下来了... 以下部分来自handawei ...

  7. redis堵死致数据清空

    情景: zy的链路监控突然都恢复,而且在哪个时间段zabbix中显示回复,也发送了告警,但是实际上告警并没有发出来.这是不可能的情况,应该是redis缓存中的数据都被清空了,没有认为干预,需解决问题 ...

  8. python3 结束子线程

    最近公司内部网络经常出问题,奇慢无比,导致人脸检测程序在下载图片时经常卡住,为了不影响数据的核对, 决定在网络不佳图片下载超时后放弃下载,继续执行后续程序. 于是整理出解决思路如下: 1.在线程中完成 ...

  9. FCS省选模拟赛 Day4

    传送门 Solution Code  /* 斯坦纳树:O(n*3^n+kE*2^n) 暂且把O(k*E)当成是spfa的复杂度 15:15~16:20 原题:bzoj_4774 */ #include ...

  10. 查看CPU占用工具

    recording_5492_1.jfr jcmd 5942 JFR.start delay=1s duration=200s name=serverRecording filename=./reco ...