Leetcode no. 258
来源:互联网 发布:淘宝的花呗怎么还款 编辑:程序博客网 时间:2024/06/01 16:17
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) { if (num==0) return 0; return (num % 9==0) ? 9 : (num % 9); }}
0 0
- Leetcode no. 258
- LeetCode No.258 Add Digits
- LeetCode No.11 ~No.12
- Leetcode NO.78 Subsets
- Leetcode NO.77 Combinations
- Leetcode NO.46 Permutations
- 【leetcode】No. 235 LCABST
- LeetCode no. 35
- Leetcode no. 37
- Leetcode no. 279
- Leetcode no. 335
- Leetcode no. 283
- Leetcode no. 181 (sql)
- Leetcode no. 40
- Leetcode no. 238
- Leetcode no. 26
- Leetcode no. 80
- Leetcode no. 228
- Android lint 定制规则
- iOS快速开发框架--Bee Framework
- leetcode_c++:Unique Paths(062)
- 与 '\r' 字符的两次交锋
- PyQt5:QCalendarWidget日历部件(27)
- Leetcode no. 258
- 八. Linux 常用命令搜集
- Socket编程之非阻塞connect
- Base64编码解码原理详解
- 在无头单链表的一个非头节点前插入一个节点
- 第8章 线程池的使用
- Redis简单使用
- 点击一个程序,程序在操作系统的工作过程
- 解决mac 下的 Sourcetree 每次拉取提交都需要输入密码