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 = 111 + 1 = 2. Since 2 has only one digit, return it.

Follow up:
Could you do it without any loop/recursion in O(1) runtime?

方法1:常规做法,循环,不符合题意。

public class Solution {
public int addDigits(int num) {
while(num>9){
int p=0;
while(num!=0){
p+=num%10;
num=num/10;
}
num=p;
}
return num;
}
}

方法2:找到规律,一行代码

代码如下:

public class Solution {
public int addDigits(int num) {
return (num-1)%9+1;
}
}

  

(easy)LeetCode 258.Add Digits的更多相关文章

  1. LN : leetcode 258 Add Digits

    lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...

  2. LeetCode: 258 Add Digits(easy)

    题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...

  3. LeetCode 258 Add Digits(数字相加,数字根)

    翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定sum = 38,这个过程就像是:3 + 8 = 11.1 + 1 = 2.由于2仅仅有一位数.所以返回它. ...

  4. [LeetCode] 258. Add Digits 加数字

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  5. LeetCode 258. Add Digits

    Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...

  6. Java [Leetcode 258]Add Digits

    题目描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...

  7. LeetCode 258 Add Digits 解题报告

    题目要求 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...

  8. leetcode 258. Add Digits(数论)

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  9. leetcode 258. Add Digits——我擦,这种要你O(1)时间搞定的必然是观察规律,总结一个公式哇

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

随机推荐

  1. System.AccessViolationException,尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

    从事件查看器中发现,IIS不定期崩溃并重启的现象.抓取crash dump文件后,发现能够看到异常,但没有堆栈信息(主要是只会看托管代码的堆栈,非托管的不清楚.),问题表现及dump日志的截图如下: ...

  2. java指针

    import java.util.ArrayList; import java.util.List; public class TestPoint { public static void main( ...

  3. [extjs] ExtJS4 treepanel 子节点选中父节点自动选中,选中父节点 子节点自动全部选中

    ExtJS4 treepanel 主要添加viewConfig重的代码: xtype:'treepanel', store: menuStore, id:'menuTreePanel', viewCo ...

  4. innodb数据库批量转换表引擎为MyISAM

    2013.0106 innodb数据库批量转换表引擎为MyISAM 来源:本站原创 PHP, 数据库, 系统技术 超过488名童鞋围观 1条评论  <?php //连接数据库 $host='lo ...

  5. sqlite 下载的 ZIP 包的区别

    https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki sqlite-netFx20-binary-bundle- ...

  6. CentOS6.8安装Redis3.2.5

    1.下载Redis3.2.5安装包           wget http://download.redis.io/releases/redis-3.2.5.tar.gz 2.解压.编译.安装redi ...

  7. 关于mysql数据库在输入密码后,滴的一声直接退出界面的解决办法

    转自:http://www.2cto.com/database/201412/361751.html 网上搜索到的解决办法: 1.找到mysql安装目录下的bin目录路径.2.打开cmd,进入到bin ...

  8. sql里将重复行数据合并为一行,数据用逗号分隔

    一.定义表变量 DECLARE @T1 table ( UserID int , UserName ), CityName ) ); ,'a','上海') ,'b','北京') ,'c','上海') ...

  9. ACL权限设置使用

    acl操作 # 查看操作的分区支不支持acldumpe2fs -h /dev/sda2 | grep aclDefault mount options: user xattr acl 代表支持acl如 ...

  10. JVM知识学习与巩固

    JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的. 我们运行和调 ...