LeetCode 258
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 = 11
, 1 + 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:
- A naive implementation of the above process is trivial. Could you come up with other methods?
- What are all the possible results?
- How do they occur, periodically or randomly?
- You may find this Wikipedia article useful.
- public class Solution {
- public int addDigits(int num) {
- return (num-1) % 9 + 1;
- }
- }
- /*************************************************************************
- > File Name: LeetCode258.c
- > Author: Juntaran
- > Mail: JuntaranMail@gmail.com
- > Created Time: Tue 10 May 2016 07:44:57 PM CST
- ************************************************************************/
- /*************************************************************************
- 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 = 11, 1 + 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:
- A naive implementation of the above process is trivial.
- Could you come up with other methods?
- What are all the possible results?
- How do they occur, periodically or randomly?
- You may find this Wikipedia article useful.
- ************************************************************************/
- #include <stdio.h>
- int addDigits( int num )
- {
- int ret = (num-) % + ;
- return ret;
- }
- int main()
- {
- int n = ;
- int ret = addDigits(n);
- printf("%d\n", ret);
- return ;
- }
LeetCode 258的更多相关文章
- LeetCode 258. 各位相加(Add Digits)
258. 各位相加 258. Add Digits 题目描述 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. LeetCode258. Add Digits 示例: 输入: 3 ...
- Leetcode -- 258 数位相加
258. Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...
- LN : leetcode 258 Add Digits
lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...
- LeetCode 258 Add Digits(数字相加,数字根)
翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定sum = 38,这个过程就像是:3 + 8 = 11.1 + 1 = 2.由于2仅仅有一位数.所以返回它. ...
- [LeetCode] 258. Add Digits 加数字
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- Java实现 LeetCode 258 各位相加
258. 各位相加 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由 ...
- LeetCode 258. Add Digits
Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...
- (easy)LeetCode 258.Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- Java [Leetcode 258]Add Digits
题目描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
随机推荐
- 提高iOS开发效率的方法和工具
http://www.cocoachina.com/ios/20150717/12626.html 介绍 这篇文章主要是介绍一下我在iOS开发中使用到的一些可以提升开发效率的方法和工具. IDE 首先 ...
- 【转】并发编程之GCD
http://blog.xcodev.com/blog/2013/11/04/gcd-intro/ Dispatch Queue Dispatch Queue是一个任务执行队列,可以让你异步或同步地执 ...
- HDU 5775 Bubble Sort (线段树)
Bubble Sort 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...
- iOS学习之触摸事件
触摸事件 iOS中的事件: 在用户使用app过程中,会产生各种各样的事件.iOS中的事件可以分为3大类型: view的触摸事件处理: 响应者对象: 在iOS中不是任何对象都能处理事件,只有继承了UIR ...
- AppDelegate 、UIApplication 的用法
转载自 http://blog.chinaunix.net/uid-26768267-id-3300042.html //AppDelegate.h 头文件 #import <UIKit/UI ...
- UIImageView旋转任意角度
-(UIImageView *) makeRotation:(UIImageView *)image speedX:(float)X speedY:(float)Y { // 头文件中需要定义 ...
- HDU 2050 折线分割平面 (递推)
题意:略. 析:多写几个就找到规律了,第1条是2,2条时是7个,3条时是16,4条时是29,.... 那么规律就出来了2 * n * n + 1 - n; 也可以递推,第n条折线的两条边都与前n-1条 ...
- [转]Oracle关于null的处理
转至:http://www.2cto.com/database/201209/157606.html 关于空值null的排序问题 Oracle排序中NULL值处理的五种常用方法: 1.缺省O ...
- CKEditor与CKFinder整合并实现文件上传功能
事先说明:此整合的是java版本的, 用到的有:jsp + ckeditor + ckfinder (没有servlet 及其它框架技术) 一.需要的资源: 用到的网站,文件自己下载: a) cked ...
- 教你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 ...