leetcode学习篇五——Add Digits
来源:互联网 发布:uuu网络加速器下载 编辑:程序博客网 时间:2024/06/13 00:37
题目如下:
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?
题目要求能不能在O(1)的时间里求出来,所以感觉应该是有某种规律可以直接推出结果的,所以从1开始列了数字然后算出结果,果真存在规律,从1到9然后又是从1到9,得到一下代码,通过。
class Solution {public: int addDigits(int num) { return num==0?0:(num%9==0?9:num%9); }};
0 0
- leetcode学习篇五——Add Digits
- Leetcode学习(31)—— Add Digits
- LeetCode题解——Add Digits
- LeetCode[258]——Add Digits
- 渣渣写LEETCODE——258. Add Digits
- LeetCode ——258.Add Digits
- Leetcode——258. Add Digits
- leetcode——258——Add Digits
- leetcode解题报告258——Add Digits
- leetcode刷题日记——Add Digits
- LeetCode OJ刷题历程——Add Digits
- leetcode算法——258:Add Digits(基于JAVA)
- [leetcode][math] Add Digits
- [leetcode] Add Digits
- 【leetcode】Add Digits【java】
- Leetcode: Add Digits
- LeetCode Add Digits
- [LeetCode] Add Digits
- 【EndNote】:向word中插入参考文献
- D3D11教程七之2D渲染
- http和socket通信的区别
- 剑指offer:二叉树的深度(递归&&非递归)(java)
- poj-【1942 Paths on a Grid】
- leetcode学习篇五——Add Digits
- [LeetCode-383]Ransom Note(java)
- https协议相关
- Why do I choose to major in software engineering?
- 一个程序员的进化史-第十一章
- 单链表的基本操作
- DbUnit数据库测试之备份与还原
- 2016CCPC东北地区大学生程序设计竞赛-Minimum’s Revenge
- 洛克菲勒给儿子的38封信(前14封选摘)