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. sqlserver 字符串拼接及拆开联表查询的问题

    一.sql根据一个以逗号隔开的人员guid类型的ID字符串查出其对应的姓名同样拼接成逗号隔开的字符串: 1.需求:管理员发送通知(通知分为普通通知,奖品订单,调查问卷三种类型)给用户,并且可以查看统计 ...

  2. python之selenium

    selenium是处理异步加载的一种方法 总的来说是操作浏览器访问来获取自己想要的资料 优点是浏览器能看到的都能爬下来,简单有效,不需要深入破解网页加载形式 缺点是加载的东西太多,导致爬取速度变慢 # ...

  3. maven学习笔记(定制普通Java一个项目)

    创建一个新项目: mvn archetype:generate -DgroupId=cn.net.comsys.ut4.simpleweather -DartifactId=simple-weathe ...

  4. Jfinal中log4j的配置

    基本不用配置: 1.web.xml不用配置: 2.添加文件log4j.properties到src下面: 3.lib中复制log4j的jar包进去: 4.可以使用了; package demo; im ...

  5. get跟post编码--转

    1.Get是用来从服务器上获得数据(没有请求体),而Post是用来向服务器上传递数据(包含请求体). 2.Get将表单中数据的按照variable=value的形式,添加到action(服务)所指向的 ...

  6. 如何在ExtJS 6中使用Fashion美化应用程序

    在Ext JS 6,一个最大的改变就是框架合并,使用一个单一的代码库,就可以为每一种设备开发各具有良好体验的最好应用程序.它还带来了一种美化应用程序的新方式. 在本文,重点是Sencha Fashio ...

  7. [linux] linux下编译安装zlib

    zlib官方网站:http://www.zlib.net上下载源码来安装zlib软件包. 目前最新版本zlib是zlib1.2.8,安装开始:$wget http://www.zlib.net/zli ...

  8. 多线程操作(ThreadPool.QueueUserWorkItem

    主线程: private void GetPolicy_Load(object sender, EventArgs e) { ////ThreadPool.QueueUserWorkItem(new ...

  9. Servlet Filter 2

    10.Filter常见应用 )统一全站字符编码的过滤器 通过配置参数encoding指明使用何种字符编码,以处理Html Form请求参数的中文问题 案例:编写jsp 输入用户名,在Servlet中获 ...

  10. div的打开与关闭js

    <script type="text/javascript"> var BoxHeight=$('.t_c').css("height"); //$ ...