Easy-21
来源:互联网 发布:淘宝上的零食安全吗 编辑:程序博客网 时间:2024/06/01 08:06
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?
AC:
int addDigits(int num) {
return 1+(num-1)%9;
}
tips:刚开始看到这个题目,首先想到的就是循环一次就行了,但是题目要求O(1)的复杂度,所以......然后就开始了百思不得其姐。
一个个列出来看看有没有什么规律。。 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20.。。。。
结果应该是 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 .。。。。
模9?!然后就可以开始coding 了 ....
0 0
- Easy-21
- <LeetCode><Easy> 21 MergeTwoSortedLists
- easy
- Easy
- [Leetcode 21, Easy] Merge Two Sorted Lists
- Easy-题目21:326. Power of Three
- Easy 21 Symmetric Tree(101)
- 【Leetcode-Easy-21】Merge Two Sorted Lists
- LeetCode-21-Merge Two Sorted Lists(链表/归并)-Easy
- NHK Easy News 翻译 12月21日-2
- NHK Easy News 翻译 12月21日-1
- Easy 6 Merge Two Sorted Lists(21)
- LeetCode OJ 21 Merge Two Sorted Lists [Easy]
- LeetCode-Easy刷题(21) Maximum Depth of Binary Tree
- Easy Plugs
- easy problem
- So Easy!
- so easy
- 聊聊架构(1)
- extend(扩展)和append(追加)的区别
- Softmax求导计算
- 国内镜像安装python相关包
- Leetcode 323. Number of Connected Components in an Undirected Graph
- Easy-21
- Problem F: 来算一下自己的成绩吧!
- C++中引用(&)的用法和应用实例
- jQuery中的一些自己写的方法
- 【Get深一度】Hilbert变换(平时写的文档,没法直接复制粘贴到CSDN里,只好截图了)
- 调取微信相册相机
- 3872---->幸运名单
- Android 弹出对话框 且焦点在确认或者取消
- oracle imp和exp