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. TVS瞬态抑制二极管选型指南

    一.TVS二极管工作原理 TVS(Transient Voltage Suppressors)二极管,即瞬态电压抑制器,又称雪崩击穿二极管,是采用半导体工艺制成的单个PN结或多个PN结集成的器件.TV ...

  2. BeautifulSoup模板简单应用-提取html指定数据(api_name/api_method/api_path,请求body/请求header/pagam参数)

    from bs4 import BeautifulSoup import re import os.path import itertools name='newcrm' source_file_pa ...

  3. 国赛 strange_int

    参考文章地址https://www.52pojie.cn/thread-936377-1-1.html https://qrzbing.cn/2019/04/27/CISCN2019-strange- ...

  4. memorization-根据输入重新计算render的数据

    在实际开发过程中,经常遇到根据props和state变化,重新计算“渲染阶段”需要的数据的情况. 如:根据输入的值实时过滤select列表,或者表格数据(查询过滤). 问题特点: 1. 每次渲染都会调 ...

  5. NISP二级笔记(一) 信息安全管理

    ISO27001 信息安全管理体系要求 ISO27002 信息安全控制措施(实用规则) ISO27003 信息安全管理体系实施指南 ISO27004 信息安全管理测量 ISO27005 信息安全风险管 ...

  6. greenplum常见问题及解决方法

    本文链接:https://blog.csdn.net/q936889811/article/details/85612046                文章目录 1.错误:数据库初始化:gpini ...

  7. Mybatis 入门 (二)

    1. Mapper配置文件处理特殊字符 用 > 和 &It; 代替 > 和 < 2. 延迟加载 单表查询性能比多表关联查询要高得多,即先查询单表,如果需要关联多表时再进行查询 ...

  8. learning java 正则表达式

    var regStr = "Java is very good"; Matcher m = Pattern.compile("\\w+").matcher(re ...

  9. cube.js 学习(六)cube.js segments 说明

    segments 是你需要查询的数据的子集,实际上filter 也可以做类似的事情,但是,目前这个设计估计是为了更好的数据 查询吧,同时在操作界面上我们也可以看出来 参考格式 segments: { ...

  10. Open Judge 1.4 09

    09:判断能否被3,5,7整除 总时间限制:  1000ms 内存限制:  65536kB 描述 给定一个整数,判断它能否被3,5,7整除,并输出以下信息:1.能同时被3,5,7整除(直接输出3 5 ...