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. P4357 [CQOI2016]K远点对

    题意:给定平面中的 \(n\) 个点,求第 \(K\) 远的点对之间的距离,\(n\leq 1e5,K\leq min(100,\frac{n\times (n-1)}{2})\) 题解:kd-tre ...

  2. 对日开发中 PG , PL , SE , PM 是什么

    PG(ProGramer)指程序员. 这类人才在企业中所占数量最多,通常占到整个项目员工数的70%,也是企业中最紧缺的一类职位,一般为具有专业知识的软件工程技术人员. PL(project leade ...

  3. (尚006)Vue计算属性之set与get

    test004.html <!DOCTYPE html><html lang="en"><head> <meta charset=&quo ...

  4. Ubuntu 系统安装ssh的命令

    更新源列表 打开"终端窗口",输入"sudo apt-get update"-->回车-->"输入当前登录用户的管理员密码"-- ...

  5. codecs 1264 芳香数

    1264 芳香数 题目描述 Description This question involves calculating the value of aromatic numbers which are ...

  6. python 之 序列 常用方法

  7. combox使用自定义的model列表中无元素显示

    自定义的model(stationModel)中有 name 和point两种属性名. 初始化stationModel Combobox{ textRole: 'name' model:station ...

  8. python 路径引用问题

    文件结构 入口文件· 将当前文件的父级,加入搜索目录里面 import sys import os current_dir = os.path.abspath(os.path.dirname(__fi ...

  9. 64位内核开发第二讲.内核编程注意事项,以及UNICODE_STRING

    目录 一丶驱动是如何运行的 1.服务注册驱动 二丶Ring3跟Ring0通讯的几种方式 1.IOCTRL_CODE 控制代码的几种IO 2.非控制 缓冲区的三种方式. 三丶Ring3跟Ring0开发区 ...

  10. GAN 原理及公式推导

    Generative Adversarial Network,就是大家耳熟能详的 GAN,由 Ian Goodfellow 首先提出,在这两年更是深度学习中最热门的东西,仿佛什么东西都能由 GAN 做 ...