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:常规做法,循环,不符合题意。

  1. public class Solution {
  2. public int addDigits(int num) {
  3. while(num>9){
  4. int p=0;
  5. while(num!=0){
  6. p+=num%10;
  7. num=num/10;
  8. }
  9. num=p;
  10. }
  11. return num;
  12. }
  13. }

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

代码如下:

  1. public class Solution {
  2. public int addDigits(int num) {
  3. return (num-1)%9+1;
  4. }
  5. }

  

(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. html之input系列标签

    input属性太多,我这里仅列出几个我喜欢的吧. disabled:首次加载时禁用此元素 checked:首次加载时选中此元素 form:输入字段所属的一个或多个表单 hieght:定义input字段 ...

  2. js添加事件通用方法

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. Rsync企业实战之自动异地备份(转)

    认真的测试过网上的大多数文章和版本,真正能一次性测试通过的文章太少了,需要反复的推敲,反复的查阅资料,才能真正的测试成功,所以,在此背景下,总结了Rsync,加上自己的理解分享出来: 1. 原理篇 R ...

  4. 转: Ext拖拽分析

    整个Ext架构中组件是其重要的组成部分,除了少部分(如树的结点)的界面表现元素不是在这样的一个体系中,大部分的页面表现元素都被绑定在这个体系之中,下面从这个体系的最底层即在这个继承体系的最高层进行研究 ...

  5. 【转】如何使用PhoneGap打包Web App

    如何使用PhoneGap打包Web App 最近做了一款小游戏,定位是移动端访问,思来想去最后选择了jQuery mobile最为框架,制作差不多以后,是否可以打包成App,恰好以前对PhoneGap ...

  6. linux挂载文件

    Linux挂载Winodws共享文件夹 mount -t cifs -o username=***,password=*** //192.168.1.48/share /mnt 其中-t表示要挂载的类 ...

  7. linux下进程、端口号相互查看方法

    linux下通过进程名查看其占用端口: 1.先查看进程pid ps -ef | grep 进程名 2.通过pid查看占用端口 netstat -nap | grep 进程pid 例:通过nginx进程 ...

  8. 【sql】之使用sql根据身份证查询过生日人数

    根据当前日期查询有多少人过生日 ,) = DATE_FORMAT(NOW(),'%m'); 查询price一样的人数 select * from people where price in (sele ...

  9. FreeDroid开发过程中遇到的一些问题

    http://bestzp.com/?p=83 Android Studio混淆: build.gradle中   1 2 3 4 5 6 buildTypes {         release { ...

  10. [Java Web – 3A] – Spring MVC开发注意事项

    1.使用Maven构建项目 2.SpringMVC 绝对路径的问题 首先要明确一点,在html中,资源文件也是有自己的URL,即href中是支持绝对路径.如下代码: <link href=&qu ...