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 ...
随机推荐
- 数据库防sql注入
- POJ2001Shortest Prefixes(字典树)
题目大意就是帮你给N条字符串,每条长度不超过20.问要将他们单一识别出来,每个字符串最短可以缩为多短. 如: abc abcdefg bc adef 这四个字符串就可分别缩写为 abc abcd b ...
- #pragma comment使用
编程经常碰到,理解的总不是很透彻,在这里查阅资料总结一下! 在编写程序的时候,我们常用到#pragma指令来设定编译器的状态或者是指示编译器完成一些特定的动作. #pragma once : 这是一个 ...
- ZendFramework2 与MongoDB的整合
从网上找了很多文章,先是直接搜关键字找zf2与mongoDB的文章,然后回到源头先学习了一下mongoDB是什么,以及纯PHP环境下怎么用,又从github上找了几个mongoDB的zf2模块,还FQ ...
- JQuery要点(一)
简介: jQuery Mobile 是用于创建移动 Web 应用的前端开发框架. jQuery Mobile 可以应用于智能手机与平板电脑. jQuery Mobile 使用 HTML5 & ...
- C# - DynamicObject with Dynamic
本文转载:http://joe-bq-wang.iteye.com/blog/1872756 里面有动态Linq to xml的写法. There is a expando object which ...
- openssl生成rsa公私钥
1.生成私钥pem, 执行命令openssl genrsa -out rsa_private_key.pem 1024 2.生成公钥,执行命令openssl rsa -in rsa_private_ ...
- Codeforces Beta Round #51 D. Beautiful numbers 数位dp
D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...
- Android常用代码集合
这篇文章主要记录一些常用的一些代码段,方便以后查阅,不断更新中. 1:调用浏览器,载入某网址 1 2 3 Uri uri = Uri.parse("http://www.android-st ...
- iOS开发——网络编程Swift篇&(三)同步Get方式
同步Get方式 // MARK: - 同步Get方式 func synchronousGet() { //创建NSURL对象 var url:NSURL! = NSURL(string: " ...