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?

貌似是前天还是大前天新加的一道题来着。

这是一道很简单的题哈。直接用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)的更多相关文章

  1. [LeetCode] Add Digits 加数字

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

  2. (leetcode)Add Digits

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

  3. LeetCode——Add Digits

    Description: Given a non-negative integer num, repeatedly add all its digits until the result has on ...

  4. [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 ...

  5. LeetCode Add Digits (规律题)

    题意: 将一个整数num变成它的所有十进制位的和,重复操作,直到num的位数为1,返回num. 思路: 注意到答案的范围是在区间[0,9]的自然数,而仅当num=0才可能答案为0. 规律在于随着所给自 ...

  6. LeetCode:Add Digits - 非负整数各位相加

    1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...

  7. LeetCode 258. 各位相加(Add Digits)

    258. 各位相加 258. Add Digits 题目描述 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. LeetCode258. Add Digits 示例: 输入: 3 ...

  8. 【LeetCode】Add Digits

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

  9. 【LeetCode】258. Add Digits (2 solutions)

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

随机推荐

  1. 服务器端json解析

    1.客户端通过http传上来的可定都是json数据啊,json数据传到服务器端,就要通过键值对getkey方法得到具体值,对吧,然后再操控具体值. 2.JSONObject与JSONArray: Js ...

  2. linux系统的文件类型学习

    linux是一个文件型操作系统,在linux下一切皆文件. 目录.字符设备.块设备.管道.套接字.符号连接文件等在linux下统统都是文件. linux下的文件类型分为以下几种类型: 1. 正规文件, ...

  3. andorid源码中察看版本

    build\core\version_defaults.mk //搜索该文件中的 PLATFORM_VERSION值

  4. 【转】android 自定义控件

    Android自定义View实现很简单 继承View,重写构造函数.onDraw,(onMeasure)等函数. 如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml.在 ...

  5. gulp edm测试

    gulp工具的 gulp-mailgun 插件可以将你的html代码,通过mailgun服务器来发送,用于测试,用这个工具发送邮件最适合不过了. 首先我们需要引入gulp和gulp-mailgun模块 ...

  6. enum,struct,union类型使用和长度

    VC,C++ Builder和lcc三个编译器 间枚举类型enum长度的情况. 各种C编译器默认的字节对齐数不一致,要写通用的代码,经常就是使用 #pragma pack(1) ... #pragma ...

  7. bzoj4011

    好题,首先有一个结论,有向无环图的树形图数目=根节点意外入度之积 现在相当于在原图上加一条边问树形图的数目 考虑多出来不合法的方案,一定是成环且包含新加入的边 对于一条路贡献就是∏d[i] [i∉pa ...

  8. Qt之进程间通信(QProcess)

    简述 QProcess可以在应用程序内部与其它进程通信,或启动其它应用程序.与在终端机之类的命令输入窗口上使用名称和参数是一样的,可以使用QProcess提供的函数start()启动进程.可以注册QS ...

  9. SharePoint CMAL方式处理的 增,删,查,改

    SPContext.Current.Web.Lists["UserInfo"]:获取网站的List,名称是:UserInfo userlist.AddItem():添加数据到Lis ...

  10. bzoj1877: [SDOI2009]晨跑

    挺裸的最小费用最大流... #include<cstdio> #include<queue> #include<cstring> #include<iostr ...