[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.
For example:
Given num = 38, 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?
貌似是前天还是大前天新加的一道题来着。
这是一道很简单的题哈。直接用loop就可以解决。这是常规思维。接着下面分享一个非常取巧的算法。
常规思维的代码如下。~(不要忘了-‘0’哦)
public class Solution {
public int addDigits(int num) {
String test=String.valueOf(num);
while(test.length()>1){
num=0;
for(int i=0;i<test.length();i++){
num=num+test.charAt(i)-'0';
}
test=String.valueOf(num);
}
return num;
}
}
还有一个非常巧妙的算法。 只要一行即可解决。代码如下。~
这个算法的道理很简单,分别算算1-20的这20个数的add digits的结果,就可以找出规律了。
public class Solution {
public int addDigits(int num) {
return (num-1)%9+1;
}
}
[LeetCode] Add Digits (a New question added)的更多相关文章
- [LeetCode] Add Digits 加数字
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- (leetcode)Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- LeetCode——Add Digits
Description: Given a non-negative integer num, repeatedly add all its digits until the result has on ...
- [LeetCode] Ugly Number (A New Question Added Today)
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- LeetCode Add Digits (规律题)
题意: 将一个整数num变成它的所有十进制位的和,重复操作,直到num的位数为1,返回num. 思路: 注意到答案的范围是在区间[0,9]的自然数,而仅当num=0才可能答案为0. 规律在于随着所给自 ...
- LeetCode:Add Digits - 非负整数各位相加
1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...
- LeetCode 258. 各位相加(Add Digits)
258. 各位相加 258. Add Digits 题目描述 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. LeetCode258. Add Digits 示例: 输入: 3 ...
- 【LeetCode】Add Digits
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 【LeetCode】258. Add Digits (2 solutions)
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
随机推荐
- ubuntu 修改主机及主机名
修改主机: sudo vim /etc/hostname sudo vim /etc/hosts 修改用户名: sudo vim /etc/passwd sudo mv /home/yinggc /h ...
- ubuntu 下搭建vsftp
1. 安装:sudo apt-get install vsftpd 2. 我的目的是建立个ftp,专门的账户访问,账户不可以登陆.不允许匿名登陆 3. 更改配置文件/etc/vsftpd.conf l ...
- REACTOS(193)与汇编编译器(69)的高人
REACTOS(193)与汇编编译器(69)的高人http://blog.csdn.net/caimouse ReactOS编译成VS工程1: 首先从https://www.reactos.org/w ...
- 对于delphi for linux心存梦想的同学,彻底醒醒吧
Borland 曾经多次做市场调查,很多人呼吁推出delphi for linux.可是直到kylix发展到第三版,borland才明白:一个内置C编译器环境并与之关系紧密的免费OS,没有人愿意花钱去 ...
- java.util.regex.PatternSyntaxException: Unclosed character class near index解决办法
使用str.split("[")时,出现java.util.regex.PatternSyntaxException: Unclosed character class near ...
- 转 Difference between WCF and Web API and WCF REST and Web Service
http://www.dotnet-tricks.com/Tutorial/webapi/JI2X050413-Difference-between-WCF-and-Web-API-and-WCF-R ...
- new int[]和new int()的区别
1. new int[] 是创建一个int型数组,数组大小是在[]中指定,例如:int * p = new int[10]; //p执行一个长度为10的int数组.2. new int()是创建一个i ...
- apache启动报错(98)Address already in use: make_sock: could not bind to...
# /etc/init.d/httpd startStarting httpd: (98)Address already in use: make_sock: could not bind to ad ...
- [原创]Android中LocationManager的简单使用,获取当前位置
Android中LocationManager的提供了一系列方法来地理位置相关的问题,包括查询上一个已知位置:注册/注销来自某个 LocationProvider的周期性的位置更新:以及注册/注销接近 ...
- basicjava
.完数 . 第一个完全数是6,它有约数1.2.3.6,除去它本身6外,其余3个数相加,1+2+3=6.第二个完全数是28,它有约数1.2.4.7.14. 28,除去它本身28外,其余5个数相加,1+2 ...