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. 创建UIImage的两种方法

    1. UIImage *img = [UIImage imageNamed:@"imageName"]; 2. NSString *imageFilePath = [[NSBund ...

  2. UIview定义背景图片

    UIImage *image = [UIImage imageNamed:@"bgimagename"];    UIView *view = [[UIView alloc]ini ...

  3. 基于.net开发chrome核心浏览器

    本文转载自:http://www.cnblogs.com/liulun/archive/2013/04/20/3031502.html 一: 上一篇的链接: 基于.net开发chrome核心浏览器[一 ...

  4. android打印调用栈

    在某些机器上,不能下断点,出现了某个诡异的问题,想到唯一的解决方式,就是打印调用栈了,google发现这个,记录下,以后备用 Log.d(",Log.getStackTraceString( ...

  5. poj1160 post office

    题目大意:有n个乡村,现在要建立m个邮局,邮局只能建在乡村里.现在要使每个乡村到离它最近的邮局距离的总和尽量小,求这个最小距离和. n<300,p<30,乡村的位置不超过10000. 分析 ...

  6. flash文件制作笔记

    在uboot串口台输入printenv 可以分区以及其他信息,如下 hisilicon # printenv bootdelay=1baudrate=115200ethaddr=00:00:23:34 ...

  7. 解决 SQLite数据库 no current row

    场景: SQLite数据库,在查询数据时,提示 标题错误异常.查看堆栈,是在SQLiteDataReader.CheckValidRow 时报错. 数据查询是通过 adapter.Fill(dt) 进 ...

  8. oracle学习笔记(四)oracle内存优化

    emca -config dbcontrol db -repos recreate 解决'oracle Environment variable ORACLE_SID not defined. Ple ...

  9. Form_Form树形结构HTree的开发(案例)

    2014-06-09 Created By BaoXinjian

  10. 用socket操作redis

    代码: $cmd = "*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n"; // set foo bar $socket = socke ...