【Leetcode算法】-Add Digits
来源:互联网 发布:北斗卫星定位精度知乎 编辑:程序博客网 时间:2024/06/05 15:36
题目:
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?
链接:https://leetcode.com/problems/add-digits/
解法1:
%% C++class Solution {public: int add_digits(int num, int sum) { int remain; remain = num % 10; num = num / 10; if (num == 0) if (sum + remain >= 10) return add_digits(sum + remain, 0); else return sum + remain; else return add_digits(num, sum + remain); } int addDigits(int num) { return add_digits(num, 0); }};
0 0
- 【Leetcode算法】-Add Digits
- [leetcode][math] Add Digits
- [leetcode] Add Digits
- 【leetcode】Add Digits【java】
- Leetcode: Add Digits
- LeetCode Add Digits
- [LeetCode] Add Digits
- leetcode 258: Add Digits
- LeetCode题解:Add Digits
- leetcode: Add Digits
- 258Add Digits - LeetCode
- leetCode #258 Add Digits
- [LeetCode 258] Add Digits
- LeetCode: Add Digits
- LeetCode:Add Digits
- leetcode--add digits
- leetcode Add Digits
- leetcode--Add Digits
- 伪集群模式zookeeper
- 【codevs1226】倒水问题 宽搜
- 优秀程序设计的18大原则
- Navicate for MySQL 的一点细节问题
- 分布式MySQL数据库TDSQL架构分析
- 【Leetcode算法】-Add Digits
- linux内存管理之vmalloc
- Android进程和线程
- linux内存管理之malloc
- Canonical Correlation Analysis 典型相关分析
- Mybatis系列(七)关联映射
- C++ core guidelines -- P.1. -- 直接在代码中表达你的想法
- linux内存管理之内存映射
- linux内存管理之DMA