Add Digits

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?

Hint:

  1. A naive implementation of the above process is trivial. Could you come up with other methods?
  2. What are all the possible results?
  3. How do they occur, periodically or randomly?
  4. You may find this Wikipedia article useful.
  1. public class Solution {
  2. public int addDigits(int num) {
  3. return (num-1) % 9 + 1;
  4. }
  5. }
  1. /*************************************************************************
  2. > File Name: LeetCode258.c
  3. > Author: Juntaran
  4. > Mail: JuntaranMail@gmail.com
  5. > Created Time: Tue 10 May 2016 07:44:57 PM CST
  6. ************************************************************************/
  7.  
  8. /*************************************************************************
  9.  
  10. Add Digits
  11.  
  12. Given a non-negative integer num,
  13. repeatedly add all its digits until the result has only one digit.
  14.  
  15. For example:
  16.  
  17. Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2.
  18. Since 2 has only one digit, return it.
  19.  
  20. Follow up:
  21. Could you do it without any loop/recursion in O(1) runtime?
  22.  
  23. Hint:
  24.  
  25. A naive implementation of the above process is trivial.
  26. Could you come up with other methods?
  27. What are all the possible results?
  28. How do they occur, periodically or randomly?
  29. You may find this Wikipedia article useful.
  30.  
  31. ************************************************************************/
  32.  
  33. #include <stdio.h>
  34.  
  35. int addDigits( int num )
  36. {
  37. int ret = (num-) % + ;
  38. return ret;
  39. }
  40.  
  41. int main()
  42. {
  43. int n = ;
  44. int ret = addDigits(n);
  45. printf("%d\n", ret);
  46. return ;
  47. }

LeetCode 258的更多相关文章

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

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

  2. Leetcode -- 258 数位相加

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

  3. LN : leetcode 258 Add Digits

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

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

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

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

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

  6. Java实现 LeetCode 258 各位相加

    258. 各位相加 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由 ...

  7. LeetCode 258. Add Digits

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

  8. (easy)LeetCode 258.Add Digits

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

  9. Java [Leetcode 258]Add Digits

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

随机推荐

  1. 提高iOS开发效率的方法和工具

    http://www.cocoachina.com/ios/20150717/12626.html 介绍 这篇文章主要是介绍一下我在iOS开发中使用到的一些可以提升开发效率的方法和工具. IDE 首先 ...

  2. 【转】并发编程之GCD

    http://blog.xcodev.com/blog/2013/11/04/gcd-intro/ Dispatch Queue Dispatch Queue是一个任务执行队列,可以让你异步或同步地执 ...

  3. HDU 5775 Bubble Sort (线段树)

    Bubble Sort 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...

  4. iOS学习之触摸事件

    触摸事件 iOS中的事件: 在用户使用app过程中,会产生各种各样的事件.iOS中的事件可以分为3大类型: view的触摸事件处理: 响应者对象: 在iOS中不是任何对象都能处理事件,只有继承了UIR ...

  5. AppDelegate 、UIApplication 的用法

    转载自  http://blog.chinaunix.net/uid-26768267-id-3300042.html //AppDelegate.h 头文件 #import <UIKit/UI ...

  6. UIImageView旋转任意角度

    -(UIImageView *) makeRotation:(UIImageView *)image speedX:(float)X speedY:(float)Y { //    头文件中需要定义  ...

  7. HDU 2050 折线分割平面 (递推)

    题意:略. 析:多写几个就找到规律了,第1条是2,2条时是7个,3条时是16,4条时是29,.... 那么规律就出来了2 * n * n + 1 - n; 也可以递推,第n条折线的两条边都与前n-1条 ...

  8. [转]Oracle关于null的处理

    转至:http://www.2cto.com/database/201209/157606.html 关于空值null的排序问题   Oracle排序中NULL值处理的五种常用方法:    1.缺省O ...

  9. CKEditor与CKFinder整合并实现文件上传功能

    事先说明:此整合的是java版本的, 用到的有:jsp + ckeditor + ckfinder (没有servlet 及其它框架技术) 一.需要的资源: 用到的网站,文件自己下载: a) cked ...

  10. 教你50招提升ASP.NET性能(二十二):利用.NET 4.5异步结构

    (40)Take advantage of .NET 4.5 async constructs 招数40: 利用.NET 4.5异步结构 With the arrival of .NET 4.5, w ...