LeetCode_258. Add Digits
258. Add Digits
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.
Since2has 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的更多相关文章
- 【LeetCode】Add Digits
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 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 ...
- 【LeetCode】258. Add Digits (2 solutions)
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- [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. ...
- 258. Add Digits(C++)
258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has ...
- 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 ...
- 【02_258】Add Digits
Add Digits Total Accepted: 49702 Total Submissions: 104483 Difficulty: Easy Given a non-negative int ...
- LeetCode:Add Digits - 非负整数各位相加
1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...
- LN : leetcode 258 Add Digits
lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...
随机推荐
- indexOf()、lastIndexOf()、startsWith()等方法应用
- HDFS写机制
HDFS写机制: 1.client客户端调用分布式文件系统对象DistributedFileSystem对象的create方法,创建一个文件输出流FSDataOutputStream对象. 2.Dis ...
- Python3学习(一)
基本语法 python3不向下兼容,有些语法跟python2.x不一样,IDLE shell编辑器,快捷键:ALT+p,上一个历史输入内容,ALT+n 下一个历史输入内容.#idle中按F5可以运行代 ...
- HTML5新增input标签属性
一. input type属性 <form action=""> 邮箱<input type="email" name="" ...
- 洛谷 P1966 火柴排队 题解
归并排序 很玄学的一道题目,用另类的方法求出逆序对的数量就可以AC 我的思路是这样的: 按照题目,输入数据用两个数组a,b储存, 同时,用另外两个数组c,d分别对应前面两个a,b储存, 就是前面两个的 ...
- 数组splay ------ luogu P3369 【模板】普通平衡树(Treap/SBT)
二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) #include <cstdio> #define Max 100005 #define Inline _ ...
- lixuxmint系统定制与配置(1)-系统初始配置
小书匠Linux 经常安装新的系统,每次安装完都得去搜索一边如何将系统部署为之前的环境,不仅耗费时间,还不一定能弄回之前的环境,现在把从裸机->到工作环境的系统定制及配置过程记录下来,期间的配置 ...
- Unity 绘制Mesh线条
using UnityEngine; using System.Collections; using System.Collections.Generic; public struct Segme ...
- 【微信小程序】如何获取用户绑定手机号
用户调用wx.login()方法,获取登录用户凭证code wx.login({ success: function(res) { console.log('loginCode', res.code) ...
- php 压缩文件
<?php $zip = new ZipArchive; $myfile = fopen("test.zip", "w"); chmod(); if ($ ...