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. Mybatis下Oracle插入新增返回主键id

    具体xml中sql是这样写,但是要注意SQ_USER.Nextval,SQ_USER是序列,你要替换下自己要进行操作的表的序列,不知道序列的话,可以sql查找下,select * from user_ ...

  2. [React] Write a Custom React Effect Hook

    Similar to writing a custom State Hook, we’ll write our own Effect Hook called useStarWarsQuote, whi ...

  3. django 第五天 自定义标签 静态文件

    昨日忘记上传,先预留位置,稍后补上

  4. Acwing P274 移动服务 题解

    每日一题 day21 打卡 Analysis DP的状态为已经完成的请求数量,通过指派一位服务员可以把”完成i - 1个请求的状态”转移到”完成i个请求的状态”那么我们可以知道转移从dp[i - 1] ...

  5. php大文件分块上传断点续传demo

    前段时间做视频上传业务,通过网页上传视频到服务器. 视频大小 小则几十M,大则 1G+,以一般的HTTP请求发送数据的方式的话,会遇到的问题:1,文件过大,超出服务端的请求大小限制:2,请求时间过长, ...

  6. 配置asgi来达到能处理websocket

    在项目中使用了webscoket进行实时通讯,但是生产环境又使用了django+nginx+uwsgi的部署方式,我们都知道uwsgi并不能处理websocket请求,所以需要asgi服务器来处理we ...

  7. firefox设置

    如果长期使用linux(我常使用的是fedora)的朋友,就会知道,默认情况下,只要是这台linux再内网,开机几个小时后就无法再访问 google 和webqq 这些网站了并且打开一些网站也是要话上 ...

  8. Kafka(二) —— Server端设计原理

    整理架构 kafka confluence kafka官方文档中文翻译-设计 消息设计 /** * 消息类 * * @author Michael Fang * @since 2019-11-14 * ...

  9. 时间控件My97DatePicker事件监听及用法

    引入js <script src="My97DatePicker/WdatePicker.js"></script> 应用 <input type=& ...

  10. [spring-boot] ControllerAdvice异常类

    package com.example.demo.exception; import org.springframework.web.bind.annotation.ControllerAdvice; ...